自定義進(jìn)度條動(dòng)起來(lái)

效果圖:

image.png

image.png

xml 布局:
···

<?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:ap="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="com.example.progress.MainActivity">

<LinearLayout
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:orientation="vertical">
    <com.example.progress.RoundProgress
        android:id="@+id/progress"
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:layout_marginTop="10dp"
        ap:roundColor="@color/colorPrimaryDark"
        ap:textColor="@color/colorPrimary"
        ap:textSize="20dp"
        ap:max="100"
        ap:progress="70"
        ap:roundProgressColor="@color/colorAccent"
        />

</LinearLayout>

</LinearLayout>

···
MainActivity 中代碼
···

package com.example.progress;

import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
private RoundProgress progress;
Runnable runnable=new Runnable() {
@Override
public void run() {
progress.setMax(100);
for (int i = 0; i <90 ; i++) {
progress.setProgress(i+1);
SystemClock.sleep(20);
//強(qiáng)制重繪
// progress.invalidate(); //只有主線程可以調(diào)用
progress.postInvalidate();//主線程 分線程都可以如此調(diào)用
}

   }

};

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

private void init() {
    progress = (RoundProgress) findViewById(R.id.progress);
   //分線程 實(shí)現(xiàn)進(jìn)度變化
    new Thread(runnable).start();

}

}

···
自定義類 RoundProgress 繼承View
具體代碼

···
package com.example.progress;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**

  • Created by shkstart on 2016/12/3 0003.

  • 自定義視圖
    */
    public class RoundProgress extends View {

    //設(shè)置繪制的圓環(huán)及文本的屬性---->使用自定義屬性替換
    // private int roundColor = Color.GRAY;//圓環(huán)的顏色
    // private int roundProgressColor = Color.RED;//圓弧的顏色
    // private int textColor = Color.BLUE;//文本的顏色
    //
    // private int roundWidth = UIUtils.dp2px(10);//圓環(huán)的寬度
    // private int textSize = UIUtils.dp2px(20);//文本的字體大小
    //
    // private int max = 100;//圓環(huán)的最大值
    // private int progress = 60;//圓環(huán)的進(jìn)度

    //使用自定義屬性來(lái)初始化如下的變量
    private int roundColor;//圓環(huán)的顏色
    private int roundProgressColor ;//圓弧的顏色
    private int textColor;//文本的顏色

    private float roundWidth ;//圓環(huán)的寬度
    private float textSize ;//文本的字體大小

    private int max;//圓環(huán)的最大值
    private int progress;//圓環(huán)的進(jìn)度

    private int width;//當(dāng)前視圖的寬度(=高度)

    private Paint paint;//畫筆

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

    public RoundProgress(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    }

    public RoundProgress(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

     //初始化畫筆
     paint = new Paint();
     paint.setAntiAlias(true);//去除毛邊
    
     //獲取自定義的屬性
     //1.獲取TypeArray的對(duì)象(調(diào)用兩個(gè)參數(shù)的方法)
     TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgress);
    
     //2.取出所有的自定義屬性
     roundColor = typedArray.getColor(R.styleable.RoundProgress_roundColor, Color.GRAY);
     roundProgressColor = typedArray.getColor(R.styleable.RoundProgress_roundProgressColor,Color.RED);
     textColor = typedArray.getColor(R.styleable.RoundProgress_textColor,Color.GREEN);
     roundWidth = typedArray.getDimension(R.styleable.RoundProgress_roundWith,UIUtils.dp2px(10));
     textSize = typedArray.getDimension(R.styleable.RoundProgress_textSize,UIUtils.dp2px(20));
     max = typedArray.getInteger(R.styleable.RoundProgress_max,100);
     progress = typedArray.getInteger(R.styleable.RoundProgress_progress,30);
    
     //3.回收處理
     typedArray.recycle();
    

    }

    public int getMax() {
    return max;
    }

    public void setMax(int max) {
    this.max = max;
    }

    public int getProgress() {
    return progress;
    }

    public void setProgress(int progress) {
    this.progress = progress;
    }

    //測(cè)量:獲取當(dāng)前視圖寬高
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    width = this.getMeasuredWidth();
    }

    //canvas:畫布,對(duì)應(yīng)著視圖在布局中的范圍區(qū)間。范圍的左上頂點(diǎn)即為坐標(biāo)原點(diǎn)
    @Override
    protected void onDraw(Canvas canvas) {
    //1.繪制圓環(huán)
    //獲取圓心坐標(biāo)
    int cx = width / 2;
    int cy = width / 2;
    float radius = width / 2 - roundWidth / 2;
    paint.setColor(roundColor);//設(shè)置畫筆顏色
    paint.setStyle(Paint.Style.STROKE);//設(shè)置圓環(huán)的樣式
    paint.setStrokeWidth(roundWidth);//設(shè)置圓環(huán)的寬度
    canvas.drawCircle(cx, cy, radius, paint);

     //2.繪制圓弧
     RectF rectF = new RectF(roundWidth / 2, roundWidth / 2, width - roundWidth / 2, width - roundWidth / 2);
     paint.setColor(roundProgressColor);//設(shè)置畫筆顏色
     canvas.drawArc(rectF,0,progress * 360 / max ,false,paint);
    
     //3.繪制文本
     String text = progress * 100 / max  + "%";
     //設(shè)置paint
     paint.setColor(textColor);
     paint.setTextSize(textSize);
     paint.setStrokeWidth(0);
     Rect rect = new Rect();//創(chuàng)建了一個(gè)矩形,此時(shí)矩形沒有具體的寬度和高度
     paint.getTextBounds(text,0,text.length(),rect);//此時(shí)的矩形的寬度和高度即為整好包裹文本的矩形的寬高
     //獲取左下頂點(diǎn)的坐標(biāo)
     int x = width / 2 - rect.width() / 2;
     int y = width / 2 + rect.height() / 2;
    
    
     canvas.drawText(text,x,y,paint);
    

    }
    }

···
MyApplication類
···

package com.example.progress;
import android.app.Application;
import android.content.Context;
import android.os.Handler;

/**

  • Created by shkstart on 2016/12/2 0002.
    */
    public class MyApplication extends Application {

    //在整個(gè)應(yīng)用執(zhí)行過(guò)程中,需要提供的變量
    public static Context context;//需要使用的上下文對(duì)象:application實(shí)例
    public static Handler handler;//需要使用的handler
    public static Thread mainThread;//提供主線程對(duì)象
    public static int mainThreadId;//提供主線程對(duì)象的id

    @Override
    public void onCreate() {
    super.onCreate();

     context = this.getApplicationContext();
     handler = new Handler();
     mainThread = Thread.currentThread();//實(shí)例化當(dāng)前Application的線程即為主線程
     mainThreadId = android.os.Process.myTid();//獲取當(dāng)前線程的id
    
     //設(shè)置未捕獲異常的處理器
    

// CrashHandler.getInstance().init();
//初始化ShareSDK

}

}

···
UiUtils類
···

package com.example.progress;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;

/**

  • 專門提供為處理一些UI相關(guān)的問(wèn)題而創(chuàng)建的工具類,

  • 提供資源獲取的通用方法,避免每次都寫重復(fù)的代碼獲取結(jié)果。
    */
    public class UIUtils {

    public static Context getContext(){
    return MyApplication.context;
    }

    public static Handler getHandler(){
    return MyApplication.handler;
    }

    //返回指定colorId對(duì)應(yīng)的顏色值
    public static int getColor(int colorId){
    return getContext().getResources().getColor(colorId);
    }

    //加載指定viewId的視圖對(duì)象,并返回
    public static View getView(int viewId){
    View view = View.inflate(getContext(), viewId, null);
    return view;
    }

    public static String[] getStringArr(int strArrId){
    String[] stringArray = getContext().getResources().getStringArray(strArrId);
    return stringArray;
    }

    //將dp轉(zhuǎn)化為px
    public static int dp2px(int dp){
    //獲取手機(jī)密度
    float density = getContext().getResources().getDisplayMetrics().density;
    return (int) (dp * density + 0.5);//實(shí)現(xiàn)四舍五入
    }

    public static int px2dp(int px){
    //獲取手機(jī)密度
    float density = getContext().getResources().getDisplayMetrics().density;
    return (int) (px / density + 0.5);//實(shí)現(xiàn)四舍五入
    }

    //保證runnable中的操作在主線程中執(zhí)行
    public static void runOnUiThread(Runnable runnable) {
    if(isInMainThread()){
    runnable.run();
    }else{
    UIUtils.getHandler().post(runnable);
    }
    }
    //判斷當(dāng)前線程是否是主線程
    private static boolean isInMainThread() {
    int currentThreadId = android.os.Process.myTid();
    return MyApplication.mainThreadId == currentThreadId;

    }

    public static void toast(String message,boolean isLengthLong){
    Toast.makeText(UIUtils.getContext(), message,isLengthLong? Toast.LENGTH_LONG : Toast.LENGTH_SHORT).show();
    }
    }

···

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,786評(píng)論 6 534
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,656評(píng)論 3 419
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人,你說(shuō)我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,697評(píng)論 0 379
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我,道長(zhǎng),這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,098評(píng)論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,855評(píng)論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,254評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,322評(píng)論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,473評(píng)論 0 289
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,014評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,833評(píng)論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,016評(píng)論 1 371
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,568評(píng)論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,273評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,680評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,946評(píng)論 1 288
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,730評(píng)論 3 393
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,006評(píng)論 2 374

推薦閱讀更多精彩內(nèi)容