編輯:關於 Android 程式設計

這個自訂 View 的做法是:在 onDraw 裡先鋪一層背景色,再依百分比畫出進度區塊,最後把日期與描述文字定位上去。顏色、字級都透過自訂屬性對外開放。
package com.framework.widget; import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.view.View; import com.R; /** * @author dahai * @ClassName: (仿支付寶) 收益進度條 * @Description: ${todo} * @date ${date} ${time} * @email 202491024@qq.com * @since $android 進度條 */ public class ProfitProgerssBar extends View { //背景色 private static final int DEFAULT_BACK_COLOR = Color.parseColor("#ffffff"); //字的顏色 private static final int DEFAULT_TEXT_COLOR = Color.parseColor("#ffffff"); //進度條背景顏色 private static final int DEFAULT_PROGRESS_COLOR = Color.parseColor("#abacaf"); //進度條默認的高度 private static final float DEFAULT_PROGRESS_HEIGHT =120f; //文字的大小 private static final float DEFAULT_TEXT_SIZE = 50; /** * 收益進度條左右兩邊margin大小 */ private static final int MARGIN_SIZE = 20; private Context context; /** * 背景顏色的畫筆 */ private Paint backgroundPaint; /** * 收益進度顏色的畫筆 */ private Paint progressPaint; /** * 畫文字的畫筆 */ private Paint textPaint; /** * 背景的寬度 */ private int view_background_width; /** * 背景的高度 */ private float view_background_height = DEFAULT_PROGRESS_HEIGHT; /** * 日期 */ private String date = "2016/12/07"; /** * 描敘(百分比/元) */ private String desc = "2.1234"; /** * 要顯示的長度的百分比 */ private int progress = 70; //進度條顏色 private int progress_color = DEFAULT_PROGRESS_COLOR; //背景色 private int progress_back_color = DEFAULT_BACK_COLOR; //字的顏色 private int text_color = DEFAULT_TEXT_COLOR; //字的大小 private float TEXT_SIZE = DEFAULT_TEXT_SIZE; public ProfitProgerssBar(Context context) { super(context); initView(context); } public ProfitProgerssBar(Context context, AttributeSet attrs) { super(context, attrs); initView(context); } public ProfitProgerssBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } private void initView(Context context) { this.context = context; TypedArray typedArray = this.context.obtainStyledAttributes(R.styleable.ProfitProgerssBar); progress_back_color = typedArray.getColor(R.styleable.ProfitProgerssBar_progress_backg_color,DEFAULT_BACK_COLOR); text_color = typedArray.getColor(R.styleable.ProfitProgerssBar_progress_text_color,DEFAULT_TEXT_COLOR); TEXT_SIZE = typedArray.getDimension(R.styleable.ProfitProgerssBar_progress_text_size,DEFAULT_TEXT_SIZE); backgroundPaint = new Paint(); backgroundPaint.setStrokeWidth(10); backgroundPaint.setColor(progress_back_color); backgroundPaint.setDither(true); backgroundPaint.setAntiAlias(true); progressPaint = new Paint(); progressPaint.setStrokeWidth(10); progressPaint.setDither(true); progressPaint.setAntiAlias(true); textPaint = new Paint(); textPaint.setStrokeWidth(10); textPaint.setDither(true); textPaint.setAntiAlias(true); textPaint.setTextSize(TEXT_SIZE); DisplayMetrics d = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(d); view_background_width = d.widthPixels; } /** * 初始化 進度條 * @param date * @param desc * @param progress * @param progressColor */ public void init(String date,String desc,int progress,int progressColor){ this.date = date; this.desc = desc; this.progress = progress; this.progress_color = progressColor; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); view_background_height = this.getMeasuredHeight(); RectF r = new RectF(); r.left = 0; r.top = 0; r.right = view_background_width; r.bottom = view_background_height;////------------------------ canvas.drawRect(r, backgroundPaint); RectF r1 = new RectF(); r1.left = 0; r1.top = 0; r1.right = view_background_width * progress / 100; r1.bottom = view_background_height;////------------------------ progressPaint.setColor(progress_color); canvas.drawRect(r1, progressPaint); textPaint.setColor(text_color); Rect r2 = new Rect(); textPaint.getTextBounds(date,0,date.length(),r2); canvas.drawText(date, MARGIN_SIZE, (view_background_height-r2.top)/2, textPaint);//日期 Rect r3 = new Rect(); textPaint.getTextBounds(desc,0,desc.length(),r3); if(progress>95&&progress<100){ canvas.drawText(desc, r1.right-textPaint.measureText(desc)-MARGIN_SIZE-30,(view_background_height-r3.top)/2, textPaint); }else if(progress>=100) { canvas.drawText(desc, r1.right-textPaint.measureText(desc)-MARGIN_SIZE-45,(view_background_height-r3.top)/2, textPaint); }else { canvas.drawText(desc, r1.right - textPaint.measureText(desc) - MARGIN_SIZE, (view_background_height - r3.top) / 2, textPaint); } invalidate(); } }
程式中有個細節值得注意:當進度接近 100% 時,描述文字必須往左退一些,否則會超出進度色塊的右邊界。這段條件判斷就是為了處理這個邊界情況。
<com.framework.widget.ProfitProgerssBar android:layout_width="match_parent" android:layout_height="35dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="10dp" app:progress_back_color="@color/white" app:progress_text_color="@color/white" app:progress_text_size="14dp" android:id="@+id/profitProgerssBar" /> <declare-styleable name="ProfitProgerssBar"> <attr name="progress_backg_color" format="color"/> <attr name="progress_text_color" format="color"/> <attr name="progress_text_size" format="dimension"/> </declare-styleable>
整體而言,這類「進度 + 文字」的橫幅元件在自訂時要留意兩件事:文字定位要根據實際測量寬度計算,而不是寫死座標;onDraw 裡的 invalidate() 若要保留,記得確認不會造成無限重繪的效能問題。
用 EventBus 在元件之間傳遞訊息的實作教學
一、EventBus 是什麼 EventBus 是一套針對 Android 最佳化過的發布/訂閱事件總線,主要用來取代 Intent、Handler 與 Broadcas
打造可無限平滑循環的 ViewPager 廣告輪播條
很多應用的 banner 都能自動循環播放,還能手指滑動無限翻頁。實現它一定會想到 ViewPager,但 ViewPager 本身並不支持循環,最後一頁滑到底就卡住了。
Android 插件化之資源動態加載:原理、衝突處理與 R 文件難題
一、要解決的問題 插件化裡,代碼加載只是入門題,資源訪問才是真正的坎。需要回答三個問題:怎麼把插件的資源加載進來?插件與宿主的資源衝突怎麼處理?如何確保插件和宿主拿到的都
Android NDK 建置與呼叫 so 函式庫完整流程
本文將帶讀者梳理在 Android 專案中運用 NDK 建置並呼叫 so 函式庫的完整流程,適合初次接觸 JNI 開發的朋友參考。 一、使用 NDK 建置 so 函式庫