第一章:初入Android大门(Gallery拖动相片特效) AndroidXMLOSCC++
程序员文章站
2024-03-15 12:07:05
...
效果:
main.xml
main.xml
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/widget0" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <Gallery android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="43px" android:layout_y="142px" > </Gallery> </AbsoluteLayout>
package gallery.test; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class GalleryTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /** 获得 Gallery对象组件*/ ((Gallery)findViewById(R.id.image)).setAdapter(new ImageAdapter(this)); } public class ImageAdapter extends BaseAdapter{ private Context myContext; /** 定义图片*/ private int[] myImages={ android.R.drawable.btn_minus, android.R.drawable.btn_radio, android.R.drawable.ic_lock_idle_low_battery, android.R.drawable.ic_menu_camera, }; /** 存储Context*/ public ImageAdapter(Context c){ this.myContext=c; } /** 获得图片数量*/ public int getCount() { // TODO Auto-generated method stub return this.myImages.length; } /** 获得目前图像数组ID*/ @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } /**取得显示图像View,传入数组ID值读取数组图像*/ @Override public View getView(int position, View convertView, ViewGroup parent) { /**实例化ImageView对象*/ ImageView i=new ImageView(this.myContext); /**设置图片*/ i.setImageResource(this.myImages[position]); /**设置View的大小*/ i.setScaleType(ImageView.ScaleType.FIT_XY); /**设置ImageView对象宽度和高度*/ i.setLayoutParams(new Gallery.LayoutParams(120,120)); return i; } /**距离*距离位移梁 利用getScale返回View大小*/ public float getScale(boolean flg,int offset){ return Math.max(0,1.0f/(float)Math.pow(2, Math.abs(offset))); } } }