Android特效第三篇:自定义Gallery实战(仿网易) .

Android系统提供了一个Gallery画廊控件,在项目很多时候都会用到Gallery,比如新浪首页的广告,网易看客户端首页等随处可见,今天我自己定义了一个仿网易的Gallery与大家共享。

     首先请看效果图:

                                         Android特效第三篇:自定义Gallery实战(仿网易) ._第1张图片


代码:

[java] view plain copy print ?
  1. package com.jefry.gallery;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.content.Context;  
  8. import android.content.res.Resources;  
  9. import android.graphics.Bitmap;  
  10. import android.graphics.BitmapFactory;  
  11. import android.util.AttributeSet;  
  12. import android.view.LayoutInflater;  
  13. import android.view.View;  
  14. import android.view.ViewGroup;  
  15. import android.widget.AdapterView;  
  16. import android.widget.BaseAdapter;  
  17. import android.widget.FrameLayout;  
  18. import android.widget.Gallery;  
  19. import android.widget.ImageView;  
  20. import android.widget.LinearLayout;  
  21. import android.widget.TextView;  
  22.   
  23. /** 
  24.  * @author jefry 
  25.  * 
  26.  */  
  27. public class MyGallery extends FrameLayout {  
  28.     List<Showing> showings = new ArrayList<Showing>();  
  29.     private static final double GALLERY_IMAGE_HORIZONTAL_RATIO = 1.0;  
  30.     private static final double GALLERY_IMAGE_VERTICAL_RATIO = 1.0;  
  31.     private static final int GALLERY_SPACING = 2;  
  32.     private Context mContext;  
  33.     private PromotionImages promotionImages;  
  34.     private LinearLayout mBottomLayout;  
  35.     private TextView tips;  
  36.     private int mPcount;  
  37.     private ImageView[] icons;  
  38.     public MyGallery(Context context) {  
  39.         super(context);  
  40.     }  
  41.   
  42.     public MyGallery(Context context, AttributeSet attrs) {  
  43.         super(context, attrs);  
  44.         mContext = context;  
  45.         promotionImages = new PromotionImages(context);  
  46.         LayoutParams layoutParams = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.FILL_PARENT);  
  47.         layoutParams.bottomMargin = 20;  
  48.         this.addView(promotionImages, layoutParams);  
  49.         LayoutInflater inflater = ((Activity) context).getLayoutInflater();  
  50.         View indicator = inflater.inflate(R.layout.promotion_hint,null);  
  51.         this.addView(indicator);  
  52.         //   
  53.         mBottomLayout = (LinearLayout) indicator.findViewById(R.id.promotion_index_layout);  
  54.         tips      = (TextView) indicator.findViewById(R.id.promotion_tip);  
  55.         initBottomIcons();  
  56.     }  
  57.       
  58.       
  59.       
  60.   
  61.     class PromotionImages extends Gallery {  
  62.          
  63.         PromotionImagesAdapter promotionAdapter;  
  64.         public PromotionImages(Context context) {  
  65.             super(context);  
  66.             this.setSpacing(GALLERY_SPACING);  
  67.             promotionAdapter = new PromotionImagesAdapter(context);  
  68.             this.setAdapter(promotionAdapter);  
  69.               
  70.             this.setOnItemSelectedListener(new OnItemSelectedListener() {  
  71.                   
  72.                 public void onItemSelected(AdapterView<?> parent, View view,  
  73.                         int position, long id) {  
  74.                      update(position);  
  75.                 }  
  76.   
  77.           
  78.                 public void onNothingSelected(AdapterView<?> parent) {  
  79.                       
  80.                 }  
  81.             });  
  82.         }  
  83.           
  84.           
  85.         private final void update(int selected) {  
  86.             tips.setText(showings.get(selected).getText());  
  87.             for (int i = 0; i < mPcount; i++) {  
  88.                    if (selected == i) {  
  89.                         icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_focus));  
  90.                     } else {  
  91.                         icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_default));  
  92.                     }  
  93.               }  
  94.             }  
  95.         }  
  96.       
  97.       
  98.        private final void initBottomIcons() {  
  99.          mPcount = showings.size();  
  100.          icons = new ImageView[mPcount];  
  101.          mBottomLayout.removeAllViews();  
  102.          mBottomLayout.setWeightSum(mPcount);  
  103.         //   
  104.         for (int i = 0; i < mPcount; i++) {  
  105.             icons[i] = new ImageView(mContext);  
  106.             if(i == 0) icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_focus));  
  107.             else icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_default));  
  108.             mBottomLayout.addView(icons[i]);  
  109.         }  
  110.     }  
  111.   
  112.         private final class PromotionImagesAdapter extends BaseAdapter {  
  113.             public PromotionImagesAdapter(Context context) {  
  114.                 Resources res = getResources();  
  115.   
  116.                 Bitmap bmp = BitmapFactory.decodeResource(res,  
  117.                         R.drawable.test2);  
  118.                 Showing show1 = new Showing();  
  119.                 show1.setBitmap(bmp);  
  120.                 show1.setText("jefry_1");  
  121.   
  122.                 Bitmap bmp1 = BitmapFactory.decodeResource(res,  
  123.                         R.drawable.test1);  
  124.                 Showing show2 = new Showing();  
  125.                 show2.setBitmap(bmp1);  
  126.                 show2.setText("jefry_2");  
  127.                   
  128.                 Bitmap bmp3 = BitmapFactory.decodeResource(res,  
  129.                         R.drawable.test3);  
  130.                 Showing show3 = new Showing();  
  131.                 show3.setBitmap(bmp3);  
  132.                 show3.setText("jefry_3");  
  133.   
  134.                 showings.add(show1);  
  135.                 showings.add(show2);  
  136.                 showings.add(show3);  
  137.   
  138.             }  
  139.   
  140.               
  141.             public int getCount() {  
  142.                 return showings.size();  
  143.             }  
  144.   
  145.               
  146.             public Object getItem(int position) {  
  147.                 return position;  
  148.             }  
  149.   
  150.               
  151.             public long getItemId(int position) {  
  152.                 return position;  
  153.             }  
  154.   
  155.               
  156.             public View getView(final int position, final View convertView,  
  157.                     ViewGroup parent) {  
  158.                 //System.out.println("showings = " + showings.size());   
  159.                 //System.out.println("position=" + position);   
  160.                 final Showing promotion = showings.get(position);  
  161.                 ImageView promotionImage = (ImageView) convertView;   
  162.                 if (promotionImage == null) {  
  163.                     promotionImage = new ImageView(mContext);  
  164.                     System.out.println("postion=" + position + ",promotion.getBitmap()1 = "+ promotion.getBitmap());  
  165.                 }  
  166.                 int width = (int) (MyGallery.this.getWidth() * GALLERY_IMAGE_HORIZONTAL_RATIO);  
  167.                 int height = (int) (MyGallery.this.getHeight() * GALLERY_IMAGE_VERTICAL_RATIO);  
  168.                 promotionImage.setLayoutParams(new Gallery.LayoutParams(width,height));  
  169.                 promotionImage.setScaleType(ImageView.ScaleType.CENTER);  
  170.                 promotionImage.setScaleType(ImageView.ScaleType.FIT_XY);  
  171.                 System.out.println("postion=" + position + ",promotion.getBitmap() = "+ promotion.getBitmap());  
  172.                 promotionImage.setImageBitmap(promotion.getBitmap());  
  173.                 return promotionImage;  
  174.             }  
  175.         }  
  176.     }  


源码下载http://download.csdn.net/detail/jefry_xdz/4521377

你可能感兴趣的:(android,image,null,Class,网易,icons)