Android自定义组件系列【16】——最帅气的自动滚动广告条

前一段时间要实现一个滚动的广告条,参考了一下网上许多实现,发现实现都很麻烦,所以我决定自己使用ViewFlipper来实现一个,在此将代码贴出来,与大家共享。

转载请说明出处:http://blog.csdn.net/dawanganban

  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5.   
  6. import com.guozha.buy.R;  
  7. import com.guozha.buy.util.DimenUtil;  
  8.   
  9. import android.content.Context;  
  10. import android.graphics.Bitmap;  
  11. import android.graphics.BitmapFactory;  
  12. import android.os.Handler;  
  13. import android.util.AttributeSet;  
  14. import android.view.Gravity;  
  15. import android.view.MotionEvent;  
  16. import android.view.View;  
  17. import android.view.ViewConfiguration;  
  18. import android.widget.FrameLayout;  
  19. import android.widget.ImageView;  
  20. import android.widget.ImageView.ScaleType;  
  21. import android.widget.LinearLayout;  
  22. import android.widget.ViewFlipper;  
  23.   
  24. /** 
  25.  * 自定播放图片View 
  26.  * @author lixiaoqiang 
  27.  *  
  28.  * CSDN博客:http://blog.csdn.net/dawanganban 
  29.  * 
  30.  */  
  31. public class AutoViewFlipper extends FrameLayout{  
  32.     private static final int SLOP_DELAY_TIME = 3000//滑动等待时间,单位ms  
  33.     private ViewFlipper mViewFlipper;       //滑动的视图  
  34.     private View mPointBar;                 //指示点显示条  
  35.     private int mItemCount;                 //条目数  
  36.     private int mCurrentItem;               //当前的条目  
  37.     private int mTouchSlop;                 //有效最短滑动距离  
  38.     private Context context;  
  39.     private List<ImageView> points = new ArrayList<ImageView>();  
  40.       
  41.     private TimerTask mTimerTask;  
  42.       
  43.     private static final int HANDLER_SLOP_LEFT = 0x0001;  
  44.     private Handler hander = new Handler(){  
  45.         public void handleMessage(android.os.Message msg) {  
  46.             switch (msg.what) {  
  47.             case HANDLER_SLOP_LEFT:  
  48.                 slopToLeft();  
  49.                 break;  
  50.             }  
  51.         };  
  52.     };  
  53.   
  54.     public AutoViewFlipper(Context context, AttributeSet attrs) {  
  55.         super(context, attrs);  
  56.         this.context = context;  
  57.           
  58.         mTouchSlop = ViewConfiguration.getTouchSlop();  
  59.           
  60.         addChild(context);  
  61.           
  62.         startAutoPlay();  
  63.     }  
  64.       
  65.     /** 
  66.      * 停止自动播放 
  67.      */  
  68.     public void stopAutoPlay(){  
  69.         if(mTimerTask == nullreturn;  
  70.         mTimerTask.cancel();  
  71.         mTimerTask = null;  
  72.     }  
  73.       
  74.     /** 
  75.      * 开始自动播放 
  76.      */  
  77.     public void startAutoPlay(){  
  78.         if(mTimerTask != nullreturn;  
  79.         mTimerTask = new TimerTask() {  
  80.             @Override  
  81.             public void run() {  
  82.                 hander.sendEmptyMessage(HANDLER_SLOP_LEFT);  
  83.             }  
  84.         };  
  85.         new Timer().scheduleAtFixedRate(mTimerTask, SLOP_DELAY_TIME, SLOP_DELAY_TIME);  
  86.     }  
  87.       
  88.     /** 
  89.      * 添加子视图 
  90.      * @param context 
  91.      */  
  92.     private void addChild(Context context){  
  93.           
  94.         mViewFlipper = getViewFlipper(context);  
  95.         this.addView(mViewFlipper);  
  96.           
  97.         mPointBar = getPointBar(context);  
  98.         this.addView(mPointBar);  
  99.     }  
  100.       
  101.     /** 
  102.      * 获取ViewFlipper 
  103.      * @param context 
  104.      * @return 
  105.      */  
  106.     private ViewFlipper getViewFlipper(Context context){  
  107.         ViewFlipper viewFlipper = new ViewFlipper(context);  
  108.         addImageViews(context, viewFlipper);  
  109.         return viewFlipper;  
  110.     }  
  111.       
  112.     /** 
  113.      * 获取指示点显示条 
  114.      * @param context 
  115.      * @return 
  116.      */  
  117.     private View getPointBar(Context context){  
  118.         LinearLayout.LayoutParams pointBarParams = new LinearLayout.LayoutParams(  
  119.                 LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);  
  120.         pointBarParams.gravity = Gravity.RIGHT | Gravity.BOTTOM;  
  121.         LinearLayout pointBar = new LinearLayout(context);  
  122.           
  123.         pointBar.setOrientation(LinearLayout.HORIZONTAL);  
  124.         pointBar.setLayoutParams(pointBarParams);  
  125.         pointBar.setGravity(Gravity.RIGHT | Gravity.BOTTOM);  
  126.           
  127.         addPoints(context, pointBar);  
  128.         return pointBar;  
  129.     }  
  130.       
  131.     /** 
  132.      * 添加小圆点 
  133.      * @param context 
  134.      * @param pointBar 
  135.      */  
  136.     private void addPoints(Context context, LinearLayout pointBar){  
  137.         LinearLayout.LayoutParams pointViewParams = new LinearLayout.LayoutParams(  
  138.                 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
  139.         pointViewParams.setMargins(DimenUtil.dp2px(context, 8), 0,  
  140.                 DimenUtil.dp2px(context, 8), DimenUtil.dp2px(context, 8));  
  141.           
  142.         ImageView pointView;  
  143.         for(int i = 0; i < mItemCount; i++){  
  144.             pointView = new ImageView(context);  
  145.             pointView.setScaleType(ScaleType.CENTER_INSIDE);  
  146.             pointView.setLayoutParams(pointViewParams);  
  147.             points.add(pointView);  
  148.             pointBar.addView(pointView);  
  149.         }  
  150.         setPointColorByCurrentItem();  
  151.     }  
  152.       
  153.     /** 
  154.      * 根据当前选中项来设置圆点 
  155.      */  
  156.     private void setPointColorByCurrentItem(){  
  157.         mCurrentItem = (Integer)mViewFlipper.getCurrentView().getTag();  
  158.         Bitmap grayPointBitmap = getGrayPointBitmap(context);  
  159.         Bitmap lightPointBitmap = getLightPointBitmap(context);  
  160.         ImageView imageView;  
  161.         for(int i = 0; i < points.size(); i++){  
  162.             imageView = points.get(i);  
  163.             if(mCurrentItem == i){  
  164.                 imageView.setImageBitmap(lightPointBitmap);  
  165.             }else{  
  166.                 imageView.setImageBitmap(grayPointBitmap);  
  167.             }  
  168.   
  169.         }  
  170.     }  
  171.       
  172.     /** 
  173.      * 添加图片资源 
  174.      * @param context 
  175.      * @param viewFlipper 
  176.      */  
  177.     private void addImageViews(Context context, ViewFlipper viewFlipper){  
  178.         if(viewFlipper == nullreturn;  
  179.         List<Bitmap> bitmaps = getBitmaps();  
  180.           
  181.         if(bitmaps == nullreturn;  
  182.         LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams(  
  183.                 LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);  
  184.           
  185.         ImageView imageView;  
  186.         mItemCount = bitmaps.size();  
  187.         for(int i = 0; i < mItemCount; i++){  
  188.             imageView = new ImageView(context);  
  189.             imageView.setImageBitmap(bitmaps.get(i));  
  190.             imageView.setScaleType(ScaleType.CENTER_CROP);  
  191.             imageView.setLayoutParams(imageViewParams);  
  192.             imageView.setTag(i);  
  193.             viewFlipper.addView(imageView);  
  194.         }     
  195.     }  
  196.       
  197.     /** 
  198.      * 获取图片资源 
  199.      * @return 
  200.      */  
  201.     private List<Bitmap> getBitmaps(){  
  202.         //TODO 从网络获取图片  
  203.         List<Bitmap> bitmaps = new ArrayList<Bitmap>();   
  204.           
  205.         bitmaps.add(BitmapFactory.decodeResource(  
  206.                 getResources(), R.drawable.main_page_scroll_image1));  
  207.         bitmaps.add(BitmapFactory.decodeResource(  
  208.                 getResources(), R.drawable.main_page_scroll_image2));  
  209.         bitmaps.add(BitmapFactory.decodeResource(  
  210.                 getResources(), R.drawable.main_page_scroll_image3));  
  211.           
  212.         return bitmaps;  
  213.     }  
  214.       
  215.     /** 
  216.      * 获取选择圆点图片 
  217.      * @param context 
  218.      * @return 
  219.      */  
  220.     private Bitmap getLightPointBitmap(Context context){  
  221.         return BitmapFactory.decodeResource(  
  222.                 getResources(), R.drawable.main_page_scorll_point_selected);  
  223.     }  
  224.       
  225.     /** 
  226.      * 获取灰色圆点图片 
  227.      * @param context 
  228.      * @return 
  229.      */  
  230.     private Bitmap getGrayPointBitmap(Context context){  
  231.         return BitmapFactory.decodeResource(  
  232.                 getResources(), R.drawable.main_page_scroll_point_unselected);  
  233.     }  
  234.       
  235.       
  236.     private float mDownX;  
  237.     @Override  
  238.     public boolean onTouchEvent(MotionEvent event) {  
  239.         float eventX = event.getX();  
  240.           
  241.         switch (event.getAction()) {  
  242.         case MotionEvent.ACTION_DOWN:  
  243.             mDownX = eventX;  
  244.             break;  
  245.         case MotionEvent.ACTION_UP:  
  246.               
  247.             float gap = eventX - mDownX;  
  248.             if(Math.abs(gap) > mTouchSlop){  
  249.                 if(gap > 0){  
  250.                     //向右滑动  
  251.                     slopToRight();  
  252.                 }else{  
  253.                     //向左滑动  
  254.                     slopToLeft();  
  255.                 }  
  256.             }  
  257.             break;  
  258.         }  
  259.           
  260.         return true;  
  261.     }  
  262.       
  263.     /** 
  264.      * 向右滑动 
  265.      */  
  266.     private void slopToRight(){  
  267.         mViewFlipper.setInAnimation(context, R.anim.slide_in_left);  
  268.         mViewFlipper.setOutAnimation(context, R.anim.slide_out_right);  
  269.         mViewFlipper.showPrevious();  
  270.         setPointColorByCurrentItem();  
  271.     }  
  272.       
  273.     /** 
  274.      * 向左滑动 
  275.      */  
  276.     private void slopToLeft(){  
  277.         mViewFlipper.setInAnimation(context, R.anim.slide_in_right);  
  278.         mViewFlipper.setOutAnimation(context, R.anim.slide_out_left);  
  279.         mViewFlipper.showNext();  
  280.         setPointColorByCurrentItem();  
  281.     }  
  282.       
  283.     private OnSlopTouchListener mOnSlopTouchListener;  
  284.       
  285.     /** 
  286.      * 监听滑动等事件 
  287.      * @author Administrator 
  288.      * 
  289.      */  
  290.     interface OnSlopTouchListener{  
  291.           
  292.         /** 
  293.          * touch事件响应 
  294.          */  
  295.         public void onTouchedView();  
  296.     }  
  297.       
  298.     /** 
  299.      * 设置滑动等事件的监听 
  300.      * @param onSlopTouchListener 
  301.      */  
  302.     public void setOnSlopTouchListener(OnSlopTouchListener onSlopTouchListener){  
  303.         this.mOnSlopTouchListener = onSlopTouchListener;  
  304.     }  
  305. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值