Android自定義帶圓點的半圓形進度條
本文實例為大家分享了Android自定義帶圓點的半圓形進度條,供大家參考,具體內(nèi)容如下
僅限用于半圓形,如須要帶圓點的圓形進度條,圓點會出現(xiàn)錯位現(xià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;
/**
* 自定義帶圓點的進度條
*/
public class HalfProgressBar extends View{
private int maxProgress = 100;
//設置進度條背景寬度
private float progressStrokeWidth = 3;
//設置進度條進度寬度
private float marxArcStorkeWidth = 6;
//設置進度條圓點的寬度
private float circularDotWidth=15;
/**
* 畫筆對象的引用
*/
private Paint paint;
public synchronized int getProgress() {
return progress;
}
/**
* Android提供了Invalidate方法實現(xiàn)界面刷新,但是Invalidate不能直接在線程中調(diào)用,因為他是違背了單線程模型:Android UI操作并不是線程安全的,并且這些操作必須在UI線程中調(diào)用。
* 而postInvalidate()在工作者線程中被調(diào)用 使用postInvalidate則比較簡單,不需要handler,直接在線程中調(diào)用postInvalidate即可。
* @param progress 傳過來的進度
*/
public void setProgress(int progress) {
if (progress < 0) {
progress = 0;
}
if (progress > maxProgress) {
progress = maxProgress;
}
if (progress <= maxProgress) {
this.progress = progress;
postInvalidate();
}
}
/**
* 當前進度
*/
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 自動生成的方法存根
super.onDraw(canvas);
float width = getWidth();
float height = getHeight();
paint.setAntiAlias(false); // 設置畫筆為抗鋸齒
paint.setColor(roundColor); // 設置畫筆顏色
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); // 繪制紅絲圓圈,即進度條背景
//進度條顏色
paint.setColor(roundProgressColor);
paint.setStrokeWidth(marxArcStorkeWidth);
canvas.drawArc(oval, 180, 180 * ((float) progress / (float) maxProgress), false, paint); // 繪制進度圓弧,這里是藍色
//畫圓點
paint.setColor(circularDotColor);
paint.setAntiAlias(true); // 設置畫筆為抗鋸齒
paint.setStyle(Paint.Style.FILL);
paint.setStrokeWidth(circularDotWidth);
//當畫筆樣式為STROKE或FILL_OR_STROKE時,設置筆刷的圖形樣式,如圓形樣式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> <!--自定義半圓形加載進度條--> <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ào)調(diào)顏色 調(diào)整一下高度寬度

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
RecyclerView實現(xiàn)側滑和網(wǎng)絡斷點續(xù)傳
這篇文章主要為大家詳細介紹了RecyclerView實現(xiàn)側滑和網(wǎng)絡斷點續(xù)傳,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07
Android Lottie實現(xiàn)中秋月餅變明月動畫特效實例
Lottie是Airbnb開源的一個支持 Android、iOS 以及 ReactNative,利用json文件的方式快速實現(xiàn)動畫效果的庫,下面這篇文章主要給大家介紹了關于Android Lottie實現(xiàn)中秋月餅變明月動畫特效的相關資料,需要的朋友可以參考下2021-09-09
Android recyclerview實現(xiàn)縱向虛線時間軸的示例代碼
本文主要介紹了Android recyclerview實現(xiàn)縱向虛線時間軸的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07
房卡麻將分析系列 "牌局回放" 之 數(shù)據(jù)設計詳解及實例
這篇文章主要介紹了房卡麻將分析系列 "牌局回放" 之 數(shù)據(jù)設計詳解及實例的相關資料,需要的朋友可以參考下2017-03-03
Kotlin語言使用BroadcastReceiver示例介紹
Android開發(fā)的四大組件分別是:活動(activity),用于表現(xiàn)功能;服務(service),后臺運行服務,不提供界面呈現(xiàn);廣播接受者(Broadcast Receive),勇于接收廣播;內(nèi)容提供者(Content Provider),支持多個應用中存儲和讀取數(shù)據(jù),相當于數(shù)據(jù)庫,本篇著重介紹廣播組件2022-09-09
Android發(fā)送GET與POST請求的DEMO詳解
本篇文章是對Android發(fā)送GET與POST請求的DEMO進行了詳細的分析介紹,需要的朋友參考下2013-06-06

