android绘制环形进度_Android动态自定义圆形进度条
這篇文章主要介紹了Android動態自定義圓形進度條,需要的朋友可以參考下
效果圖:
A.繪制圓環,圓弧,文本
//1.畫圓環
//原點坐標
float circleX = width / 2;
float circleY = width / 2;
//半徑
float radius = width / 2 - roundWidth / 2;
//設置畫筆的屬性
paint.setColor(roundColor);
paint.setStrokeWidth(roundWidth);
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(circleX, circleY, radius, paint);
//2.畫圓弧
RectF oval = new RectF(roundWidth/2,roundWidth/2,width-roundWidth/2,width - roundWidth/2);
paint.setColor(roundProgressColor);
canvas.drawArc(oval, 0, progress * 360 / max, false, paint);
//3.畫文本
paint.setTextSize(textSize);
paint.setColor(textColor);
paint.setStrokeWidth(0);
String text = progress * 100 / max + "%";
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
canvas.drawText(text, width / 2 - bounds.width() / 2, width / 2 + bounds.height() / 2, paint);
B.自定義屬性的具體步驟
具體步驟:
1. 定義屬性: 在values目錄下創建attrs.xml
2. 在布局文件中引用當前應用的名稱空間
3. 在自定義視圖標簽中使用自定義屬性
android:id="@+id/rp_home_progress"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
atguigu:roundColor="@android:color/darker_gray
atguigu:roundProgressColor="@android:color/holo_red_dark"
atguigu:textColor="@color/text_progress"
atguigu:roundWidth="10dp"
atguigu:textSize="20sp"
/>
4. 在自定義View類的構造方法中, 取出布局中的自定義屬性值
//1.得到所有自定義屬性的數組
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgress);
//2.獲取自定義屬性的值, 如果沒有指定取默認值
roundColor = typedArray.getColor(R.styleable.RoundProgress_roundColor, Color.RED);
roundProgressColor = typedArray.getColor(R.styleable.RoundProgress_roundProgressColor, Color.GREEN);
textColor = typedArray.getColor(R.styleable.RoundProgress_textColor, Color.GREEN);
roundWidth = typedArray.getDimension(R.styleable.RoundProgress_roundWidth, UIUtils.dp2px(10));
textSize = typedArray.getDimension(R.styleable.RoundProgress_textSize, UIUtils.dp2px(20));
//3.釋放資源數據
typedArray.recycle();
C.讓圓環進度"動起來"
1.自定義RoundProgress類中提供進度屬性的getter和setter方法
2.在HomeFragment的onSuccess()中:
以上所述是小編給大家介紹的Android動態自定義圓形進度條,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
總結
以上是生活随笔為你收集整理的android绘制环形进度_Android动态自定义圆形进度条的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 美育在计算机教育中应用,在现代教育技术中
- 下一篇: android sina oauth2.