自定义控件Android开发程序员

Android仿微信摄像圆环进度

2017-11-24  本文已影响339人  奔跑吧李博

平时都路上,生活上遇到新奇的事情,都要立即打开微信视频录下来发给朋友看看。这个录制进度条看起来还不错哦,就仿着写了一个,不是样式完全的高仿,是功能的仿制。

微信效果:
微信效果

github代码直通车

自制效果:
自制效果
实现过程:

1.自定义圆半径和圆环颜色属性:

    <declare-styleable name="CiclePercentView">
        <attr name="radius" format="integer"/>
        <attr name="ring_color" format="color"/>
    </declare-styleable>

2.设置3支画笔,分别画圆环,背景浅白色,中心白色圆。

    private void init() {
        paint = new Paint();
        paint.setColor(ringColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(14);

        bgPaint = new Paint();
        bgPaint.setAntiAlias(true);
        bgPaint.setColor(getResources().getColor(R.color.halfwhite));

        centerPaint = new Paint();
        centerPaint.setAntiAlias(true);
        centerPaint.setColor(Color.WHITE);

        //起始角度
        startAngle = -90;
    }

3.依次画背景圆,中心圆,圆弧。canvas.drawArc(),第一个参数表示圆弧外切矩形大小;第二、三个参数表示起始角度,当前角度,-90度为12点方向,0度为3点方向,这里用-90度作为起始;第四个参数表示是否与中心点填充为扇形,false表示只画圆弧线;

画圆弧drawArc()方法参数

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

        //画圆弧
        RectF rectf = new RectF(6,6,dp2px(radius-2),dp2px(radius-2));
        canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,dp2px(radius)/2,bgPaint);
        canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,dp2px(radius/3)/2,centerPaint);
        canvas.drawArc(rectf,startAngle,curAngle,false,paint);
    }

4.计时器,每100毫秒更新一次进度,可设置拍摄总时间totalTime;时间转化为进度范围为0-100;

    public void countDown(final int totalTime){
        countDownTimer = new CountDownTimer(totalTime, (long)(totalTime/100f)) {
            @Override
            public void onTick(long millisUntilFinished) {
                curPercentate = (int) ((totalTime-millisUntilFinished)/(float)totalTime*100);
                percentToAngle(curPercentate);
            }

            @Override
            public void onFinish() {
                curPercentate = 0;
                percentToAngle(curPercentate);
            }
        }.start();
    }

5.按下开始拍摄,只要抬起就完成拍摄,进度恢复为0。

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                countDown(countdownTime);
                break;
            case MotionEvent.ACTION_UP:
                countDownTimer.cancel();
                curPercentate = 0;
                percentToAngle(curPercentate);
                break;
        }
        return true;
    }

CiclePercentView类完整代码:

public class CiclePercentView extends View{
    private Paint paint;
    private int curAngle;
    private int curPercentate;
    private Paint bgPaint,centerPaint;
    private int radius;
    private int ringColor;
    private int startAngle;
    private int countdownTime;
    private CountDownTimer countDownTimer;

    public CiclePercentView(Context context) {
        super(context);
        init();
    }

    public CiclePercentView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.CiclePercentView);
        radius = array.getInt(R.styleable.CiclePercentView_radius,85);
        ringColor = array.getColor(R.styleable.CiclePercentView_ring_color,Color.GREEN);
        array.recycle();

        init();
    }

    private void init() {
        paint = new Paint();
        paint.setColor(ringColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(14);

        bgPaint = new Paint();
        bgPaint.setAntiAlias(true);
        bgPaint.setColor(getResources().getColor(R.color.halfwhite));

        centerPaint = new Paint();
        centerPaint.setAntiAlias(true);
        centerPaint.setColor(Color.WHITE);

        //起始角度
        startAngle = -90;
    }

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

        //画圆弧
        RectF rectf = new RectF(6,6,dp2px(radius-2),dp2px(radius-2));
        canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,dp2px(radius)/2,bgPaint);
        canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,dp2px(radius/3)/2,centerPaint);
        canvas.drawArc(rectf,startAngle,curAngle,false,paint);
    }

    private void percentToAngle(int percentage){
        curAngle =  (int) (percentage/100f*360);
        invalidate();
    }

    public void setCountdownTime(int countdownTime){
        this.countdownTime = countdownTime;
    }

    public void countDown(final int totalTime){
        countDownTimer = new CountDownTimer(totalTime, (long)(totalTime/100f)) {
            @Override
            public void onTick(long millisUntilFinished) {
                curPercentate = (int) ((totalTime-millisUntilFinished)/(float)totalTime*100);
                percentToAngle(curPercentate);
            }

            @Override
            public void onFinish() {
                curPercentate = 0;
                percentToAngle(curPercentate);
            }
        }.start();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                countDown(countdownTime);
                break;
            case MotionEvent.ACTION_UP:
                countDownTimer.cancel();
                curPercentate = 0;
                percentToAngle(curPercentate);
                break;
        }
        return true;
    }

    private int dp2px(int dp){
        return (int) (getContext().getResources().getDisplayMetrics().density*dp + 0.5);
    }
}

喜欢我就点我吧,读者的关注和喜欢是我不懈的动力。

上一篇下一篇

猜你喜欢

热点阅读