波纹刻度电池View

波纹刻度电池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();
                }
            }
        }
    }
}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,504评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,434评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,089评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,378评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,472评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,506评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,519评论 3 413
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,292评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,738评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,022评论 2 329
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,194评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,873评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,536评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,162评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,413评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,075评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,080评论 2 352

推荐阅读更多精彩内容