用gallery展示图片,实现中间图片稍大,两边较小的效果
实现原理:在onItemSelected事件中,选中galerry中的图片时,把选中的图片放大,把没有选中的缩小。
以下是关键代码
1 public void onItemSelected(AdapterView<?> parent, View view, int position,
2 long id) {
3 // 选中Gallery中某个图像时,放大显示该图像
4 ImageView imageview = (ImageView)view;
5 ((ImageView) view).setImageDrawable((Drawable) product_image_list.get(position));
6 view.setLayoutParams(new Gallery.LayoutParams(520 / 2, 318 / 2));
7 title.setText((String)product_title.get(position));
8 info.setText((String)product_info.get(position));
9 for(int i=0; i<parent.getChildCount();i++){
10 //缩小选中图片旁边的图片
11 ImageView local_imageview = (ImageView)parent.getChildAt(i);
12 if(local_imageview!=imageview){
13 local_imageview.setLayoutParams(new Gallery.LayoutParams(520/4, 318/4));
14 local_imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
15 }
16 }
17 }
18
19 public void onNothingSelected(AdapterView<?> parent)
20 {
21 }
22
23 public class ImageAdapter extends BaseAdapter
24 {
25 int mGalleryItemBackground;
26 private Context mContext;
27
28 public ImageAdapter(Context context)
29 {
30 mContext = context;
31 // TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
32 // mGalleryItemBackground = typedArray.getResourceId(
33 // R.styleable.Gallery_android_galleryItemBackground, 0);
34 }
35
36 public int getCount()
37 {
38 return product_image_list.size();
39 }
40
41 public Object getItem(int position)
42 {
43 return position;
44 }
45
46 public long getItemId(int position)
47 {
48 return position;
49 }
50
51 public View getView(int position, View convertView, ViewGroup parent)
52 {
53 ImageView imageView = new ImageView(mContext);
54 imageView.setLayoutParams(new Gallery.LayoutParams(520/4, 318/4));//默认都是大图
55 imageView.setImageDrawable((Drawable) product_image_list.get(position));
56 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
57 return imageView;
58 }
59 }
2 long id) {
3 // 选中Gallery中某个图像时,放大显示该图像
4 ImageView imageview = (ImageView)view;
5 ((ImageView) view).setImageDrawable((Drawable) product_image_list.get(position));
6 view.setLayoutParams(new Gallery.LayoutParams(520 / 2, 318 / 2));
7 title.setText((String)product_title.get(position));
8 info.setText((String)product_info.get(position));
9 for(int i=0; i<parent.getChildCount();i++){
10 //缩小选中图片旁边的图片
11 ImageView local_imageview = (ImageView)parent.getChildAt(i);
12 if(local_imageview!=imageview){
13 local_imageview.setLayoutParams(new Gallery.LayoutParams(520/4, 318/4));
14 local_imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
15 }
16 }
17 }
18
19 public void onNothingSelected(AdapterView<?> parent)
20 {
21 }
22
23 public class ImageAdapter extends BaseAdapter
24 {
25 int mGalleryItemBackground;
26 private Context mContext;
27
28 public ImageAdapter(Context context)
29 {
30 mContext = context;
31 // TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
32 // mGalleryItemBackground = typedArray.getResourceId(
33 // R.styleable.Gallery_android_galleryItemBackground, 0);
34 }
35
36 public int getCount()
37 {
38 return product_image_list.size();
39 }
40
41 public Object getItem(int position)
42 {
43 return position;
44 }
45
46 public long getItemId(int position)
47 {
48 return position;
49 }
50
51 public View getView(int position, View convertView, ViewGroup parent)
52 {
53 ImageView imageView = new ImageView(mContext);
54 imageView.setLayoutParams(new Gallery.LayoutParams(520/4, 318/4));//默认都是大图
55 imageView.setImageDrawable((Drawable) product_image_list.get(position));
56 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
57 return imageView;
58 }
59 }