自定义圆形加载(简单)

//自定义圆形加载(简单)
public class Yuan extends View {

private int mCurrent;//当前进度
private Paint mPaintOut;
private Paint mPaintCurrent;
private Paint mPaintText;
private RectF rectF;
private int width;
private int height;

private OnLoadingCompleteListener mLoadingCompleteListener;
private int mWidth;

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

public Yuan(Context context, AttributeSet attrs) {
    this(context, attrs, -1);
}

public Yuan(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    //画笔->背景圆弧
    mPaintOut = new Paint();
    mPaintOut.setAntiAlias(true);
    mPaintOut.setStrokeWidth(16f);
    mPaintOut.setStyle(Paint.Style.STROKE);
    mPaintOut.setColor(Color.GRAY);
    mPaintOut.setStrokeCap(Paint.Cap.ROUND);

    //画笔->进度圆弧
    mPaintCurrent = new Paint();
    mPaintCurrent.setAntiAlias(true);
    mPaintCurrent.setStrokeWidth(16f);
    mPaintCurrent.setStyle(Paint.Style.STROKE);
    mPaintCurrent.setColor(Color.RED);
    mPaintCurrent.setStrokeCap(Paint.Cap.ROUND);

    //画笔->绘制字体
    mPaintText = new Paint();
    mPaintText.setAntiAlias(true);
    mPaintText.setStyle(Paint.Style.FILL);
    mPaintText.setColor(Color.BLACK);
    mPaintText.setStrokeWidth(10f);
    mPaintText.setTextSize(18f);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    width = getMeasuredWidth();
    height = getMeasuredHeight();
    rectF = new RectF(0, 0, width, height);
    setMeasuredDimension(300, 300);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawArc(rectF, 0, 360, false, mPaintOut);
    //绘制当前进度
    float sweepAngle = 360 * mCurrent / 100;
    canvas.drawArc(rectF, 360, sweepAngle, false, mPaintCurrent);
    //绘制进度数字
    String text = mCurrent + "%";
    //获取文字宽度
    float textWidth = mPaintText.measureText(text, 0, text.length());
    float dx = getWidth() / 2 - textWidth / 2;
    Paint.FontMetricsInt fontMetricsInt = mPaintText.getFontMetricsInt();
    float dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
    float baseLine = getHeight() / 2 + dy;
    canvas.drawText(text, dx, baseLine, mPaintText);
    if (mLoadingCompleteListener != null && mCurrent == 100) {
        mLoadingCompleteListener.complete();
    }
}

/**
 * 设置当前进度并重新绘制界面
 *
 * @param mCurrent
 */
public void setmCurrent(int mCurrent) {
    this.mCurrent = mCurrent;
    invalidate();
}

public void setOnLoadingCompleteListener(OnLoadingCompleteListener loadingCompleteListener) {
    this.mLoadingCompleteListener = loadingCompleteListener;
}

public interface OnLoadingCompleteListener {
    void complete();
}

}

oid.com/apk/res/android"
xmlns:app=“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:gravity=“center”
tools:context=".MainActivity">

<com.qgs.gd.zdy.Yuan
    android:id="@+id/yuan"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

public class MainActivity extends AppCompatActivity {

private Yuan yuan;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    yuan = findViewById(R.id.yuan);

    ValueAnimator animator = ValueAnimator.ofFloat(0,100);
    animator.setDuration(6000);
    animator.setInterpolator(new LinearInterpolator());
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float current = (float) animation.getAnimatedValue();
            yuan.setmCurrent((int) current);
        }
    });
    animator.start();

    yuan.setOnLoadingCompleteListener(new Yuan.OnLoadingCompleteListener() {
        @Override
        public void complete() {
            Toast.makeText(MainActivity.this, "加载完成", Toast.LENGTH_SHORT).show();
        }
    });
}

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值