Android自定义电源view


101 是正在充电:会循环显示充电

1-100 自定义显示



import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;


/**
 * Created by Administrator on 2016/6/25 0025.
 */
public class BatteryView extends View {

    private Paint imgPaint;
    private Paint textPaint;
    private int batteryColor =0xFF4ECB77;
    private Bitmap bitmap;
    private int progress = 101;
    private int mProgress = 20;
    private int delay = 1500;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 101:
                    if (progress == 101) {
                        mProgress+=20;
                        if (mProgress > 100) {
                            mProgress = 20;
                        }
                        invalidate();
                        handler.sendEmptyMessageDelayed(101,delay) ;
                    }
                    break;
            }
        }
    };
    public BatteryView(Context context) {
        super(context);
        init(context);
    }

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

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

    private void init(Context context) {
        if (bitmap != null) {
            bitmap.recycle();
        }

        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ico_battery_bg);

        imgPaint = new Paint();
        imgPaint.setColor(batteryColor);
        imgPaint.setAntiAlias(true);
        imgPaint.setAlpha(255);
        imgPaint.setStyle(Paint.Style.FILL);

        textPaint = new Paint();
        textPaint.setColor(batteryColor);
        textPaint.setAntiAlias(true);
        textPaint.setAlpha(255);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setTextSize(getContext().getResources().getDimensionPixelSize(R.dimen.text_14));

    }

    public void setBatteryColor(int color) {
        this.batteryColor = color;
    }
    public void setProgress(int progress){
        if (progress >=0 && progress <= 100) {
            if (progress <= 20) {
                batteryColor =0xFFFF0000;
            } else {
                batteryColor =0xFF4ECB77;
            }
            if (handler.hasMessages(101)) {
                handler.removeMessages(101);
            }
        } else if (progress == 101){
            batteryColor =0xFF4ECB77;
            if (!handler.hasMessages(101)) {
                handler.sendEmptyMessageDelayed(101, delay);
            }
        }
        this.progress = progress;
        textPaint.setColor(batteryColor);
        imgPaint.setColor(batteryColor);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int bitmapWidth = bitmap.getWidth();
        int bitmapHeight = bitmap.getHeight();
        int rectWidth = bitmapWidth - CommonUtils.dip2px(getContext(),7);
        int rectHeight = bitmapHeight - CommonUtils.dip2px(getContext(),4);
        RectF rectF = new RectF(0, (getHeight()-bitmapHeight)/2,bitmapWidth,bitmapHeight+(getHeight()-bitmapHeight)/2);

        canvas.drawBitmap(bitmap,null,rectF,new Paint());
        String text = "";
        if (progress == 101) {
            float realWidth = Float.valueOf(rectWidth * mProgress)/100;
            RectF rectF2 = new RectF(CommonUtils.dip2px(getContext(),2),(getHeight()-rectHeight)/2,realWidth+CommonUtils.dip2px(getContext(),2),rectHeight+(getHeight()-rectHeight)/2);
            canvas.drawRect(rectF2,imgPaint);
            text = getContext().getString(R.string.battery_Charging);
        } else if (progress >=0 && progress <101) {
            float realWidth = Float.valueOf(rectWidth * progress)/100;
            RectF rectF2 = new RectF(CommonUtils.dip2px(getContext(),2),(getHeight()-rectHeight)/2,realWidth+CommonUtils.dip2px(getContext(),2),rectHeight+(getHeight()-rectHeight)/2);
            canvas.drawRect(rectF2,imgPaint);
            text = progress + "%";
        }
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float fontHeight = fontMetrics.descent - fontMetrics.ascent;
        canvas.drawText(text, bitmapWidth + CommonUtils.dip2px(getContext(), 5), (getHeight() + fontHeight) / 2 - CommonUtils.dip2px(getContext(), 5), textPaint);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值