1. 程式人生 > >Android特效第三篇:自定義Gallery實戰(仿網易) .

Android特效第三篇:自定義Gallery實戰(仿網易) .

Android系統提供了一個Gallery畫廊控制元件,在專案很多時候都會用到Gallery,比如新浪首頁的廣告,網易看客戶端首頁等隨處可見,今天我自己定義了一個仿網易的Gallery與大家共享。

     首先請看效果圖:

                                         

程式碼:

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

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author jefry
 *
 */
public class MyGallery extends FrameLayout {
	List<Showing> showings = new ArrayList<Showing>();
	private static final double GALLERY_IMAGE_HORIZONTAL_RATIO = 1.0;
	private static final double GALLERY_IMAGE_VERTICAL_RATIO = 1.0;
	private static final int GALLERY_SPACING = 2;
    private Context mContext;
    private PromotionImages promotionImages;
    private LinearLayout mBottomLayout;
    private TextView tips;
    private int mPcount;
    private ImageView[] icons;
	public MyGallery(Context context) {
		super(context);
	}

	public MyGallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		promotionImages = new PromotionImages(context);
		LayoutParams layoutParams = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.FILL_PARENT);
		layoutParams.bottomMargin = 20;
		this.addView(promotionImages, layoutParams);
		LayoutInflater inflater = ((Activity) context).getLayoutInflater();
		View indicator = inflater.inflate(R.layout.promotion_hint,null);
		this.addView(indicator);
		//
		mBottomLayout = (LinearLayout) indicator.findViewById(R.id.promotion_index_layout);
	    tips	  = (TextView) indicator.findViewById(R.id.promotion_tip);
	    initBottomIcons();
	}
	
	
	

	class PromotionImages extends Gallery {
       
		PromotionImagesAdapter promotionAdapter;
		public PromotionImages(Context context) {
			super(context);
			this.setSpacing(GALLERY_SPACING);
			promotionAdapter = new PromotionImagesAdapter(context);
			this.setAdapter(promotionAdapter);
			
			this.setOnItemSelectedListener(new OnItemSelectedListener() {
				
				public void onItemSelected(AdapterView<?> parent, View view,
						int position, long id) {
					 update(position);
				}

		
				public void onNothingSelected(AdapterView<?> parent) {
					
				}
			});
		}
		
		
		private final void update(int selected) {
		    tips.setText(showings.get(selected).getText());
			for (int i = 0; i < mPcount; i++) {
				   if (selected == i) {
						icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_focus));
					} else {
						icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_default));
				    }
			  }
		    }
		}
	
	
	   private final void initBottomIcons() {
		 mPcount = showings.size();
		 icons = new ImageView[mPcount];
		 mBottomLayout.removeAllViews();
		 mBottomLayout.setWeightSum(mPcount);
		//
		for (int i = 0; i < mPcount; i++) {
			icons[i] = new ImageView(mContext);
			if(i == 0) icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_focus));
			else icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_default));
			mBottomLayout.addView(icons[i]);
		}
	}

		private final class PromotionImagesAdapter extends BaseAdapter {
			public PromotionImagesAdapter(Context context) {
				Resources res = getResources();

				Bitmap bmp = BitmapFactory.decodeResource(res,
						R.drawable.test2);
				Showing show1 = new Showing();
				show1.setBitmap(bmp);
				show1.setText("jefry_1");

				Bitmap bmp1 = BitmapFactory.decodeResource(res,
						R.drawable.test1);
				Showing show2 = new Showing();
				show2.setBitmap(bmp1);
				show2.setText("jefry_2");
                
				Bitmap bmp3 = BitmapFactory.decodeResource(res,
						R.drawable.test3);
				Showing show3 = new Showing();
				show3.setBitmap(bmp3);
				show3.setText("jefry_3");

				showings.add(show1);
				showings.add(show2);
				showings.add(show3);

			}

			
			public int getCount() {
				return showings.size();
			}

			
			public Object getItem(int position) {
				return position;
			}

			
			public long getItemId(int position) {
				return position;
			}

			
			public View getView(final int position, final View convertView,
					ViewGroup parent) {
				//System.out.println("showings = " + showings.size());
                //System.out.println("position=" + position);
				final Showing promotion = showings.get(position);
				ImageView promotionImage = (ImageView) convertView; 
				if (promotionImage == null) {
					promotionImage = new ImageView(mContext);
					System.out.println("postion=" + position + ",promotion.getBitmap()1 = "+ promotion.getBitmap());
				}
				int width = (int) (MyGallery.this.getWidth() * GALLERY_IMAGE_HORIZONTAL_RATIO);
				int height = (int) (MyGallery.this.getHeight() * GALLERY_IMAGE_VERTICAL_RATIO);
				promotionImage.setLayoutParams(new Gallery.LayoutParams(width,height));
				promotionImage.setScaleType(ImageView.ScaleType.CENTER);
				promotionImage.setScaleType(ImageView.ScaleType.FIT_XY);
				System.out.println("postion=" + position + ",promotion.getBitmap() = "+ promotion.getBitmap());
				promotionImage.setImageBitmap(promotion.getBitmap());
				return promotionImage;
			}
		}
	}
原始碼下載http://download.csdn.net/detail/jefry_xdz/4521377