自定义view进度条

主页面的xml

<?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="com.bwie.xiaqin.yuekao112102lx.zidingyi.JindutiaoActivity">

    <TextView
        android:id="@+id/text111"
        android:textSize="30sp"
        android:text="八维星空"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="125dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<com.bwie.xiaqin.yuekao112102lx.zidingyi.MyCircleProgress
    android:id="@+id/MyCircleProgress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="200dp"
    app:r="85"
    app:strokeWidth="5"
    app:bgColor="#cccccc"
    app:fgColor="#ff0000"
    app:drawStyle="STROKE"
    />

</RelativeLayout>

 

之后是主java

package com.bwie.xiaqin.yuekao112102lx.zidingyi;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.bwie.xiaqin.yuekao112102lx.MainActivity;
import com.bwie.xiaqin.yuekao112102lx.R;
import com.bwie.xiaqin.yuekao112102lx.fragment.FenFragment;
import com.bwie.xiaqin.yuekao112102lx.fragment.ShouFragment;

public class JindutiaoActivity extends AppCompatActivity {
    private MyCircleProgress progressView;
    private TextView mT1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jindutiao);
        mT1 = findViewById(R.id.text111);
        progressView = findViewById(R.id.MyCircleProgress);
        initAnmint();
        new ProgressAnimation().execute();

    }

    private void initAnmint() {
        //做缩放动画
        ObjectAnimator scale = ObjectAnimator.ofFloat(mT1, "scaleX", new float[]{0f,0.5f,1f,3f,1f});
        //做透明动画
        ObjectAnimator alpha = ObjectAnimator.ofFloat(mT1, "alpha", new float[]{0.2f, 0.4f, 0.6f, 0.8f, 1.0f});
        //做旋转动画
        ObjectAnimator rotationY = ObjectAnimator.ofFloat(mT1, "rotationX", new float[]{0f,90f, 160f, 270f, 360f});
        AnimatorSet set = new AnimatorSet();
        set.setDuration(5000);
        set.playTogether(scale,alpha,rotationY);
        set.start();
    }

    private class ProgressAnimation extends AsyncTask<Void,Integer,Void>{
        int i1=0;
        @Override
        protected Void doInBackground(Void... params) {
            //进度值不断变化
            for (int i = 0;i<=progressView.getMax();i++){
                //Log.e("Lixiaoliang", "wwwww"+i);
                try{
                    i1 = i;
                    publishProgress(i);
                    Thread.sleep(50);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (i1 == 100){
                                startActivity(new Intent(JindutiaoActivity.this, MainActivity.class));

                                finish();
                            }
                        }
                    });
                }catch (InterruptedException e){
                    e.printStackTrace();
                }


            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            //更新进度值
            progressView.setProgress(values[0]);
            progressView.invalidate();
            super.onProgressUpdate(values);
        }
    }
}

之后还有value里面的attrs

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CircleProgressBar">
        <attr name="bgColor" format="color"/>
        <attr name="fgColor" format="color"/>
        <attr name="r" format="integer"/>
        <attr name="strokeWidth" format="integer"/>
        <attr name="drawStyle">
            <enum name="STROKE" value="0"></enum>
            <enum name="FILL" value="1"></enum>
        </attr>
        <attr name="max" format="integer"/>
    </declare-styleable>
</resources>

 

之后是继承view的java

package com.bwie.xiaqin.yuekao112102lx.zidingyi;

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.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.bwie.xiaqin.yuekao112102lx.R;

/**
 * Created by lenovo on 2018/11/22.
 */

public class MyCircleProgress extends View {
    public int progress  = 0;//进度实际值,当前进度
    /**
     * 自定义控件属性,可灵活的设置圆形进度条的大小、颜色、类型等
     */
    private int mR;//圆半径,决定圆大小
    private int bgColor;//圆或弧的背景颜色
    private int fgColor;//圆或弧的前景颜色,即绘制时的颜色
    private int drawStyle; //绘制类型 FILL画圆形进度条,STROKE绘制弧形进度条
    private int strokeWidth;//STROKE绘制弧形的弧线的宽度
    private int max=100;//最大值,设置进度的最大值
    private Paint mPaint;
    private boolean opt;

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

    public MyCircleProgress(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyCircleProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray tArray = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressBar);
        mR=tArray.getInteger(R.styleable.CircleProgressBar_r,30);
        bgColor=tArray.getColor(R.styleable.CircleProgressBar_bgColor, Color.GRAY);
        fgColor=tArray.getColor(R.styleable.CircleProgressBar_fgColor, Color.RED);
        drawStyle=tArray.getInt(R.styleable.CircleProgressBar_drawStyle, 0);
        strokeWidth=tArray.getInteger(R.styleable.CircleProgressBar_strokeWidth, 10);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        //mPaint.setStyle(Paint.Style.STROKE);
        initProperty(attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int center = getWidth() / 2; // 圆心位置
        int height = getHeight();
        mPaint.setColor(bgColor);
        mPaint.setStrokeWidth(strokeWidth);
        canvas.drawCircle(center, center, mR, mPaint);
        // 绘制圆环
        mPaint.setColor(fgColor);
        if (drawStyle == 0){
            mPaint.setStyle(Paint.Style.STROKE);
            opt = false;
        }else {
            mPaint.setStyle(Paint.Style.STROKE);
            opt = true;
        }
        int top = (center - mR);
        int bottom = (center + mR);
        RectF oval = new RectF(top, top, bottom, bottom);
        canvas.drawArc(oval, 270, 360*progress/max, opt, mPaint);
        mPaint.setColor(Color.BLACK);
        String text=progress+"%";
        float stringWidth = mPaint.measureText(text);
        float x =(getWidth()-stringWidth)/2;
        //Log.e("Lixiaoliang", "小亮亮"+progress);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setTextSize(70f);
        canvas.drawText(text,x,height/2,mPaint);
    }
    private void initProperty(AttributeSet attrs){

    }

    /**
     * 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步
     */
    public synchronized void setProgress(int progress) {
        if(progress<0){
            progress=0;
        }else if(progress>max){
            progress=max;
        }else{
            this.progress = progress;
        }
    }
    public int getMax() {
        return max;    }
}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值