android图片拖动Gallery

1.Gallery可以拖动图片,实现很炫的效果,效果图如下,可以向左右拖动:

2.布局代码



3.用一个类继承自BaseAdapter来充当容器

 

public class ImageAdapter extends BaseAdapter { private Context mContext; private Integer[] mImageIds = { R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8 }; public ImageAdapter(Context c){ this.mContext = c; } @Override public int getCount() { return mImageIds.length;//获取图片的个数 } @Override public Object getItem(int position) { return position;//获取图片在库中的位置 } @Override public long getItemId(int position) { return position;//获取图片在库中的位置 } @Override public View getView(int position, View convertView, ViewGroup parent) { Log.i("TAG", position+""); ImageView imageView = new ImageView(mContext); imageView.setImageResource(mImageIds[position]); imageView.setLayoutParams(new Gallery.LayoutParams(100,100));//设置图片大小 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);//设置显示比例类型 return imageView; } }

 

4.使用时的java代码:

public class Test_Gallery extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Gallery g = (Gallery) this.findViewById(R.id.Gallery); g.setAdapter(new ImageAdapter(this)); g.setBackgroundResource(R.drawable.bg0); g.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { Toast.makeText(Test_Gallery.this, "选择了"+(arg2+1)+"号图片", Toast.LENGTH_SHORT).show(); } }); } }

你可能感兴趣的:(android,android,layout,integer,encoding,class,object)