欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android 自定义View-动态跳动线

程序员文章站 2022-03-28 12:58:44
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;/** * ================================================ * 作 .....

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

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

效果图如下:

Android 自定义View-动态跳动线

代码如下:


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();
                }
            }
        }
    }

}

本文地址:https://blog.csdn.net/xiaohelloming/article/details/107952877

相关标签: Android