仿京东跑马灯

//自定义view  _____VerticalMarqueeView

public class VerticalMarqueeView extends View {
    public static final int DURATION_SCROLL = 2000;
    public static final int DURATION_ANIMATOR = 1000;
    private int color = Color.BLACK;
    private int textSize = 40;
    private String[] datas = null;
    private int width;
    private int height;
    private int centerX;
    private int centerY;

    private List<TextBlock> blocks = new ArrayList<TextBlock>(2);
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Handler handler = new Handler();
    private boolean isStopScroll = false;

    public VerticalMarqueeView(Context context, AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
    }

    public VerticalMarqueeView(Context context, AttributeSet attrs){
        super(context, attrs);
    }

    public VerticalMarqueeView(Context context){
        super(context);
    }

    public VerticalMarqueeView color(int color){
        this.color = color;
        return this;
    }

    public VerticalMarqueeView textSize(int textSize){
        this.textSize = textSize;
        return this;
    }

    public VerticalMarqueeView datas(String[] datas){
        this.datas = datas;
        return this;
    }

    public void commit(){
        if(this.datas == null || datas.length == 0){
            Log.e("VerticalMarqueeView", "the datas's length is illegal");
            throw new IllegalStateException("may be not invoke the method named datas(String[])");
        }
        paint.setColor(color);
        paint.setTextSize(textSize);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if(this.datas == null || this.datas.length == 0){
            Log.e("VerticalMarqueeView", "the datas's length is illegal");
            return;
        }
        width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
        height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom();
        centerX = width / 2;
        centerY = height / 2;
        blocks.clear();
        //添加显示区域的文字块
        TextBlock block1 = new TextBlock(width, height, paint);
        block1.reset(datas[0], centerX, centerY, 0);
        blocks.add(block1);
        if(datas.length > 1){
            TextBlock block2 = new TextBlock(width, height, paint);
            block2.reset(datas[1], centerX, centerY + height, 1);
            blocks.add(block2);
        }
    }

    @Override
    protected void onDraw(Canvas canvas){
        for(int i = 0; i < blocks.size(); i++){
            blocks.get(i).draw(canvas);
        }
    }

    public void startScroll(){
        isStopScroll = false;
        if(datas == null || datas.length == 0 || datas.length == 1){
            Log.e("VerticalMarqueeView", "the datas's length is illegal");
            return;
        }
        if(!isStopScroll){
            handler.postDelayed(new Runnable(){

                @Override
                public void run(){
                    scroll();
                    if(!isStopScroll){
                        handler.postDelayed(this, DURATION_SCROLL);
                    }
                }
            }, DURATION_SCROLL);
        }
    }

    public void stopScroll(){
        this.isStopScroll = true;
    }

    private void scroll(){
        ValueAnimator animator = ValueAnimator.ofPropertyValuesHolder(PropertyValuesHolder.ofInt("scrollY", centerY, centerY - height));
        animator.setDuration(DURATION_ANIMATOR);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){

            @Override
            public void onAnimationUpdate(ValueAnimator animation){
                int scrollY = (int)animation.getAnimatedValue("scrollY");
                blocks.get(0).reset(scrollY);
                blocks.get(1).reset(scrollY + height);
                invalidate();
            }
        });
        animator.addListener(new Animator.AnimatorListener(){

            @Override
            public void onAnimationStart(Animator animation){
            }

            @Override
            public void onAnimationRepeat(Animator animation){
            }

            @Override
            public void onAnimationEnd(Animator animation){
                //移除第一块
                int position = blocks.get(1).getPosition();
                TextBlock textBlock = blocks.remove(0);
                //最后一个
                if(position == datas.length - 1){
                    position = 0;
                }else{
                    position ++;
                }
                textBlock.reset(datas[position], centerY + height, position);
                blocks.add(textBlock);
                invalidate();
            }

            @Override
            public void onAnimationCancel(Animator animation){
            }
        });
        animator.start();
    }

    public int getCurrentPosition(){
        if(datas == null || datas.length == 0){
            return -1;
        }
        if(datas.length == 1 && blocks.size() == 1){
            return 0;
        }
        return blocks.get(0).getPosition();
    }

    public class TextBlock {
        private int width;
        private int height;
        private String text;
        private int drawX;
        private int drawY;
        private Paint paint;
        private int position;

        public TextBlock(int width, int height, Paint paint){
            this.width = width;
            this.height = height;
            this.paint = paint;
        }

        public void reset(int centerY){
            reset(text, centerX, centerY, position);
        }

        public void reset(String text, int centerY){
            reset(text, centerX, centerY, position);
        }

        public void reset(String text, int centerY, int position){
            reset(text, centerX, centerY, position);
        }

        public void reset(String text, int centerX, int centerY, int position){
            this.text = text;
            this.position = position;
            int measureWidth = (int)paint.measureText(text);
            drawX = (width - measureWidth) / 2;
            Paint.FontMetrics metrics = paint.getFontMetrics();
            drawY = (int)(centerY + (metrics.bottom - metrics.top) / 2 - metrics.bottom);
        }

        public int getPosition(){
            return position;
        }

        public void draw(Canvas canvas){
            canvas.drawText(text, drawX, drawY, paint);
        }
    }

}
//跑马灯布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp"
    android:background="@drawable/home_title_bar_search_corner_bg1"
    android:layout_height="30dp">

    <TextView
        android:textSize="20dp"
        android:text="京东快报:"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    <com.bwie.jd_zj.Zidingyi.VerticalMarqueeView
        android:layout_weight="1"
        android:id="@+id/vv"
        android:layout_width="wrap_content"
        android:layout_height="30dp" />

      <View
          android:layout_marginTop="6dp"
          android:background="#9999"
          android:layout_marginRight="17dp"
          android:layout_width="2dp"
          android:layout_height="15dp"/>
     <TextView
         android:layout_marginRight="2dp"
        android:layout_marginBottom="5dp"
        android:textSize="18dp"
        android:layout_gravity="right"
        android:text="更多"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
//背景布局   home_title_bar_search_corner_bg1
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="8dp"/>
    <solid android:color="@color/white"/>
    </shape>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值