用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     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM