-
繪制條形進(jìn)度條 1.onDraw() protected synchronized void onDraw(Canvas canvas) { canvas.save(); boolean noNeedUnreach; // reached String text = mProgress + "%"; float ratio = mProgress * 1.0f / getMax(); float progressX = ratio * mRealWidth; float reachX = progressX - mTextOffset / 2 - textWidth / 2; if ((progressX + textWidth / 2) >= mRealWidth) { reachX = mRealWidth - textWidth - mTextOffset / 2; noNeedUnreach = true; } if (reachX > 0) { ...... canvas.drawLine(0, 0, reachX, 0, mPaint); } else { progressX = textWidth / 2; } // text ...... canvas.drawText(text, progressX - textWidth / 2, textY, mPaint); // unreached if (!noNeedUnreach) { ...... canvas.drawLine(progressX + textWidth / 2 + mTextOffset / 2, 0, mRealWidth, 0, mPaint); } canvas.restore(); }查看全部
-
2.自定義控件的onMeasure(),這個(gè)是重點(diǎn)! (2)寬 因?yàn)閷τ趯挾葋碚f,必須由用戶指定,所以要么是固定值,或者是match_parent (3)最后調(diào)用setMeasuredDimension()來確定view的寬和高 protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { /** * 對于寬度來說,因?yàn)楸仨氂捎脩糁付?,要么是固定值或match_parent */ // int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthVal = MeasureSpec.getSize(widthMeasureSpec); int height = measureHeight(heightMeasureSpec); setMeasuredDimension(widthVal, height); mRealWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight(); } 3.小結(jié) 在onMeasure()這個(gè)方法中重要是確定view的高,一定要判斷三種模式查看全部
-
1.獲取自定義屬性 自定義水平進(jìn)度條共有7個(gè)自定義屬性,在類初始化時(shí)給它們設(shè)置一個(gè)默認(rèn)值,然后在構(gòu)造函數(shù)中通過TypedArray來獲取 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.HorizontalProgressBar); mTextSize = (int) typedArray.getDimension(R.styleable.HorizontalProgressBar_progress_text_size, mTextSize); mTextColor = mUnreachColor = mUnreachHeight = mReachColor = mReachHeight = mTextOffset = typedArray.recycle(); 2.自定義控件的onMeasure(),這個(gè)是重點(diǎn)! onMeasure()需要區(qū)分三種不同的模式,模式和數(shù)值是通過輔助類MeasureSpec從onMeasure()的兩個(gè)參數(shù)中獲取的。 在這里需要確定水平進(jìn)度條的寬和高 (1)高 private int measureHeight(int height) { int result = 0; int heightMode = MeasureSpec.getMode(height); int heightSize = MeasureSpec.getSize(height); // 下面就是三種模式的判斷 if (heightMode == EXACTLY) { result = heightSize; } else { /**如果是UNSPECIFIED 高度值應(yīng)該是左邊進(jìn)度條高度,右邊進(jìn)度條高度,文字高度三者最大值加上上下的padding*/ …… if (heightMode == AT_MOST) { /**如果是AT_MOST 高度值應(yīng)該是進(jìn)度條本身高度和父布局測量的高度值兩者的較小值*/ } } return result; }查看全部
-
自定義控件步驟查看全部
-
好難查看全部
-
三種模式查看全部
-
如何自定義控件 1.需要注意的點(diǎn)和步驟 如圖 參考之前的筆記:http://idcbgp.cn/note/579?sort=last&page=1&owner=mine 2.重新復(fù)習(xí)了一下衛(wèi)星菜單和自定義的ViewPagerIndicator 3.自定義屬性的使用 (1)attrs.xml聲明 (2)layout布局中指定 (3)java代碼中使用TypedArray獲取并賦值 本項(xiàng)目中需要自定義的屬性 (1)水平進(jìn)度條兩邊的進(jìn)度條顏色和寬度,中間數(shù)字部分的顏色和字體大小 (3)圓形進(jìn)度條背景的顏色和寬度,進(jìn)度條的顏色和寬度,中間數(shù)字部分查看全部
-
如何自定義控件查看全部
-
可以查看全部
-
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="progress_horizontal"> <attr name="progress_reach_color" format="color"></attr> <attr name="progress_reach_hight" format="dimension"></attr> <attr name="progress_unreach_color" format="color"></attr> <attr name="progress_unreach_hight" format="dimension"></attr> <attr name="progress_text_color" format="color"></attr> <attr name="progress_text_size" format="dimension"></attr> <attr name="progress_text_offset" format="dimension"></attr> </declare-styleable> </resources>查看全部
-
自定義控件步驟查看全部
-
這個(gè)方法是轉(zhuǎn)變?yōu)闃?biāo)準(zhǔn)尺寸的一個(gè)函數(shù),例如 int size = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, context.getResources().getDisplayMetrics()); 這里COMPLEX_UNIT_SP是單位,20是數(shù)值,也就是20sp。查看全部
-
dp轉(zhuǎn)換成px的函數(shù)查看全部
-
1233456查看全部
-
截圖123查看全部
舉報(bào)
0/150
提交
取消