自定義圓形進度條
CircleBarView
package wanghuiqi.bawie.com.whq_yk_moni1.model;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.OvershootInterpolator;
public class CircleBarView extends View { private final Context context; private Paint paint; private Paint bkPaint; private Paint tvPaint; private Rect mBound; private int height; private int width; private float progress;//進度 public CircleBarView(Context context) { this(context, null); } public CircleBarView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleBarView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; this.paint = new Paint(); this.bkPaint = new Paint(); this.tvPaint = new Paint(); this.mBound = new Rect(); init(); } private void init() { paint.setStyle(Paint.Style.FILL); paint.setColor(Color.parseColor("#d91b1b")); paint.setAntiAlias(true); bkPaint.setStyle(Paint.Style.FILL); bkPaint.setColor(Color.parseColor("#afa7a7")); bkPaint.setAntiAlias(true); tvPaint.setColor(Color.parseColor("#ffffff")); tvPaint.setTextSize(25); } //獲取當前控制元件的高度和寬度 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); height = MeasureSpec.getSize(heightMeasureSpec); width = MeasureSpec.getSize(widthMeasureSpec); } private float setDegree(float sendFt) { //將進度的數值變為弧度值 return sendFt * 3.6f; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (width * height == 0) { return; } canvas.drawArc(new RectF(0, 0, width, height), 270, setDegree(progress), true, paint); canvas.drawCircle(width / 2, height / 2, width / 2 - 5, bkPaint); if (progress < 100) { String strPro = String.valueOf(progress) + "%"; tvPaint.getTextBounds(strPro, 0, strPro.length(), mBound); canvas.drawText(strPro, width / 2 - mBound.width() / 2, height / 2 + mBound.height() / 2, tvPaint); } else { //達到100%顯示完成 String text = "完成"; tvPaint.getTextBounds(text, 0, text.length(), mBound); canvas.drawText(text, width / 2 - mBound.width() / 2, height / 2 + mBound.height() / 2, tvPaint); } } public void setProgress(float progress) { this.progress=progress; postInvalidate(); } }
MainActivity
package wanghuiqi.bawie.com.whq_yk_moni1;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import wanghuiqi.bawie.com.whq_yk_moni1.model.CircleBarView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private CircleBarView circle_pro;
private float progress = 0;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 1) {
if (progress <= 99) {
++progress;
circle_pro.setProgress(progress);
this.sendEmptyMessageDelayed(1, 1);
}
}
}
};
private TextView textBawei;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.circle_pro:
handler.sendEmptyMessageDelayed(1, 1);
donghua();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, PageActivity.class);
startActivity(intent);
finish();
}
}, 2000);
break;
}
}
public void donghua() {
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator ob_scale = ObjectAnimator.ofFloat(textBawei, "scaleX", new float[]{1f, 3f, 1f});
ob_scale.setDuration(2000);
ObjectAnimator ob_alpha = ObjectAnimator.ofFloat(textBawei, "alpha", new float[]{1.0f,0.8f});
ob_alpha.setDuration(2000);
ObjectAnimator ob_rotation = ObjectAnimator.ofFloat(textBawei, "rotationX", new float[]{0f, 360f});
ob_rotation.setDuration(2000);
animatorSet.playTogether(ob_scale,ob_alpha,ob_rotation);
animatorSet.start();
}
private void initView() {
circle_pro = findViewById(R.id.circle_pro);
circle_pro.setOnClickListener(this);
textBawei = findViewById(R.id.text_bw);
}
}