Android view自定義帶文字帶進度的控件
目標:自定義一個帶文字帶進度的控件,具體內容如下
效果圖:

不啰嗦先看東西:

步驟分析
提取自定義屬性
//提供對外暴露的屬性,如有不夠自己擴展
<declare-styleable name="DescProgressView">
<attr name="dpv_text_normal_color" format="color" />
<attr name="dpv_text_seleced_color" format="color" />
<attr name="dpv_text_size" format="dimension" />
<attr name="dev_progress_bg_color" format="color" />
<attr name="dev_progress_small_circle_color" format="color" />
<attr name="dev_progress_big_circle_color" format="color" />
</declare-styleable>
解析自定義屬性
private void initAttrs(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DescProgressView, defStyleAttr, R.style.Def_DescProgressViewStyle);
int indexCount = typedArray.getIndexCount();
for (int i = 0; i < indexCount; i++) {
int attr = typedArray.getIndex(i);
switch (attr) {
case R.styleable.DescProgressView_dpv_text_normal_color:
textNormalColor = typedArray.getColor(attr, Color.BLACK);
break;
case R.styleable.DescProgressView_dpv_text_seleced_color:
textSelectedColor = typedArray.getColor(attr, Color.BLACK);
break;
case R.styleable.DescProgressView_dpv_text_size:
dpvTextSize = typedArray.getDimensionPixelSize(attr, 0);
break;
case R.styleable.DescProgressView_dev_progress_bg_color:
dpvProgressBgColor = typedArray.getColor(attr, Color.BLACK);
break;
case R.styleable.DescProgressView_dev_progress_small_circle_color:
dpvSmallCicleColor = typedArray.getColor(attr, Color.BLACK);
break;
case R.styleable.DescProgressView_dev_progress_big_circle_color:
dpvBigCircleColor = typedArray.getColor(attr, Color.BLACK);
break;
}
}
typedArray.recycle();
}
測量UI圖的比例(包含圖標大小比例,位置比例)
//這里大家可以根據(jù)自己的習慣來,我習慣用view的尺寸當做參照,來約束界面的view,各有利弊,也可以暴露出屬性設置具體的dp值,根據(jù)比例的話,調整好比例后,所有的繪制內容會統(tǒng)一約束 private static final float SCALE_OF_PROGRESS_HEIGHT = 70.F / 120; private static final float SCALE_OF_TOP_AND_BOTTOM_PADDING = 10.F / 120; private static final float SCALE_OF_LEFT_AND_RIGHT_PADDING = 20.F / 120; private static final float SCALE_OF_TEXT_DESC_CONTAINER = 50.F / 120; private static final float SCALE_OF_BIG_CIRCLE_HEIGHT = 22.F / 120; private static final float SCALE_OF_SMALL_CIRCLE_HEIGHT = 16.F / 120; private static final float SCALE_OF_LINE_HEIGHT = 4.F / 120; private static final float DEF_VIEW_HEIGHT = 120.F;
提取繪制的各個元素的位置屬性坐標等
這個view的唯一要提前確定的就是文字的位置,文字的位置確定需要知道所有文字的長度,左右間距,計算出中間的白色間隔
代碼如下
/**
* 獲取文字在畫布中的位置
*/
private void getDescTextRegonPoint() {
for (int i = 0; i < descs.size(); i++) {
Point textRegonPoint = new Point();
int sumX = 0;
//非常重要:計算各個文字在view中的具體坐標,體會下這個二級for循環(huán),子循環(huán)是確定每個描述文本的位置
for (int j = 0; j < i; j++) {
Point tempSum = allDescTextPoints.get(j);
sumX += tempSum.x;
}
sumX += i * getTextDescSpace();
textRegonPoint.x = sumX + leftAndRightPadding;
textRegonPoint.y = dpViewHeight - topAndBottomPadding - textDescContainerHeight / 2;
textPoints4Draw.add(textRegonPoint);
}
}
/**
* 獲取文字的間距
*
* @return 獲取文字的間距
*/
private float getTextDescSpace() {
float allDescWith = 0;
for (Point tempDesc : allDescTextPoints) {
allDescWith += tempDesc.x;
}
int textContainerW = (int) (dpViewWidth - leftAndRightPadding * 2 - allDescWith);
if (descs != null && descs.size() > 1) {
int spaceCount = descs.size() - 1;
return textContainerW * 1.F / spaceCount;
}
return 0;
}
繪制
我們在view測量確定了尺寸完畢之后,直接繪制即可
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// 確定各個比例的大小
super.onSizeChanged(w, h, oldw, oldh);
dpViewHeight = h;
dpViewWidth = w;
progressContainerHeight = (int) (SCALE_OF_PROGRESS_HEIGHT * dpViewHeight);
topAndBottomPadding = (int) (SCALE_OF_TOP_AND_BOTTOM_PADDING * dpViewHeight);
leftAndRightPadding = (int) (SCALE_OF_LEFT_AND_RIGHT_PADDING * dpViewHeight);
textDescContainerHeight = (int) (SCALE_OF_TEXT_DESC_CONTAINER * dpViewHeight);
smallCircleRadio = (int) (SCALE_OF_SMALL_CIRCLE_HEIGHT * dpViewHeight / 2);
bigCircleRadio = (int) (SCALE_OF_BIG_CIRCLE_HEIGHT * dpViewHeight / 2);
lineHeight = (int) (SCALE_OF_LINE_HEIGHT * dpViewHeight);
// 獲取各個部分所需要的約束坐標
getDescTextWidthAndHeight();
getDescTextRegonPoint();
getBgLineRectF();
getBgCirclePoints();
getSelectedRectF();
getColorFullRectF();
getGrayRectF();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawDescText(canvas);
drawBgLine(canvas);
drawSelectedLine(canvas);
drawGrayRectF(canvas);
drawSelectedCircles(canvas);
}
//繪制部分的代碼就是canvas 的API的使用,沒有什么技術含量.
//最后暴露給外面設置數(shù)據(jù)的接口
public void setProgressDescs(List<String> descs, int currentSelectPosition) {
this.currentSelectPosition = currentSelectPosition;
if (descs != null && descs.size() > 1) {
this.descs.clear();
this.descs.addAll(descs);
this.allDescTextPoints.clear();
invalidate();
}
}
源代碼下載地址https://github.com/GuoFeilong/DescPbView來個star就更好了謝謝!
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android利用Intent.ACTION_SEND進行分享
這篇文章主要介紹了Android利用Intent.ACTION_SEND進行分享,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
Android 個人理財工具一:項目概述與啟動界面的實現(xiàn)
本文主要介紹Android 開發(fā)個人理財工具項目概述與啟動界面的實現(xiàn),這里主要對實現(xiàn)項目的流程做了詳細概述,并對啟動界面簡單實現(xiàn),有需要的小伙伴可以參考下2016-08-08
PagerSlidingTabStrip制作Android帶標簽的多界面滑動切換
這篇文章主要介紹了使用PagerSlidingTabStrip制作Android帶標簽的多界面滑動切換效果的方法,PagerSlidingTabStrip是GitHub上的一個開源項目,調用這個庫可以少寫不少代碼XD 需要的朋友可以參考下2016-04-04
android自定義ListView實現(xiàn)底部View自動隱藏和消失的功能
本篇文章主要介紹了android自定義ListView實現(xiàn)底部View自動隱藏和消失的功能 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03
Android基于socket實現(xiàn)的簡單C/S聊天通信功能
這篇文章主要介紹了Android基于socket實現(xiàn)的簡單C/S聊天通信功能,結合實例形式分析了Android使用socket實現(xiàn)客服端與服務器端數(shù)據(jù)的發(fā)送與接收處理技巧,需要的朋友可以參考下2016-10-10

