自定义圆形进度条 100% ,动画 ,拖延进入

.CircleProgressBar

public class CircleProgressBar extends View {

        private Context context;
        private int height,width;  //自定义控件的宽高
        private float progress;  //进度
        private Paint paint;  //蓝色扇形所需的画笔
        private Paint bkPaint;  //绿色圆形所需的画笔
        private Paint tvPaint;  //圆里面的进度字所需的画笔
        private Rect mBound;  //用于获取字体的大小

        public CircleProgressBar(Context context, AttributeSet attrs) {
            super(context, attrs);
            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(15);

        }

        //获取当前控件的高度和宽度,单位是像素
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            height = MeasureSpec.getSize(heightMeasureSpec);
            width = MeasureSpec.getSize(widthMeasureSpec);
        }


        private float set2Degree(float sendFt){
            //将进度的数值变为弧度数值,进度100,弧度有360,所以比例是3.6
            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, set2Degree(progress), true, paint);  //画蓝色扇形
            canvas.drawCircle(width / 2, height / 2, width / 2 - 5, bkPaint);  //画绿色圆形,半径比蓝色扇形的小5px
            if(progress < 100){   //进度没达到100%时显示进度
                String strPro = String.valueOf((int)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();
        }

}

.activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_bw"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="143dp"
        android:text="八维电商"
        android:textSize="40sp" />

    <com.bwie.lll.moni1.view.CircleProgressBar
        android:id="@+id/circle_progress"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="159dp" />

</RelativeLayout>

.MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private CircleProgressBar circleProgressBar;
    private float progress = 0;
    private TextView tv_bw;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                if (progress <= 99) {
                    ++progress;
                    circleProgressBar.setProgress(progress);  //更新进度条
                    this.sendEmptyMessageDelayed(1,1);
                }
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    public void donghua(){
        AnimatorSet animatorSet = new AnimatorSet();
        ObjectAnimator ob1 = ObjectAnimator.ofFloat(tv_bw, "scaleX", new float[]{1f, 3f, 1f});
        ob1.setDuration(2000);
        ObjectAnimator ob2 = ObjectAnimator.ofFloat(tv_bw, "alpha", new float[]{1.0f,0.8f});
        ob2.setDuration(2000);
        ObjectAnimator ob3 = ObjectAnimator.ofFloat(tv_bw, "rotationX", new float[]{0f, 360f});
        ob3.setDuration(2000);
        animatorSet.playTogether(ob1,ob2,ob3);
        animatorSet.start();
    }

    private void initView() {
        circleProgressBar = findViewById(R.id.circle_progress);
        circleProgressBar.setOnClickListener(this);

        tv_bw = findViewById(R.id.tv_bw);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.circle_progress:
                handler.sendEmptyMessageDelayed(1, 1);

                donghua();
                new Handler().postDelayed(new Runnable(){
                    public void run() {
                        //execute the task
                        Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }, 2000);
                break;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值