Android 自定义一种进度条

项目需要,自己写了个进度条,上图:


















自定义ProgressView代码:

package com.anyanyan.vmoive.customView;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;

import com.anyanyan.vmoive.R;

/**
 * Created by Administrator on 2016/12/12.
 */
public class MyProgress extends View {
    private Paint paint, linePaint;
    private TextPaint textPaint;
    private int progress;
    private float strokeWidth = 40;
    private int color, textColor;

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

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

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

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyProgress);
        color = typedArray.getColor(R.styleable.MyProgress_progressColor, Color.parseColor
                ("#000000"));
        textColor  = typedArray.getColor(R.styleable.MyProgress_progressTextColor,Color.parseColor("#000000"));
        typedArray.recycle();
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(color);
        linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        linePaint.setStyle(Paint.Style.FILL);
        linePaint.setStrokeWidth(3);
        linePaint.setColor(Color.WHITE);
        textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextSize(43);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        strokeWidth = getWidth() / 5f;
        paint.setStrokeWidth(strokeWidth);
        float sweepAnge = progress / 100f * 360;
        int count = Math.round(sweepAnge / 12f);
        sweepAnge = count * 12f;
        drawMyArc(canvas, sweepAnge);
        drawText(canvas,count);
        drawMyLine(canvas, sweepAnge, count);

    }

    private void drawMyArc(Canvas canvas, float sweepAnge) {
        RectF rectF = new RectF(strokeWidth/2f, strokeWidth/2f, getWidth()-strokeWidth/2f, getHeight()-strokeWidth/2f);
        canvas.drawArc(rectF, -90, sweepAnge, false, paint);
    }

    private void drawText(Canvas canvas, int count){
        String text = count + "";
        float textWidth = textPaint.measureText(text);
        float textHeight = textPaint.ascent() + textPaint.descent();
        int width = getWidth();
        int height = getHeight();
        canvas.drawText(text,(width-textWidth)/2f,(height-textHeight)/2f,textPaint);
    }

    private void drawMyLine(Canvas canvas, float sweepAnge, int count) {
        int width = getWidth();
        int height = getHeight();
        canvas.save();
        count=count>=30?count:count-1;
        for (int i = 0; i < count; i++) {
            canvas.rotate(12, width / 2, height / 2);
            canvas.drawLine(width / 2f, 1, width / 2f, strokeWidth-2, linePaint);
        }
        canvas.restore();
    }
}
自定义属性:values/attrs

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyProgress">
        <attr name="progressColor" format="color"/>
        <attr name="progressTextColor" format="color"/>
    </declare-styleable>
</resources>
布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ayy="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.anyanyan.vmoive.activity.Main6Activity">

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="setProgress"/>

    <com.anyanyan.vmoive.customView.MyProgress
        android:id="@+id/myProgress"
        android:layout_width="100dp"
        android:layout_height="100dp"
        ayy:progressColor="#FF83B64D"
        android:layout_gravity="center_horizontal"/>
</LinearLayout>
代码里设置进度:

public class Main6Activity extends AppCompatActivity {

    @BindView(R.id.et)
    EditText mEt;
    @BindView(R.id.myProgress)
    MyProgress mMyProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn)
    public void onClick() {
        String str = mEt.getText().toString().trim();
        if (TextUtils.isEmpty(str)){
            return;
        }
        int progress = Integer.valueOf(str);
        mMyProgress.setProgress(progress%101);
    }
}

完成!



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值