自定義View:01-步數(shù)進(jìn)度條

1、仿運(yùn)動(dòng)的步數(shù)條柬讨,效果圖如下


步數(shù)圖.gif

一崩瓤、自定義屬性
1.1、內(nèi)圓弧踩官、外圓弧的顏色
1.2却桶、字體、圓弧的大小
二蔗牡、繼承View
2.1颖系、onMeasure()方法測(cè)量設(shè)置view的大小
2.2畴嘶、onDraw() 繪制內(nèi)外圓弧、文字
三集晚、使用
3.1窗悯、布局中使用自定義的view,同時(shí)加上自定義的屬性
3.2偷拔、調(diào)用 View設(shè)置步數(shù)的方法,同時(shí)加上屬性動(dòng)畫(huà)

2蒋院、實(shí)現(xiàn)

2.1、自定義屬性:

沒(méi)有attrs.xml 文件就新建一個(gè)


image.png

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--步數(shù)圓環(huán)-->
    <declare-styleable name="stepView">
        <!--1莲绰、外圓弧顏色-->
        <attr name="outer" format="color" />
        <!--2欺旧、內(nèi)圓弧顏色-->
        <attr name="inside" format="color" />
        <!--3、字體大小-->
        <attr name="textSize" format="dimension" />
        <!--4蛤签、圓弧大小-->
        <attr name="radian" format="dimension" />

    </declare-styleable>
</resources>

2.2辞友、新建類(lèi)StepView_01,繼承View

public class StepView_01 extends View {

    private Paint mPaint = new Paint();
    private int outerColor = Color.WHITE;
    private int insideColor = Color.RED;
    private int textSize = 15;
    private int radianSize = 30;

    private int mCurrentStep = 0;//當(dāng)前步數(shù)
    private int mMaxSteps = 5000; //總步數(shù)

    public StepView_01(Context context) {
        super(context);
    }


    public StepView_01(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public StepView_01(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
      TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.stepView);
      outerColor = array.getColor(R.styleable.stepView_outer, outerColor);
       insideColor = array.getColor(R.styleable.stepView_inside, insideColor);
       textSize = array.getInteger(R.styleable.stepView_textSize, textSize);
       radianSize = array.getInteger(R.styleable.stepView_radian, radianSize);
        array.recycle();
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int startAngle = 135; //起始弧度
        int angle = 270; //劃過(guò)的弧度

        // 設(shè)置圓弧畫(huà)筆的寬度
        mPaint.setStrokeWidth(30);
        // 設(shè)置為 線頭為 圓角
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        // 設(shè)置畫(huà)筆顏色
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.STROKE);

        mPaint.setAntiAlias(true);

        int centerX = getWidth() / 2;//中心點(diǎn)
        int centerY = getHeight() / 2;//中心點(diǎn)
        int radius = centerX / 2;    // 半徑

        // 1.畫(huà)背景大圓弧
        RectF outer = new RectF(radius, centerY - radius, centerX + radius, centerY + radius);
        canvas.drawArc(outer, startAngle, angle, false, mPaint);


//      2.畫(huà)內(nèi)圓弧
        mPaint.setColor(Color.BLUE);
        RectF inside = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius);
        if (mMaxSteps == 0) return;
        float sweepAngle = (float) mCurrentStep / mMaxSteps;

        canvas.drawArc(inside, startAngle, sweepAngle * 270, false, mPaint);

//        3.中心文字
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setTextSize(50);

        String stepText = mCurrentStep + "";
        Rect textBounds = new Rect();
        mPaint.getTextBounds(stepText, 0, stepText.length(), textBounds);
        int dx = getWidth() / 2 - textBounds.width() / 2 - radianSize/2;
        canvas.drawText(stepText, dx, centerY + 30, mPaint);
    }

/**
*測(cè)量View
**/
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(width, height);
    }

    //設(shè)置最大步數(shù)值
    public  void setStepMax(int stepMax){
        this.mMaxSteps=stepMax;
    };

    //設(shè)置當(dāng)前步數(shù)值
    public  void setCurrentStep(int step){
        this.mCurrentStep=step;
        //不斷繪制
        invalidate();
    };
}

2.3震肮、使用
view_setp_01.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#CCCACA"
    android:layout_height="match_parent">

    <com.example.view_day01.View.StepView_01
        android:id="@+id/stepView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <EditText
        android:id="@+id/stepView_edit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="55dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="55dp"
        android:layout_marginBottom="31dp"
        android:hint="當(dāng)前步數(shù)"
        app:layout_constraintBottom_toTopOf="@+id/stepView_btn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/stepView"
        app:layout_constraintVertical_bias="0.978" />

    <Button
        android:id="@+id/stepView_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="72dp"
        android:text="啟動(dòng)"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

fragment :

public class StepFragment_01 extends Fragment {
    private EditText editText;
    private StepView_01 stepView;
    private static final String TAG = "StepFragment_01";
    private Button button;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_step_01, container, false);
        stepView = view.findViewById(R.id.stepView);
        editText = view.findViewById(R.id.stepView_edit);
        button = view.findViewById(R.id.stepView_btn);

        stepView.setStepMax(5000);//設(shè)置最大步數(shù)

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAnimator();
            }
        });

        return view;
    }

    //通過(guò)屬性動(dòng)畫(huà)調(diào)用自定義view的方法称龙,1.5秒內(nèi)繪制出進(jìn)度條
    private void setAnimator() {
        Log.d(TAG, "setAnimator: " + Integer.parseInt(editText.getText().toString()));
        ValueAnimator valueAnimator = ObjectAnimator.ofFloat(0, Integer.parseInt(editText.getText().toString())); //0-**
        valueAnimator.setDuration(1500);//需要多少秒
        valueAnimator.setInterpolator(new DecelerateInterpolator());//插值器
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float step = (float) animation.getAnimatedValue();
                Log.d(TAG, "onAnimationUpdate: " + (int) step);
                stepView.setCurrentStep((int) step);
            }
        });
        valueAnimator.start();
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市戳晌,隨后出現(xiàn)的幾起案子鲫尊,更是在濱河造成了極大的恐慌,老刑警劉巖沦偎,帶你破解...
    沈念sama閱讀 218,755評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件疫向,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡豪嚎,警方通過(guò)查閱死者的電腦和手機(jī)搔驼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)侈询,“玉大人舌涨,你說(shuō)我怎么就攤上這事⊥螅” “怎么了泼菌?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,138評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)啦租。 經(jīng)常有香客問(wèn)我哗伯,道長(zhǎng),這世上最難降的妖魔是什么篷角? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,791評(píng)論 1 295
  • 正文 為了忘掉前任焊刹,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘虐块。我一直安慰自己俩滥,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,794評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布贺奠。 她就那樣靜靜地躺著霜旧,像睡著了一般。 火紅的嫁衣襯著肌膚如雪儡率。 梳的紋絲不亂的頭發(fā)上挂据,一...
    開(kāi)封第一講書(shū)人閱讀 51,631評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音儿普,去河邊找鬼崎逃。 笑死,一個(gè)胖子當(dāng)著我的面吹牛眉孩,可吹牛的內(nèi)容都是我干的个绍。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼浪汪,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼巴柿!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起吟宦,我...
    開(kāi)封第一講書(shū)人閱讀 39,264評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤篮洁,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后殃姓,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡瓦阐,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年蜗侈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片睡蟋。...
    茶點(diǎn)故事閱讀 40,040評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡踏幻,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出戳杀,到底是詐尸還是另有隱情该面,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評(píng)論 5 346
  • 正文 年R本政府宣布信卡,位于F島的核電站隔缀,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏傍菇。R本人自食惡果不足惜猾瘸,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,364評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧牵触,春花似錦淮悼、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,944評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至钉汗,卻和暖如春瞧挤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背儡湾。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,060評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工特恬, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人徐钠。 一個(gè)月前我還...
    沈念sama閱讀 48,247評(píng)論 3 371
  • 正文 我出身青樓癌刽,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親尝丐。 傳聞我的和親對(duì)象是個(gè)殘疾皇子显拜,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,979評(píng)論 2 355