博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
波浪刻度电池View
阅读量:7206 次
发布时间:2019-06-29

本文共 8690 字,大约阅读时间需要 28 分钟。

波浪刻度电池View

import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.LinearGradient;import android.graphics.Paint;import android.graphics.Path;import android.graphics.RectF;import android.graphics.Region;import android.graphics.Shader;import android.os.Build;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.view.View;import com.ums.flowmanager.utils.LoggerUtil;public class WaveBatteryView extends View {    private final Paint bollPaint = new Paint();    // 水的颜色    public int waveColor = Color.parseColor("#010000FF");    Context context;    private float paramFloat = 0.5F;    private Path iPath;    private float radius = 1.0F;    // 圆圈背景颜色 可以设置为透明    private float circleBg = 0.0F;    // 水浪画笔    private Paint wavePaint;    private Path path;    // 左右偏移 φ    private int offset_lan = 0;    // 上下偏移    private float offset_por = -10;    // 角速度    private float angular_velocity = 0.5f;    // 振幅    private int amplitude = 20;    private int height;    private int width;    private float targetHeight;    private float textHeight;    private int progress = 0;    // 0% 时,空白的高度    private float baseBlank;    private float left = 50f;    private float right = 150f;    private float bottom = 180f;    private float top = 15f;    private WaveBatteryView.OnFinishedListener listener;    private int ms = 4;    private boolean isRun = true;    private Handler mHandler = new Handler() {        @Override        public void handleMessage(Message msg) {            if (msg.what == 1) {                invalidate();            }        }    };    private MyThread myThread;    public WaveBatteryView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        this.context = context;        init();        d();    }    private void init() {        wavePaint = new Paint();        wavePaint.setAntiAlias(true);        wavePaint.setShader(new LinearGradient(130f,                0f,                130f,                260f,                Color.parseColor("#34A350"),                Color.parseColor("#00c19a"),                Shader.TileMode.MIRROR));        wavePaint.setColor(waveColor);        path = new Path();        if (null == myThread) {            myThread = new MyThread();        }//todo        myThread.start();        isRun = true;//        new MyThread().start();    }    private void d() {        bollPaint.setStrokeWidth(1.0F);        // this.b.setColor(this.e);        this.iPath = new Path();    }    public WaveBatteryView(Context context, AttributeSet attrs) {        super(context, attrs);        this.context = context;        init();        d();    }    public WaveBatteryView(Context context) {        super(context);        this.context = context;        init();        d();    }    public void startThread() {        this.isRun = true;//        if (null == myThread) {//            myThread = new MyThread();//        }//        myThread.start();    }    public void stopThread() {        this.isRun = false;        if (null != myThread) {        myThread.interrupt();        }        myThread = null;    }    private void drawBatteryTop(Canvas canvas) {        Paint mPaint = new Paint();        mPaint.setColor(Color.parseColor("#545454"));        mPaint.isAntiAlias();        mPaint.setStyle(Paint.Style.FILL);        RectF rectF = new RectF();        rectF.left = 80f;        rectF.top =  0f;        rectF.right = 120f;        rectF.bottom = 30f;        radius = 5;        canvas.drawRoundRect(rectF, radius, radius, mPaint);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        canvas.save();        int x = getWidth();        int y = getHeight();        radius = (y / 2 - 10);        float paramY = (y * (1.0F - paramFloat) - 4.0f);        circleBg = (paramY + 4.0f);        this.iPath.reset();        bollPaint.setColor(Color.TRANSPARENT);        drawBatteryTop(canvas);        Paint mPaint = new Paint();        mPaint.setColor(Color.parseColor("#545454"));        mPaint.isAntiAlias();        mPaint.setStyle(Paint.Style.FILL);        RectF rectF = new RectF();        rectF.bottom = bottom;        rectF.top = top;        rectF.left = left;        rectF.right = right;        radius = 10;        RectF rectF2 = new RectF();        rectF2.bottom = bottom;        rectF2.top = top;        rectF2.left = left;        rectF2.right = right;        radius = 10;        RectF rectF3 = new RectF();        rectF3.left = 80f;        rectF3.top =  0f;        rectF3.right = 120f;        rectF3.bottom = 30f;        canvas.drawRoundRect(rectF, radius, radius, mPaint);        // 将圆形排除到清除区域        this.iPath.addRoundRect(rectF2, radius, radius,Path.Direction.CCW);        this.iPath.addRoundRect(rectF3,5,5,Path.Direction.CCW);        // 裁剪        canvas.clipPath(this.iPath, Region.Op.REPLACE);        setPath();        // 画波浪        canvas.drawPath(path, wavePaint);    }    private void drawBattery(Canvas canvas) {        Paint mPaint = new Paint();        mPaint.setColor(Color.parseColor("#545454"));        mPaint.isAntiAlias();        mPaint.setStyle(Paint.Style.FILL);        RectF rectF = new RectF();        rectF.bottom = bottom;        rectF.top = top;        rectF.left = left;        rectF.right = right;        radius = 10;        canvas.drawRoundRect(rectF, radius, radius, mPaint);    }    @Override    @SuppressLint("NewApi")    protected void onAttachedToWindow() {        super.onAttachedToWindow();        if (Build.VERSION.SDK_INT >= 11) {            setLayerType(1, null);        }    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        initLayoutParams();    }    private void initLayoutParams() {        height = this.getHeight();        width = this.getWidth();        baseBlank = (float) (height * 0.9);        targetHeight = baseBlank;        offset_por = baseBlank;        textHeight = baseBlank;    }    private void setPath() {        int x = 0;        int y = 0;        path.reset();        for (int i = 0; i < width; i++) {            x = i;            y = (int) (amplitude * Math.sin((i * angular_velocity + offset_lan) * Math.PI / 180) + offset_por);            if (i == 0) {                path.moveTo(x, y);            }            path.quadTo(x, y, x + 1, y);        }        path.lineTo(width, height);        path.lineTo(0, height);        path.close();    }    /**     * @param p 0~1     */    public void updateProgress(float p) {        if (p >= 0 && p <= 1) {            targetHeight = (float) (baseBlank * (1 - p));            invalidate();        }    }    public WaveBatteryView setOffset_lan(int offset_lan) {        this.offset_lan = offset_lan;        return this;    }    public WaveBatteryView setWaveColor(int color) {        this.waveColor = color;        wavePaint.setColor(waveColor);        return this;    }    /**     * 波浪振幅, 默认为 20     */    public WaveBatteryView setAmplitude(int amplitude) {        this.amplitude = amplitude;        return this;    }    /**     * @param //浪宽 默认为0.5     */    public WaveBatteryView setPalstance(float angular_velocity) {        this.angular_velocity = angular_velocity;        return this;    }    /**     * @param ms * 默认为4毫秒     */    public WaveBatteryView setRefreshTime(int ms) {        this.ms = ms;        return this;    }    public void setOnFinishedListener(WaveBatteryView.OnFinishedListener l) {        this.listener = l;    }    public interface OnFinishedListener {        public void onFinished();    }    class MyThread extends Thread {        @Override        public void run() {            LoggerUtil.e("isRun:"+isRun);            while (isRun) {                offset_lan++;                if (offset_por > targetHeight) {                    offset_por -= 0.5;                    progress = (int) ((baseBlank - offset_por) / baseBlank * 100);                    if (textHeight > (height / 2)) {                        textHeight -= 0.5;                    }                }                if (progress >= 100 && listener != null) {                    listener.onFinished();                    isRun = false;                }                if (offset_lan == 360) {                    offset_lan = 0;                }                mHandler.sendEmptyMessage(1);                try {                    Thread.sleep(ms);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }}复制代码

转载于:https://juejin.im/post/5bee87cde51d4543cd173c0f

你可能感兴趣的文章
2012腾讯暑期实习面经(技术类web前端)
查看>>
第3种方法获取redis cluster主从关系
查看>>
注册表管理(本地、远程)
查看>>
《Linux内核设计与实现》第四周读书笔记——第五章
查看>>
关于COM组件log的位置
查看>>
C++操作符重载
查看>>
postgresql 时间戳格式为5分钟、15分钟
查看>>
linq中如何在join中指定多个条件
查看>>
交换排序
查看>>
【转】链表归并排序插入排序
查看>>
EL表达式和JSTL的使用
查看>>
递归:python 实现2分查找
查看>>
Centos6.5 安装 RabbitMQ3.6.1
查看>>
如何设置eclipse自动提示功能
查看>>
mosfet详解
查看>>
hdu1203
查看>>
for_each用法
查看>>
“.NET技术”Ajax和WEB服务数据格式:自定义返回格式
查看>>
DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库.
查看>>
国家计算机病毒中心假期后警惕病毒传播
查看>>