仿京东跑马灯

  1. //自定义view  _____VerticalMarqueeView
  2. public class VerticalMarqueeView extends View {  
  3.     public static final int DURATION_SCROLL = 2000;  
  4.     public static final int DURATION_ANIMATOR = 1000;  
  5.     private int color = Color.BLACK;  
  6.     private int textSize = 40;  
  7.     private String[] datas = null;  
  8.     private int width;  
  9.     private int height;  
  10.     private int centerX;  
  11.     private int centerY;  
  12.   
  13.     private List<TextBlock> blocks = new ArrayList<TextBlock>(2);  
  14.     private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  15.     private Handler handler = new Handler();  
  16.     private boolean isStopScroll = false;  
  17.   
  18.     public VerticalMarqueeView(Context context, AttributeSet attrs, int defStyleAttr){  
  19.         super(context, attrs, defStyleAttr);  
  20.     }  
  21.   
  22.     public VerticalMarqueeView(Context context, AttributeSet attrs){  
  23.         super(context, attrs);  
  24.     }  
  25.   
  26.     public VerticalMarqueeView(Context context){  
  27.         super(context);  
  28.     }  
  29.   
  30.     public VerticalMarqueeView color(int color){  
  31.         this.color = color;  
  32.         return this;  
  33.     }  
  34.   
  35.     public VerticalMarqueeView textSize(int textSize){  
  36.         this.textSize = textSize;  
  37.         return this;  
  38.     }  
  39.   
  40.     public VerticalMarqueeView datas(String[] datas){  
  41.         this.datas = datas;  
  42.         return this;  
  43.     }  
  44.   
  45.     public void commit(){  
  46.         if(this.datas == null || datas.length == 0){  
  47.             Log.e("VerticalMarqueeView""the datas's length is illegal");  
  48.             throw new IllegalStateException("may be not invoke the method named datas(String[])");  
  49.         }  
  50.         paint.setColor(color);  
  51.         paint.setTextSize(textSize);  
  52.     }  
  53.   
  54.     @Override  
  55.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){  
  56.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  57.         if(this.datas == null || this.datas.length == 0){  
  58.             Log.e("VerticalMarqueeView""the datas's length is illegal");  
  59.             return;  
  60.         }  
  61.         width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();  
  62.         height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();  
  63.         centerX = width / 2;  
  64.         centerY = height / 2;  
  65.         blocks.clear();  
  66.         //添加显示区域的文字块  
  67.         TextBlock block1 = new TextBlock(width, height, paint);  
  68.         block1.reset(datas[0], centerX, centerY, 0);  
  69.         blocks.add(block1);  
  70.         if(datas.length > 1){  
  71.             TextBlock block2 = new TextBlock(width, height, paint);  
  72.             block2.reset(datas[1], centerX, centerY + height, 1);  
  73.             blocks.add(block2);  
  74.         }  
  75.     }  
  76.   
  77.     @Override  
  78.     protected void onDraw(Canvas canvas){  
  79.         for(int i = 0; i < blocks.size(); i++){  
  80.             blocks.get(i).draw(canvas);  
  81.         }  
  82.     }  
  83.   
  84.     public void startScroll(){  
  85.         isStopScroll = false;  
  86.         if(datas == null || datas.length == 0 || datas.length == 1){  
  87.             Log.e("VerticalMarqueeView""the datas's length is illegal");  
  88.             return;  
  89.         }  
  90.         if(!isStopScroll){  
  91.             handler.postDelayed(new Runnable(){  
  92.   
  93.                 @Override  
  94.                 public void run(){  
  95.                     scroll();  
  96.                     if(!isStopScroll){  
  97.                         handler.postDelayed(this, DURATION_SCROLL);  
  98.                     }  
  99.                 }  
  100.             }, DURATION_SCROLL);  
  101.         }  
  102.     }  
  103.   
  104.     public void stopScroll(){  
  105.         this.isStopScroll = true;  
  106.     }  
  107.   
  108.     private void scroll(){  
  109.         ValueAnimator animator = ValueAnimator.ofPropertyValuesHolder(PropertyValuesHolder.ofInt("scrollY", centerY, centerY - height));  
  110.         animator.setDuration(DURATION_ANIMATOR);  
  111.         animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){  
  112.   
  113.             @Override  
  114.             public void onAnimationUpdate(ValueAnimator animation){  
  115.                 int scrollY = (int)animation.getAnimatedValue("scrollY");  
  116.                 blocks.get(0).reset(scrollY);  
  117.                 blocks.get(1).reset(scrollY + height);  
  118.                 invalidate();  
  119.             }  
  120.         });  
  121.         animator.addListener(new Animator.AnimatorListener(){  
  122.   
  123.             @Override  
  124.             public void onAnimationStart(Animator animation){  
  125.             }  
  126.   
  127.             @Override  
  128.             public void onAnimationRepeat(Animator animation){  
  129.             }  
  130.   
  131.             @Override  
  132.             public void onAnimationEnd(Animator animation){  
  133.                 //移除第一块  
  134.                 int position = blocks.get(1).getPosition();  
  135.                 TextBlock textBlock = blocks.remove(0);  
  136.                 //最后一个  
  137.                 if(position == datas.length - 1){  
  138.                     position = 0;  
  139.                 }else{  
  140.                     position ++;  
  141.                 }  
  142.                 textBlock.reset(datas[position], centerY + height, position);  
  143.                 blocks.add(textBlock);  
  144.                 invalidate();  
  145.             }  
  146.   
  147.             @Override  
  148.             public void onAnimationCancel(Animator animation){  
  149.             }  
  150.         });  
  151.         animator.start();  
  152.     }  
  153.   
  154.     public int getCurrentPosition(){  
  155.         if(datas == null || datas.length == 0){  
  156.             return -1;  
  157.         }  
  158.         if(datas.length == 1 && blocks.size() == 1){  
  159.             return 0;  
  160.         }  
  161.         return blocks.get(0).getPosition();  
  162.     }  
  163.   
  164.     public class TextBlock {  
  165.         private int width;  
  166.         private int height;  
  167.         private String text;  
  168.         private int drawX;  
  169.         private int drawY;  
  170.         private Paint paint;  
  171.         private int position;  
  172.   
  173.         public TextBlock(int width, int height, Paint paint){  
  174.             this.width = width;  
  175.             this.height = height;  
  176.             this.paint = paint;  
  177.         }  
  178.   
  179.         public void reset(int centerY){  
  180.             reset(text, centerX, centerY, position);  
  181.         }  
  182.   
  183.         public void reset(String text, int centerY){  
  184.             reset(text, centerX, centerY, position);  
  185.         }  
  186.   
  187.         public void reset(String text, int centerY, int position){  
  188.             reset(text, centerX, centerY, position);  
  189.         }  
  190.   
  191.         public void reset(String text, int centerX, int centerY, int position){  
  192.             this.text = text;  
  193.             this.position = position;  
  194.             int measureWidth = (int)paint.measureText(text);  
  195.             drawX = (width - measureWidth) / 2;  
  196.             Paint.FontMetrics metrics = paint.getFontMetrics();  
  197.             drawY = (int)(centerY + (metrics.bottom - metrics.top) / 2 - metrics.bottom);  
  198.         }  
  199.   
  200.         public int getPosition(){  
  201.             return position;  
  202.         }  
  203.   
  204.         public void draw(Canvas canvas){  
  205.             canvas.drawText(text, drawX, drawY, paint);  
  206.         }  
  207.     }  
  208.   
  209. }</span>  
//跑马灯布局
[java]  view plain  copy
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:orientation="horizontal"  
  5.     android:layout_marginLeft="10dp"  
  6.     android:layout_marginTop="10dp"  
  7.     android:layout_marginRight="10dp"  
  8.     android:background="@drawable/home_title_bar_search_corner_bg1"  
  9.     android:layout_height="30dp">  
  10.   
  11.     <TextView  
  12.         android:textSize="20dp"  
  13.         android:text="京东快报:"  
  14.         android:layout_marginBottom="5dp"  
  15.         android:layout_marginLeft="10dp"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content" />  
  18.       
  19.     <com.bwie.jd_zj.Zidingyi.VerticalMarqueeView  
  20.         android:layout_weight="1"  
  21.         android:id="@+id/vv"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="30dp" />  
  24.   
  25.       <View  
  26.           android:layout_marginTop="6dp"  
  27.           android:background="#9999"  
  28.           android:layout_marginRight="17dp"  
  29.           android:layout_width="2dp"  
  30.           android:layout_height="15dp"/>  
  31.      <TextView  
  32.          android:layout_marginRight="2dp"  
  33.         android:layout_marginBottom="5dp"  
  34.         android:textSize="18dp"  
  35.         android:layout_gravity="right"  
  36.         android:text="更多"  
  37.         android:layout_width="wrap_content"  
  38.         android:layout_height="wrap_content" />  
  39. </LinearLayout></span>  
//背景布局   home_title_bar_search_corner_bg1
[java]  view plain  copy
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle">  
  4.   
  5.     <corners android:radius="8dp"/>  
  6.     <solid android:color="@color/white"/>  
  7.     </shape></span>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值