美文网首页
Android充电进度获取,圆形波浪进度展示

Android充电进度获取,圆形波浪进度展示

作者: 水言 | 来源:发表于2017-06-26 11:16 被阅读239次

    Android 充电进度获取

    1.判断当前是否正在冲电

        private boolean isCharging(Context c) {
            IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
            Intent batteryStatus = c.registerReceiver(null, ifilter);
            int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                    status == BatteryManager.BATTERY_STATUS_FULL;
            return isCharging;
        }
    

    2.充电当前进度获取

    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = registerReceiver(null, ifilter);
    int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    float batteryPct = (float) level / (float) scale;
    

    3.通过广播检测充电进度变化

        private BroadcastReceiver mBroadcast = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
                    //电源连接
                } else if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
                //充电进度变化
                    int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                    int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                    float batteryPct = (float) level / (float) scale;             
                } else if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
                   //断开充电线
                }
            }
        };
    
    //使用动态广播注册
        private void registerReceiver() {
            IntentFilter updateIntent = new IntentFilter();
            updateIntent.addAction("android.intent.action.ACTION_POWER_CONNECTED");
            updateIntent.addAction("android.intent.action.ACTION_POWER_DISCONNECTED");
            updateIntent.addAction("android.intent.action.ACTION_BATTERY_CHANGED");
            registerReceiver(mBroadcast , updateIntent);
        }
    

    圆形波浪进度

    效果图

    外圈就是一个简单的圆形进度,内圈波浪就是一个利用贝尔曲线绘制的波浪和一个圆两者取重叠后形成。

    贝尔曲线原理

    更多关于贝尔曲线

        private void drawWave(Canvas canvas, Paint paint, Point[] points, float waveOffset) {
            mWaveLimitPath.reset();
            mWavePath.reset();//波浪路径
            float height = lockWave ? 0 : mRadius - 2 * mRadius * mPercent;
            //moveTo和lineTo绘制出水波区域矩形
            //移动到起始位置
            mWavePath.moveTo(points[0].x + waveOffset, points[0].y + height);
            for (int i = 1; i < mAllPointCount; i += 2) {
                //前面两个是控制位参数,后面两个是终点位
                mWavePath.quadTo(points[i].x + waveOffset, points[i].y + height,
                        points[i + 1].x + waveOffset, points[i + 1].y + height);
            }
            mWavePath.lineTo(points[mAllPointCount - 1].x, points[mAllPointCount - 1].y + height);
            //不管如何移动,波浪与圆路径的交集底部永远固定,否则会造成上移的时候底部为空的情况
            mWavePath.lineTo(points[mAllPointCount - 1].x, mCenterPoint.y + mRadius);
            mWavePath.lineTo(points[0].x, mCenterPoint.y + mRadius);
            mWavePath.close();
            mWaveLimitPath.addCircle(mCenterPoint.x, mCenterPoint.y, mRadius, Path.Direction.CW);
            //取该圆与波浪路径的交集,形成波浪在圆内的效果
            mWaveLimitPath.op(mWavePath, Path.Op.INTERSECT);//取重叠部分
            canvas.drawPath(mWaveLimitPath, paint);
        }
    
        private void startDarkWaveAnimator() {
            if (mDarkWaveAnimator != null && mDarkWaveAnimator.isRunning()) {
                return;
            }
            mDarkWaveAnimator = ValueAnimator.ofFloat(0, 2 * mRadius);
            mDarkWaveAnimator.setDuration(mDarkWaveAnimTime);
            mDarkWaveAnimator.setRepeatCount(ValueAnimator.INFINITE);//循环
            mDarkWaveAnimator.setInterpolator(new LinearInterpolator());
            mDarkWaveAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    mDarkWaveOffset = (float) animation.getAnimatedValue();
                   //改变起始坐标的Y值(上面方法drawWave的waveOffset参数),通过参数控制实现波浪抖动
                    postInvalidate();//重绘
                }
            });
            mDarkWaveAnimator.addListener(
                    new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationStart(Animator animation) {
                            super.onAnimationStart(animation);
                            mDarkWaveOffset = 0;
                        }
                    }
            );
            mDarkWaveAnimator.start();
        }
    

    参考:
    https://github.com/MyLifeMyTravel/CircleProgress
    http://blog.csdn.net/z82367825/article/details/51599245

    相关文章

      网友评论

          本文标题:Android充电进度获取,圆形波浪进度展示

          本文链接:https://www.haomeiwen.com/subject/xtcuqxtx.html