編輯:關於 Android 程式設計
Android 自定義 View 有幾條路徑:完全從零繼承 View、繼承 View 的子類、或者組合已有控件。這一類最簡單,也最適合入門——繼承一個現成的控件,覆寫 onDraw(),在它原有的繪製基礎上補上自己想要的效果。
關鍵只有一個方法:onDraw(Canvas canvas)。所有繪製指令都通過傳入的 Canvas 完成,而 super.onDraw(canvas) 這一行決定了我們的內容是蓋在原控件之上,還是被它蓋住。
需求很簡單:先鋪一層藍色底,再在中間疊一個黃色矩形,最後讓文字正常顯示在最上層並且在橫向上錯開一點。
public class TestView extends TextView {
public TestView(Context context) {
super(context);
}
public TestView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TestView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
// 先畫底層:整塊區域填藍
Paint paint1 = new Paint();
paint1.setColor(getResources().getColor(android.R.color.holo_blue_light));
paint1.setStyle(Paint.Style.FILL);
canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), paint1);
// 再畫第二層:四周留出 10px 邊距的黃色矩形
Paint paint2 = new Paint();
paint2.setColor(Color.YELLOW);
paint2.setStyle(Paint.Style.FILL);
canvas.drawRect(10, 10,
getMeasuredWidth() - 10, getMeasuredHeight() - 10, paint2);
// 水平方向偏移 10px 後再交給父類繪製文字
// save/restore 必須成對出現,否則平移會污染後續繪製
canvas.save();
canvas.translate(10, 0);
super.onDraw(canvas);
canvas.restore();
}
}
幾個細節值得注意。
用 getMeasuredWidth() 而非 getWidth()。在 onDraw 階段,控件的測量已經完成但布局尚未結束,此時 getWidth() 可能還是 0,而 getMeasuredWidth() 是可靠的。
save 與 restore 必須配對。canvas.translate() 會改變 Canvas 的坐標系,如果不 restore,這個偏移會一直生效,後續所有繪製都會跟着偏。
super.onDraw() 的位置決定層疊關係。放在最前面,文字會被矩形蓋住;放在最後面,文字才會浮在最上層。
構造函數至少要有 (Context, AttributeSet)。只寫 TestView(Context) 的話,在 XML 布局裡使用這個控件會直接崩潰——因爲從 XML 加載時,系統調用的是帶 AttributeSet 的那個構造函數。原始示例只寫了一個構造函數,這是一個隱藏的坑。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<com.example.customview.TestView
android:layout_width="200dp"
android:layout_height="100dp"
android:text="我是自定義的 TextView"
android:textColor="#000000"
android:textSize="18sp" />
</LinearLayout>
注意自定義 View 在布局中的寫法是完整類名(含包名),而不是簡寫的類名。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
在布局裡聲明就夠了,不需要在代碼裡做任何額外處理。
這個例子只碰了 onDraw,真實場景裡還會經常需要覆寫:
onMeasure():控制測量邏輯,支持 wrap_content 時必須處理onSizeChanged():尺寸變化時重建緩存對象,比在 onDraw 裡 new Paint 高效得多onTouchEvent():處理交互,配合属性動畫做反饋效果invalidate() 與 postInvalidate():前者主線程調用,後者可在子線程調用另外,把 Paint 對象提到類成員裡、在構造函數中初始化一次,比每次 onDraw 都 new 一批要划算——onDraw 的調用頻率遠比想象中高。
Android 資料儲存實戰:用 SQLiteDatabase 打造一個學生管理系統
這篇文章是上一篇《Android 資料儲存:如何搞定 SQLite Database》的配套實作練習。之所以把它單獨拎出來寫,是因為除了資料庫本身,還牽涉到不少其他知識
Android 自定義 View:打造帶加載動畫的登錄按鈕
昨天偶然看到 UI 給的一個交互效果,原圖如下: 於是大致模仿了一下下面的 login button。這裡沒有實現全部效果,感興趣的同學可以自行完善後面的展開動畫。 這
Android 使用 RecyclerView 與 CardView 實現瀑布流效果
RecyclerView 與瀑布流 Android 5.0 引入 Material Design 後,RecyclerView 與 CardView 成為打造現代化列表的
Android 網路編程(四):從原始碼解析 Volley
Volley 是 Android 上常用的網路請求框架。它將請求加入佇列,由快取與網路調度線程處理,最後把結果回傳主線程。整體架構Volley 主要包含主線程、快取調度線