a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統(tǒng) > Android > 正文

Android自定義帶圓點(diǎn)的半圓形進(jìn)度條

2019-10-21 21:31:20
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了Android自定義帶圓點(diǎn)的半圓形進(jìn)度條,供大家參考,具體內(nèi)容如下

僅限用于半圓形,如須要帶圓點(diǎn)的圓形進(jìn)度條,圓點(diǎn)會出現(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; /**  * 畫筆對象的引用  */ 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則比較簡單,不需要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 自動生成的方法存根  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)整一下高度寬度 

Android,圓點(diǎn),半圓形,進(jìn)度條

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


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久国产欧美一区二区三区精品 | 久久国产一 | 在线亚洲不卡 | 欧美一级艳片视频免费观看 | 91视频三区 | 日本一区二区三区视频在线观看 | 一级免费片 | 亚洲一区二区三区四区五区午夜 | 久久青青视频 | 无套内谢孕妇毛片免费看红桃影视 | 亚洲精品久久久久久下一站 | 免费看的毛片 | 天天爽天天干 | 久久青青 | 久久久久久一区 | 欧美视频在线免费看 | 天天色天天色 | 色天天综合久久久久综合片 | 欧美日韩国产中文字幕 | 国产精品久久久久久久久久久久久久久久 | 国产激情网站 | 日本激情在线 | 久操成人 | 青青草国产在线 | 国产黄色大片网站 | 欧美精品久久久 | 91人人插| 天天干天天插 | 欧美lesbianxxxxhd视频社区 | 在线涩涩| www.99热 | 日韩大片免费播放 | 中文精品一区二区三区 | 青草成人免费视频 | 日韩欧美大片在线观看 | 久久久久久久国产 | 亚洲xxxxx| 黄色片视频在线观看 | 国产一区二区三区在线 | 99亚洲精品 | www污在线观看 |