Android 4.4 kk 电量图标分析

本人因工作需要,把4.4kkSystemUi中的电量图标由竖屏改为横屏,那我在这里跟大家分享下,众所周知在android4.4之前电量的改变都是通过监听电量变化,进行图片切换,图片资源一般都在framework\base\core\res中,而在android 4.4开始,电量图标都是通过画布画出来,下面通过代码我们看一看它是怎样进行改变的

下面是竖屏的电量画图

 public void draw(Canvas c) {
        BatteryTracker tracker = mDemoMode ? mDemoTracker : mTracker;
        final int level = tracker.level;
        /// M: Support Battery Protection.
        final boolean mChargingProtection = 
            tracker.plugged && BatteryHelper.isPlugForProtection(tracker.status, tracker.level);

        if (level == BatteryTracker.UNKNOWN_LEVEL) return;

        float drawFrac = (float) level / 100f;
        final int pt = getPaddingTop();//0
        final int pl = getPaddingLeft();//0
        final int pr = getPaddingRight();//0
        final int pb = getPaddingBottom();//0
        int height = mHeight - pt - pb;//108
        int width = mWidth - pl - pr;//60
    
        /// M: Support Wireless Charging. 支持无线充电
        if (mChargingProtection && BatteryHelper.isWirelessCharging(tracker.plugType)) {
            height = (int)((mHeight - pt - pb) * 0.95);
        }
        //竖屏
        mButtonHeight = (int) (height * 0.12f);//13
        //横屏
        //mButtonHeight=(int)(width*0.12f);
        //left,top,right,bottom
        //竖屏
        mFrame.set(0, 0, width, height);
         //横屏
        //mFrame.set(0, 0, width-rightRectWidth, height);
        mFrame.offset(pt, pr);
        //竖屏
        mButtonFrame.set(
        		mFrame.left + width * 0.25f,
        		mFrame.top,
        		mFrame.right - width * 0.25f,
        		mFrame.top + mButtonHeight + 5 );//cover frame border of intersecting area
       

        //横屏
       /* mButtonFrame.set(
        		mFrame.right-mButtonHeight-5,
        		mFrame.top + height*0.25f,
        		mFrame.right,
        		mFrame.bottom-height*0.25f);*/
       
        //竖屏
        mButtonFrame.top += SUBPIXEL;
        mButtonFrame.left += SUBPIXEL;
        mButtonFrame.right -= SUBPIXEL;

        mFrame.top += mButtonHeight;
        mFrame.left += SUBPIXEL;
        mFrame.top += SUBPIXEL;
        //横屏
       /* mButtonFrame.top += SUBPIXEL;
        mButtonFrame.bottom -= SUBPIXEL;
        mButtonFrame.right -= SUBPIXEL;
        
        mFrame.right -= mButtonHeight+5;
        mFrame.top += SUBPIXEL;
        mFrame.right += SUBPIXEL;*/
        
        // first, draw the battery shape
        c.drawRect(mFrame, mFramePaint);

        // fill 'er up
        final int color = tracker.plugged ? mChargeColor : getColorForLevel(level);
        //竖屏
        mBatteryPaint.setColor(color);
        //横屏
       // mBatteryPaint.setColor(R.color.batterymeter_current_color);
        if (level >= FULL) {
            drawFrac = 1f;
        } else if (level <= EMPTY) {
            drawFrac = 0f;
        }

        c.drawRect(mButtonFrame, drawFrac == 1f ? mBatteryPaint : mFramePaint);

        mClipFrame.set(mFrame);
        //竖屏
        mClipFrame.top += (mFrame.height() * (1f - drawFrac));
        //横屏
        //mClipFrame.right += (mFrame.width() * (1f - drawFrac));

        c.save(Canvas.CLIP_SAVE_FLAG);
        c.clipRect(mClipFrame);
        c.drawRect(mFrame, mBatteryPaint);
        c.restore();

        if (mChargingProtection) {
            // draw the bolt
        	//竖屏
        	final float bl = mFrame.left + mFrame.width() / 4.5f;
            final float bt = mFrame.top + mFrame.height() / 6f;
            final float br = mFrame.right - mFrame.width() / 7f;
            final float bb = mFrame.bottom - mFrame.height() / 10f;
        	//横屏
            /*final float bl = mFrame.left + mFrame.width() / 10f;
            final float bt = mFrame.top + mFrame.height() / 4.5f;
            final float br = mFrame.right - mFrame.width() / 6f;
            final float bb = mFrame.bottom - mFrame.height() / 7f;*/
            //画闪电图标
            if (mBoltFrame.left != bl || mBoltFrame.top != bt
                    || mBoltFrame.right != br || mBoltFrame.bottom != bb) {
                mBoltFrame.set(bl, bt, br, bb);
                mBoltPath.reset();
                mBoltPath.moveTo(
                        mBoltFrame.left + mBoltPoints[0] * mBoltFrame.width(),
                        mBoltFrame.top + mBoltPoints[1] * mBoltFrame.height());
                for (int i = 2; i < mBoltPoints.length; i += 2) {
                    mBoltPath.lineTo(
                            mBoltFrame.left + mBoltPoints[i] * mBoltFrame.width(),
                            mBoltFrame.top + mBoltPoints[i + 1] * mBoltFrame.height());
                }
                mBoltPath.lineTo(
                        mBoltFrame.left + mBoltPoints[0] * mBoltFrame.width(),
                        mBoltFrame.top + mBoltPoints[1] * mBoltFrame.height());
            }
            c.drawPath(mBoltPath, mBoltPaint);

            /// M: Support Wireless Charging.
            if (BatteryHelper.isWirelessCharging(tracker.plugType)) {
                c.drawLine(mFrame.left,
                            mHeight,
                            mFrame.right,
                            mHeight,
                            mBatteryPaint);
            }
        } else if (level <= EMPTY) {
            final float x = mWidth * 0.5f;
            final float y = (mHeight + mWarningTextHeight) * 0.48f;
            c.drawText(mWarningString, x, y, mWarningTextPaint);
        } else if (mShowPercent && !(tracker.level == 100 && !SHOW_100_PERCENT)) {
            mTextPaint.setTextSize(height *
                    (SINGLE_DIGIT_PERCENT ? 0.75f
                            : (tracker.level == 100 ? 0.38f : 0.5f)));
            mTextHeight = -mTextPaint.getFontMetrics().ascent;

            final String str = String.valueOf(SINGLE_DIGIT_PERCENT ? (level/10) : level);
            final float x = mWidth * 0.5f;
            final float y = (mHeight + mTextHeight) * 0.47f;
            c.drawText(str,
                    x,
                    y,
                    mTextPaint);
        }
    }

上面的代码我把竖屏和横屏的一些不同点进行表示出了,如果大家有需要代码的请点击下方地址进行下载:

http://download.csdn.net/detail/u014379571/8705031

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值