自定义VIEW 倒计时

实现方式,利用RectF画弧形

public class RoundCountDownView extends View {
    private Context context;
    private int foregroundColor; //前景颜色
    private int backgroundColor; //后景颜色
    private int fillColor; //填充颜色
    private float max; //最大值
    private boolean isClockwise; //顺时针or逆时针

    private Paint a;
    private Paint b;
    private Paint c;

    private RectF d;
    private RectF e;

    private int f = 360;
    private int g = -90;

    private float width;

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

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

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

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public RoundCountDownView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initParameters(context, attrs);
        init();
    }

    private void initParameters(Context context, AttributeSet attrs) {
        this.context = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundCountDownView);

        width = typedArray.getDimension(R.styleable.RoundCountDownView_width,
                getResources().getDimension(R.dimen.round_count_down_view));

        foregroundColor = typedArray.getColor(R.styleable.RoundCountDownView_foregroundColor,
                getResources().getColor(R.color.theme_main_2));

        backgroundColor = typedArray.getColor(R.styleable.RoundCountDownView_backgroundColor,
                getResources().getColor(R.color.white));

        fillColor = typedArray.getColor(R.styleable.RoundCountDownView_fillColor,
                getResources().getColor(R.color.white));

        max = typedArray.getFloat(R.styleable.RoundCountDownView_max, 100.0f);

        isClockwise = typedArray.getBoolean(R.styleable.RoundCountDownView_isClockwise, true);

        Log.d("RoundCountDownView", "max: " + max + " isClockwise: " + isClockwise + " width: " + width);

        /*if (isClockwise) {
            int temp = backgroundColor;
            backgroundColor = foregroundColor;
            foregroundColor = temp;
        }*/
    }

    private void init() {
        this.a = new Paint();
        this.a.setStyle(Paint.Style.STROKE);
        this.a.setStrokeWidth(this.width);
        this.a.setColor(foregroundColor);
        this.a.setAntiAlias(true);
        this.c = new Paint();
        this.c.setStyle(Paint.Style.STROKE);
        this.c.setStrokeWidth(this.width);
        this.c.setColor(backgroundColor);
        this.c.setAntiAlias(true);
        this.b = new Paint();
        this.b.setStyle(Paint.Style.FILL);
        this.b.setStrokeWidth(this.width);
        this.b.setColor(fillColor);
        this.b.setAntiAlias(true);
        this.d = new RectF();
        this.e = new RectF();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.d.set(this.width, this.width, getWidth() - this.width, getHeight() - this.width);
        this.e.set(2 * this.width - 1, 2 * this.width - 1, getWidth() - 2 * this.width + 1, getHeight() - 2 * this.width + 1);
        canvas.drawArc(this.d, 0.0F, 360.0F, true, this.c);
        canvas.drawArc(this.d, this.g, this.f, true, this.a);
        canvas.drawArc(this.e, 0.0F, 360.0F, true, this.b);
    }

    public void setProgress(float paramInt) {
        if (isClockwise)  {
            /*paramInt = max - paramInt;*/
            paramInt = -paramInt;
        }
        this.f = ((int) (360.0F * (paramInt / max)));
        Log.d("RoundCountDownView", "paramInt: " + paramInt + " this.f: " + this.f + " max: " + max);
        postInvalidate();
    }
}

attrs.xml

    <declare-styleable name="RoundCountDownView">
        <attr name="foregroundColor" format="color" />
        <attr name="backgroundColor" format="color" />
        <attr name="fillColor" format="color" />
        <attr name="width" format="dimension" />
        <attr name="isClockwise" format="boolean" />
        <attr name="max" format="float" />
    </declare-styleable>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现倒计时圆形进度,你可以创建一个自定义View,使用 Canvas 和 Paint 来绘制圆形和进度条。以下是一个示例代码: ```java public class CountdownCircleView extends View { private int maxProgress = 100; // 总进度 private int progress = 100; // 当前进度 private int circleColor = Color.GRAY; // 圆形颜色 private int progressColor = Color.BLUE; // 进度条颜色 private Paint circlePaint; private Paint progressPaint; public CountdownCircleView(Context context) { super(context); init(); } public CountdownCircleView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } public CountdownCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { circlePaint = new Paint(); circlePaint.setAntiAlias(true); circlePaint.setColor(circleColor); progressPaint = new Paint(); progressPaint.setAntiAlias(true); progressPaint.setColor(progressColor); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int viewWidth = getWidth(); int viewHeight = getHeight(); // 绘制圆形 int diameter = Math.min(viewWidth, viewHeight); int radius = diameter / 2; int centerX = viewWidth / 2; int centerY = viewHeight / 2; canvas.drawCircle(centerX, centerY, radius, circlePaint); // 绘制进度条 RectF rectF = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius); float sweepAngle = 360f * progress / maxProgress; canvas.drawArc(rectF, -90, sweepAngle, true, progressPaint); } public void setMaxProgress(int maxProgress) { this.maxProgress = maxProgress; } public void setProgress(int progress) { this.progress = progress; invalidate(); // 重新绘制 } } ``` 你可以将上述代码放入你的 Android 项目中,并在布局文件中使用 `CountdownCircleView`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值