我是靠谱客的博主 优雅鼠标,最近开发中收集的这篇文章主要介绍android自定义view之动显示取电池电量,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

先看效果图:

 直接上代码 


public class BatteryView extends View {
    private int mMargin = 1;    //电池内芯与边框的距离
    private int mBorder = 1;     //电池外框的宽带
    private int mWidth = 0;    //总长
    private int mHeight = 0;   //总高
    private int mHeadWidth = 2;
    private int mHeadHeight = 3;

    private RectF mMainRect;
    private RectF mHeadRect;
    private float mRadius = 0f;   //圆角
    private float mPower;
    private Path path;
    private boolean mIsCharging;    //是否在充电
    private Paint paintOuter;
    private Paint paintInner;
    private Paint paintLight;


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

    public BatteryView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public BatteryView(Context context) {
        this(context,null);
    }

    private void initView() {
        paintOuter = new Paint();

        //画电池头
        paintOuter.setStyle(Paint.Style.STROKE);  //设置空心矩形
        paintOuter.setStrokeWidth(mBorder);          //设置边框宽度
        paintOuter.setColor(Color.WHITE);
        //画电池芯
        paintInner = new Paint();
        paintInner.setStyle(Paint.Style.FILL_AND_STROKE);    //设置空心矩形
        paintInner.setStrokeWidth(mBorder);          //设置边框宽度
        //充电
        paintLight = new Paint();
        paintLight.setColor(Color.WHITE);
        paintLight.setStyle(Paint.Style.FILL_AND_STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mWidth = getWidth();
        mHeight = getHeight();
        mHeadRect = new RectF(1, (mHeight - mHeadHeight)/2, mHeadWidth, (mHeight + mHeadHeight)/2);

        float outLeft = mHeadRect.width()+ mBorder;
        float outTop = mBorder;
        float outRight = mWidth- mBorder;
        float outBottom = mHeight- mBorder;
        mMainRect = new RectF(outLeft, outTop, outRight, outBottom);
        LogUtil.d("mMainRect:"+mMainRect.toShortString());
        //画电池头
        canvas.drawRect(mHeadRect, paintOuter);

        //画外框
        paintOuter.setColor(Color.WHITE);
        canvas.drawRoundRect(mMainRect, mRadius, mRadius, paintOuter);

        //画电池芯
        int width   = (int) (mPower * (mMainRect.width() - mMargin*2- mBorder *2));
        int left    = (int) (mMainRect.right - mBorder - mMargin - width);
        int right   = (int) (mMainRect.right - mBorder - mMargin);
        int top     = (int) (mMainRect.top + mBorder + mMargin);
        int bottom  = (int) (mMainRect.bottom- mBorder - mMargin);
        Rect rect = new Rect(left,top,right, bottom);
        if (mIsCharging) {
            paintInner.setColor(Color.GREEN);
            canvas.drawRect(rect, paintInner);
            //绘制完全的闪电
            canvas.drawPath(path, paintLight);
        } else {
            if (mPower < 0.1) {
                paintInner.setColor(Color.RED);
            } else {
                paintInner.setColor(Color.WHITE);
            }
            canvas.drawRect(rect, paintInner);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mWidth, mHeight);

        //使用path绘制闪电形状
        path = new Path();
        path.moveTo(mWidth * 4 / 7, 0);

        path.lineTo(mWidth * 5 / 14, 4 * mHeight / 7);
        path.lineTo(mWidth * 3 / 7, 4 * mHeight / 7);
        path.lineTo(mWidth * 3 / 7, mHeight);
        path.lineTo(mWidth  * 9 / 14, 3 * mHeight / 7);
        path.lineTo(mWidth * 4 / 7, 3 * mHeight / 7);
        path.close();

    }

    private void setPower(float power) {
        mPower = power;
        invalidate();
    }

    private BroadcastReceiver mPowerConnectionReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            mIsCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                    status == BatteryManager.BATTERY_STATUS_FULL;

            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

            setPower(((float) level)/scale);
        }
    };

    @Override
    protected void onAttachedToWindow() {
        getContext().registerReceiver(mPowerConnectionReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        super.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {
        getContext().unregisterReceiver(mPowerConnectionReceiver);
        super.onDetachedFromWindow();
    }
}

最后

以上就是优雅鼠标为你收集整理的android自定义view之动显示取电池电量的全部内容,希望文章能够帮你解决android自定义view之动显示取电池电量所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部