一、Gallery的拖动图片特效
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.koo);
Gallery gallery = (Gallery)findViewById(R.id.gallery1);gallery.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter { /* 类成员 myContext为Context父类 */
private Context myContext; /* 使用android.R.drawable里的图片作为图库来源,类型为整数数组 */
private int[] myImageIds = {
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
R.drawable.img5}; /* 建构子只有一个参数,即要储存的Context */
public ImageAdapter(Context c) {
this.myContext = c;
} /* 回传所有已定义的图片总数量 */
public int getCount() {
return this.myImageIds.length;
} /* 利用getItem方法,取得目前容器中影像的数组ID */
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
} /* 取得目前欲显示的影像View,传入数组ID值使之读取与成像 */
public View getView(int position, View convertView, ViewGroup parent) { /* 建立一个ImageView对象 */
ImageView i = new ImageView(this.myContext);
i.setImageResource(this.myImageIds[position]);
i.setScaleType(ImageView.ScaleType.FIT_XY); /*
* 设定这个ImageView对象的宽高,单位为dip
*/
i.setLayoutParams(new Gallery.LayoutParams(80, 80));
return i;
} /* 依据距离中央的位移量 利用getScale回传views的大小(0.0f to 1.0f) */
public float getScale(boolean focused, int offset) { /*
* Formula: 1 / (2 ^
* offset)
*/
return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
}
}
galley.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
Toast.makeText(
EX04_10.this, "第" + position + "张",
Toast.LENGTH_SHORT).show();
}
});