Android 自定义View-动态跳动线

View的绘制基本由measure()、layout()、draw()这个三个函数完成

函数作用相关方法
measure()测量View的宽高getMeasuredWidth(),getMeasuredHeight()
layout()计算当前View以及子View的位置layout(),onLayout(),setFrame()
draw()视图的绘制工作onDraw()

效果图如下:

代码如下:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import java.lang.ref.WeakReference;


public class LineView extends View {
    private static final int mScaleMargin = 5; //刻度间距
    LineThread lineThread;
    private int start = 0;//起始值
    private int beforeInit = 0;//前一个初始值
    private int mWidth;//控件宽度
    private int mHeight;//控件高度

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

    public LineView(Context context, AttributeSet attrs) {
        super(context, attrs);
        lineThread = new LineThread(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint paint = new Paint();
        int count = mWidth / mScaleMargin;//总刻度线,取控件一半宽度
        int midLevel = mHeight / 2;//平均高度
//        Log.i("LineView", "count: " + count + ",mid: " + midLevel);
        //算法 1 2 3 4 3 2 1 2 3 4 3 2 1
        for (int i = start; i < count + start; i++) {
            if (i % 6 == 0) {//筛选出最小值
                canvas.drawLine((i - start) * mScaleMargin, midLevel - 20, (i - start) * mScaleMargin, midLevel + 20, paint);
            } else {
                if (i % 3 == 0) {//筛选出最大值
                    canvas.drawLine((i - start) * mScaleMargin, midLevel - 80, (i - start) * mScaleMargin, midLevel + 80, paint);
                } else {
                    if (i % 2 == 0) {//筛选出第二大值
                        canvas.drawLine((i - start) * mScaleMargin, midLevel - 40, (i - start) * mScaleMargin, midLevel + 40, paint);
                    } else {
                        canvas.drawLine((i - start) * mScaleMargin, midLevel - 30, (i - start) * mScaleMargin, midLevel + 30, paint);
                    }
                }
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    public void stopAnim() {
        if (lineThread != null) {
            lineThread.setRun(false);
            lineThread.interrupt();
            lineThread = null;
        }
    }

    public void startAnim() {
        lineThread = new LineThread(this);
        lineThread.setRun(true);
        lineThread.start();
    }

    public class LineThread extends Thread {
        private WeakReference<LineView> threadWeakReference;
        private boolean running = false;

        LineThread(LineView lineView) {
            this.threadWeakReference = new WeakReference<>(lineView);
        }

        private LineView getLieView() {
            return threadWeakReference.get();
        }

        public void setRun(boolean isRun) {
            this.running = isRun;
        }

        @Override
        public void run() {
            super.run();
            while (running) {
                try {
                    Thread.sleep(200);
                    //符合 0 1 2 3 2 1 0 算法
                    if (start == 0) {//峰谷
                        getLieView().beforeInit = start;
                        start++;
                    } else if (start == 3) {//峰顶
                        getLieView().beforeInit = start;
                        start--;
                    } else if (start == 1) {
                        if (getLieView().beforeInit > start) {
                            start--;
                        } else if (getLieView().beforeInit < start) {
                            start++;
                        }
                    } else if (start == 2) {
                        if (getLieView().beforeInit > start) {
                            start--;
                        } else if (getLieView().beforeInit < start) {
                            start++;
                        }
                    }
                    postInvalidate();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值