Android自定義帶圓點(diǎn)的半圓形進(jìn)度條
本文實(shí)例為大家分享了Android自定義帶圓點(diǎn)的半圓形進(jìn)度條,供大家參考,具體內(nèi)容如下
僅限用于半圓形,如須要帶圓點(diǎn)的圓形進(jìn)度條,圓點(diǎn)會(huì)出現(xiàn)錯(cuò)位現(xiàn)象,此代碼僅供,帶圓點(diǎn)的圓形進(jìn)度條有空研究一下!圖片效果在下方,
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.util.AttributeSet;
import android.view.View;
/**
* 自定義帶圓點(diǎn)的進(jìn)度條
*/
public class HalfProgressBar extends View{
private int maxProgress = 100;
//設(shè)置進(jìn)度條背景寬度
private float progressStrokeWidth = 3;
//設(shè)置進(jìn)度條進(jìn)度寬度
private float marxArcStorkeWidth = 6;
//設(shè)置進(jìn)度條圓點(diǎn)的寬度
private float circularDotWidth=15;
/**
* 畫筆對(duì)象的引用
*/
private Paint paint;
public synchronized int getProgress() {
return progress;
}
/**
* Android提供了Invalidate方法實(shí)現(xiàn)界面刷新,但是Invalidate不能直接在線程中調(diào)用,因?yàn)樗沁`背了單線程模型:Android UI操作并不是線程安全的,并且這些操作必須在UI線程中調(diào)用。
* 而postInvalidate()在工作者線程中被調(diào)用 使用postInvalidate則比較簡(jiǎn)單,不需要handler,直接在線程中調(diào)用postInvalidate即可。
* @param progress 傳過來的進(jìn)度
*/
public void setProgress(int progress) {
if (progress < 0) {
progress = 0;
}
if (progress > maxProgress) {
progress = maxProgress;
}
if (progress <= maxProgress) {
this.progress = progress;
postInvalidate();
}
}
/**
* 當(dāng)前進(jìn)度
*/
private int progress = 99;
private RectF oval;
private int roundProgressColor;
private int roundColor;
private int circularDotColor;
public HalfProgressBar(Context context) {
super(context);
}
public HalfProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
oval = new RectF();
//這是自定義view 必須要寫的
TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.HalfProgressBar);
roundProgressColor = mTypedArray.getColor(R.styleable.HalfProgressBar_roundProgressColor1, Color.YELLOW);
roundColor=mTypedArray.getColor(R.styleable.HalfProgressBar_roundColor1, Color.YELLOW);
circularDotColor=mTypedArray.getColor(R.styleable.HalfProgressBar_circularDotColor1, Color.YELLOW);
}
public HalfProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint = new Paint();
oval = new RectF();
TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.HalfProgressBar);
roundProgressColor = mTypedArray.getColor(R.styleable.HalfProgressBar_roundProgressColor1, Color.YELLOW);
roundColor=mTypedArray.getColor(R.styleable.HalfProgressBar_roundColor1, Color.YELLOW);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO 自動(dòng)生成的方法存根
super.onDraw(canvas);
float width = getWidth();
float height = getHeight();
paint.setAntiAlias(false); // 設(shè)置畫筆為抗鋸齒
paint.setColor(roundColor); // 設(shè)置畫筆顏色
paint.setStrokeWidth(progressStrokeWidth); // 線寬
paint.setStyle(Paint.Style.STROKE);
oval.left = marxArcStorkeWidth / 2; // 左上角x
oval.top = circularDotWidth; // 左上角y
oval.right = width - circularDotWidth / 2; // 左下角x
oval.bottom = width - circularDotWidth / 2; // 右下角y
float bangjing = ((width - circularDotWidth/2) / 2);//半徑
//調(diào)整圓背景的大小
canvas.drawArc(oval, 180, 180, false, paint); // 繪制紅絲圓圈,即進(jìn)度條背景
//進(jìn)度條顏色
paint.setColor(roundProgressColor);
paint.setStrokeWidth(marxArcStorkeWidth);
canvas.drawArc(oval, 180, 180 * ((float) progress / (float) maxProgress), false, paint); // 繪制進(jìn)度圓弧,這里是藍(lán)色
//畫圓點(diǎn)
paint.setColor(circularDotColor);
paint.setAntiAlias(true); // 設(shè)置畫筆為抗鋸齒
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(circularDotWidth);
//當(dāng)畫筆樣式為STROKE或FILL_OR_STROKE時(shí),設(shè)置筆刷的圖形樣式,如圓形樣式Cap.ROUND,或方形樣式Cap.SQUARE
paint.setStrokeCap(Paint.Cap.ROUND);
float jindu = ((float) progress * 1.8f);
canvas.drawPoint(bangjing - ((float) (Math.sin((Math.PI / (double) 180) * (jindu <= 90 ? 90 - (jindu) : -jindu + 90))) * bangjing),
bangjing+circularDotWidth - ((float) (Math.cos((Math.PI / (double) 180) * (double) (jindu <= 90 ? 90 - jindu : -jindu + 90))) * bangjing), paint);
}
}
attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!--自定義半圓形加載進(jìn)度條--> <declare-styleable name="HalfProgressBar"> <attr name="roundColor1" format="color"/> <attr name="roundProgressColor1" format="color"/> <attr name="circularDotColor1" format="color"/> </declare-styleable> </resources>
xml中
<com.jyc99.demo.HalfProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/view" android:layout_centerHorizontal="true" android:layout_marginTop="42dp" android_custom:roundColor1="#fc422b" android_custom:roundProgressColor1="#fa432e" android_custom:circularDotColor1="#246223"/>
由于截圖的原因可能看不到圓點(diǎn) , 大家自己試試調(diào)調(diào)顏色 調(diào)整一下高度寬度

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android常用正則表達(dá)式驗(yàn)證工具類(實(shí)例代碼)
正則表達(dá)式,相信接觸過編程的人都知道,但是大部分人應(yīng)該是每次用的時(shí)候現(xiàn)找,但對(duì)其語法應(yīng)該只是一知半解 。下面小編給大家分享Android常用正則表達(dá)式驗(yàn)證工具類,感興趣的朋友一起看看吧2017-10-10
Android開發(fā)實(shí)現(xiàn)SubMenu選項(xiàng)菜單和子菜單示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)SubMenu選項(xiàng)菜單和子菜單,結(jié)合實(shí)例形式分析了Android開發(fā)中SubMenu選項(xiàng)菜單和子菜單的功能、配置、布局等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
Kotlin利用Regex如何構(gòu)建正則表達(dá)式詳解
正則表達(dá)式,又稱規(guī)則表達(dá)式。下面這篇文章主要給大家介紹了關(guān)于Kotlin利用Regex構(gòu)建正則表達(dá)式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-12-12
Android如何實(shí)現(xiàn)時(shí)間線效果(下)
上一篇文章我們講了Android如何實(shí)現(xiàn)時(shí)間線效果,今天計(jì)息上一回的文章圍繞Android實(shí)現(xiàn)時(shí)間線效果內(nèi)容展開更多,需要的朋友可以參考一下2021-11-11
Android自定義SeekBar實(shí)現(xiàn)視頻播放進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義SeekBar實(shí)現(xiàn)視頻播放進(jìn)度條的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
Android實(shí)現(xiàn)帶頁面切換的鎖屏功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶頁面切換的鎖屏功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android快速實(shí)現(xiàn)無預(yù)覽拍照功能
這篇文章主要為大家詳細(xì)介紹了Android快速實(shí)現(xiàn)無預(yù)覽拍照功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
Android開發(fā)中的Surface庫及用其制作播放器UI的例子
這篇文章主要介紹了Android開發(fā)中的Surface庫及用其制作播放器界面的例子,利用SurfaceView和SurfaceHolder可以高效地繪制和控制圖形界面,需要的朋友可以參考下2016-04-04
Android編程之ActionBar Tabs用法實(shí)例分析
這篇文章主要介紹了Android編程之ActionBar Tabs用法,結(jié)合實(shí)例形式分析了ActionBar Tabs的功能及Tab切換不同的Fragment的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-03-03
Android?OpenCV基礎(chǔ)API清晰度亮度識(shí)別檢測(cè)
這篇文章主要為大家介紹了Android?OpenCV基礎(chǔ)API清晰度亮度識(shí)別檢測(cè),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

