欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android仿字節(jié)顏色自定義進(jìn)度條

 更新時間:2021年08月09日 08:35:46   作者:我想月薪過萬  
這篇文章主要為大家詳細(xì)介紹了Android仿字節(jié)顏色自定義進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android仿字節(jié)顏色自定義進(jìn)度條的具體代碼,供大家參考,具體內(nèi)容如下

效果展示

代碼實現(xiàn)

第一步:編寫自定義屬性

res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyLoadingView">
        <attr name="loading_color_one" format="color"/>
        <attr name="loading_color_two" format="color"/>
        <attr name="loading_color_three" format="color"/>
        <attr name="loading_color_four" format="color"/>
        <attr name="loading_color_five" format="color"/>
    </declare-styleable>
</resources>

第二步:編寫自定義java類

package com.wust.jingdutiao;
 
import android.animation.ValueAnimator;
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.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.Animation;
 
import androidx.annotation.Nullable;
 
/**
 * ClassName: MyLodingView <br/>
 * Description: <br/>
 * date: 2021/7/21 15:59<br/>
 *
 * @author yiqi<br />
 * @QQ 1820762465
 * @微信 yiqiideallife
 * @技術(shù)交流QQ群 928023749
 */
public class MyLoadingView extends View {
 
    private int rect_color_one;
    private int rect_color_two;
    private int rect_color_three;
    private int rect_color_four;
    private int rect_color_five;
    private Paint rect_one_paint;
    private Paint rect_two_paint;
    private Paint rect_three_paint;
    private Paint rect_four_paint;
    private Paint rect_five_paint;
    private int mWidth;
    private int mHeight;
    private float[] mHeightRate = {1/16.0f,1/10.0f,1/8.0f};
    private int HORIZONTAL_OFFSET = 5;
    private int bg_default_color;
    private ValueAnimator va;
 
    public MyLoadingView(Context context) {
        super(context);
    }
 
    public MyLoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
        initPaint();
        initAnima();
    }
 
    public MyLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
        initPaint();
        initAnima();
    }
 
    //設(shè)置 屬性動畫
    private void initAnima() {
        va = ValueAnimator.ofInt(0, 4);
        va.setDuration(3000);
        va.setRepeatCount(ValueAnimator.INFINITE);
        va.setRepeatMode(ValueAnimator.RESTART);
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                setRectColorByNum(value);
            }
        });
        postDelayed(new Runnable() {
            @Override
            public void run() {
                va.start();
            }
        },500);
    }
 
    private void initAttrs(Context context, AttributeSet attrs) {
        //獲取用戶傳來的五種顏色
        TypedArray ty = context.obtainStyledAttributes(attrs, R.styleable.MyLoadingView);
 
        rect_color_one = ty.getColor(R.styleable.MyLoadingView_loading_color_one, Color.parseColor("#325AB4"));
        rect_color_two = ty.getColor(R.styleable.MyLoadingView_loading_color_two, Color.parseColor("#3C8CFF"));
        rect_color_three = ty.getColor(R.styleable.MyLoadingView_loading_color_three, Color.parseColor("#888888"));
        rect_color_four = ty.getColor(R.styleable.MyLoadingView_loading_color_four, Color.parseColor("#00C8D2"));
        rect_color_five = ty.getColor(R.styleable.MyLoadingView_loading_color_five, Color.parseColor("#78E6DC"));
 
        //獲取背景色
        try {
            ColorDrawable bg = (ColorDrawable) getBackground();
            bg_default_color = bg.getColor();
        }catch (Exception e){
            bg_default_color = Color.WHITE;
        }
 
        ty.recycle();
    }
 
    //初始化畫筆
    private void initPaint() {
        rect_one_paint = getPaintByColor(rect_color_one);
        rect_two_paint = getPaintByColor(rect_color_two);
        rect_three_paint = getPaintByColor(rect_color_three);
        rect_four_paint = getPaintByColor(rect_color_four);
        rect_five_paint = getPaintByColor(rect_color_five);
    }
 
    private Paint getPaintByColor(int Color) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(Color);
        return paint;
    }
 
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
        //讓其為正方形,并且寬高不能小于40
        mWidth = mHeight = Math.max(Math.min(mWidth, mHeight),dp2px(100));
        setMeasuredDimension(mWidth, mHeight);
    }
 
    private int dp2px(int value) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value,getResources().getDisplayMetrics());
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //繪制矩形
        drawRect(canvas);
 
    }
 
    private void drawRect(Canvas canvas) {
        int centerX = mWidth/2;
        int centerY = mHeight/2;
        RectF rectOne = new RectF(centerX-HORIZONTAL_OFFSET,
                centerY-mWidth*mHeightRate[0],
                centerX+HORIZONTAL_OFFSET,
                centerY+mWidth*mHeightRate[0]);
        canvas.drawRoundRect(rectOne,5,5,rect_one_paint);
 
        RectF rectTwo = new RectF(centerX+HORIZONTAL_OFFSET*3,
                centerY-mWidth*mHeightRate[1],
                centerX+HORIZONTAL_OFFSET*5,
                centerY+mWidth*mHeightRate[1]);
        canvas.drawRoundRect(rectTwo,5,5,rect_two_paint);
 
        RectF rectThree = new RectF(centerX-HORIZONTAL_OFFSET*3,
                centerY-mWidth*mHeightRate[1],
                centerX-HORIZONTAL_OFFSET*5,
                centerY+mWidth*mHeightRate[1]);
        canvas.drawRoundRect(rectThree,5,5,rect_three_paint);
 
        RectF rectFour = new RectF(centerX+HORIZONTAL_OFFSET*7,
                centerY-mWidth*mHeightRate[2],
                centerX+HORIZONTAL_OFFSET*9,
                centerY+mWidth*mHeightRate[2]);
        canvas.drawRoundRect(rectFour,5,5,rect_four_paint);
 
        RectF rectFive = new RectF(centerX-HORIZONTAL_OFFSET*7,
                centerY-mWidth*mHeightRate[2],
                centerX-HORIZONTAL_OFFSET*9,
                centerY+mWidth*mHeightRate[2]);
        canvas.drawRoundRect(rectFive,5,5,rect_five_paint);
    }
 
    //根據(jù)屬性動畫的 變化的值 給畫筆換不同的顏色
    private void setRectColorByNum(int num){
        if (num == 0){
            rect_one_paint.setColor(rect_color_one);
            rect_two_paint.setColor(bg_default_color);
            rect_three_paint.setColor(bg_default_color);
            rect_four_paint.setColor(bg_default_color);
            rect_five_paint.setColor(bg_default_color);
        }else if (num == 1){
            rect_one_paint.setColor(bg_default_color);
            rect_two_paint.setColor(rect_color_two);
            rect_three_paint.setColor(rect_color_three);
            rect_four_paint.setColor(bg_default_color);
            rect_five_paint.setColor(bg_default_color);
        }else if (num == 2){
            rect_one_paint.setColor(bg_default_color);
            rect_two_paint.setColor(bg_default_color);
            rect_three_paint.setColor(bg_default_color);
            rect_four_paint.setColor(rect_color_four);
            rect_five_paint.setColor(rect_color_five);
        }else if (num == 3){
            rect_one_paint.setColor(rect_color_one);
            rect_two_paint.setColor(rect_color_two);
            rect_three_paint.setColor(rect_color_three);
            rect_four_paint.setColor(rect_color_four);
            rect_five_paint.setColor(rect_color_five);
        }
        invalidate();
    }
}

第三步:使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:gravity="center">
 
    <com.wust.jingdutiao.MyLoadingView
        android:layout_width="100dp"
        android:layout_height="100dp"/>
 
</LinearLayout>

到此為止,效果便可以完美實現(xiàn)了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python的異常概念介紹以及處理

    Python的異常概念介紹以及處理

    本篇文章給大家分享了關(guān)于Python異常的相關(guān)概念知識點以及處理方法,對此有需要的朋友趕快學(xué)習(xí)下吧。
    2018-03-03
  • android中Context深入詳解

    android中Context深入詳解

    本文主要給大家深入的講解一下android中Context知識以及繼承關(guān)系,有助于大家更好的理解Context。
    2017-11-11
  • Android仿支付寶手勢密碼解鎖功能

    Android仿支付寶手勢密碼解鎖功能

    這篇文章主要為大家詳細(xì)介紹了Android仿支付寶手勢密碼解鎖功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Flutter啟動頁(閃屏頁)的具體實現(xiàn)及原理詳析

    Flutter啟動頁(閃屏頁)的具體實現(xiàn)及原理詳析

    這篇文章主要給大家介紹了關(guān)于Flutter啟動頁(閃屏頁)的具體實現(xiàn)及原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • 利用Android實現(xiàn)比較炫酷的自定義View

    利用Android實現(xiàn)比較炫酷的自定義View

    自定義View、多線程、網(wǎng)絡(luò),被認(rèn)為是Android開發(fā)者必須牢固掌握的最基礎(chǔ)的三大基本功,這篇文章主要給大家介紹了關(guān)于如何利用Android實現(xiàn)比較炫酷的自定義View的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • 實例講解Android中的View類以及自定義View控件的方法

    實例講解Android中的View類以及自定義View控件的方法

    這篇文章主要介紹了Android中的View類以及自定義View控件的方法,講解了如何繼承View類并且展示了一個對View進(jìn)行重繪的例子,需要的朋友可以參考下
    2016-04-04
  • Android中EditText光標(biāo)的顯示與隱藏方法

    Android中EditText光標(biāo)的顯示與隱藏方法

    這篇文章主要給大家介紹了關(guān)于Android中EditText光標(biāo)的顯示與隱藏以及Android之第一次不顯示EditText光標(biāo)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-11-11
  • 詳解Android單元測試最佳實踐

    詳解Android單元測試最佳實踐

    這篇文章主要介紹了詳解Android單元測試最佳實踐,本文介紹了如何對Android原生應(yīng)用進(jìn)行單元測試,同時示例代碼采用MVP模式以提高代碼的可讀性和可測試性
    2018-08-08
  • Android中的應(yīng)用認(rèn)領(lǐng)總結(jié)

    Android中的應(yīng)用認(rèn)領(lǐng)總結(jié)

    這篇文章主要介紹了Android中的應(yīng)用認(rèn)領(lǐng)總結(jié),本文講解了如何認(rèn)領(lǐng)、對未簽名包簽名、需要替換的簽名值、驗證簽名等內(nèi)容,需要的朋友可以參考下
    2015-01-01
  • Flutter實現(xiàn)心動的動畫特效

    Flutter實現(xiàn)心動的動畫特效

    為了追求更好的用戶體驗,有時候我們需要一個類似心跳一樣跳動著的控件來吸引用戶的注意力。本文將利用Flutter實現(xiàn)這一動畫特效,需要的可以參考一下
    2022-04-04

最新評論