android Gallery 仿iPhone 图片滑动

jerry Android 2015年11月23日 收藏

先在将Gallery标签放入。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. />
  12. <Gallery
  13. android:id="@+id/gallery"
  14. android:layout_width="fill_parent"
  15. android:layout_height="fill_parent"
  16. />
  17. </LinearLayout>

Gallery 需要用Adapter来填充,先从BaseAdapter中派生一个ImageAdapter出来

  1. public class ImageAdapter extends BaseAdapter
  2. {
  3. private Context context;
  4. private int[] MyImageIDs =
  5. { R.drawable.icon, R.drawable.carlogo_52design_09,
  6. R.drawable.carlogo_52design_13, R.drawable.carlogo_52design_19,
  7. R.drawable.carlogo_52design_24, R.drawable.carlogo_52design_27,
  8. R.drawable.carlogo_52design_29, R.drawable.carlogo_52design_31,
  9. R.drawable.carlogo_52design_34, R.drawable.carlogo_52design_36 };
  10. public ImageAdapter(Context context)
  11. {
  12. // TODO Auto-generated constructor stub
  13. this.context = context;
  14. }
  15. @Override
  16. public int getCount()
  17. {
  18. // TODO Auto-generated method stub
  19. return MyImageIDs.length;
  20. }
  21. @Override
  22. public Object getItem(int arg0)
  23. {
  24. // TODO Auto-generated method stub
  25. return arg0;
  26. }
  27. @Override
  28. public long getItemId(int position)
  29. {
  30. // TODO Auto-generated method stub
  31. return position;
  32. }
  33. @Override
  34. public View getView(int position, View convertView, ViewGroup parent)
  35. {
  36. // TODO Auto-generated method stub
  37. ImageView i = new ImageView(this.context);
  38. i.setImageResource(this.MyImageIDs[position]);
  39. i.setScaleType(ImageView.ScaleType.FIT_XY);
  40. i.setLayoutParams(new Gallery.LayoutParams(120, 120));
  41. return i;
  42. }
  43. }

可左右滑动