Android 自定义一种进度条

来源:互联网 发布:网络剧错爱一生演员表 编辑:程序博客网 时间:2024/05/17 02:35

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


















自定义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
原创粉丝点击