編輯:關於 Android 開發
DrawableView 是一個 Android 手寫板元件,開箱就能用。它支援:調整畫筆顏色、調整線條粗細、手勢縮放與拖曳畫布、復原上一筆,以及將手繪結果輸出成圖檔。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zzw.testdrawableview.MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/subWidth"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/sub" />
<Button
android:id="@+id/addWidth"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/add" />
<Button
android:id="@+id/sava"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@drawable/sava" />
<Button
android:id="@+id/PaintColor"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentTop="true"
android:layout_marginRight="50dp"
android:layout_toLeftOf="@+id/sava"
android:background="@drawable/rand" />
<Button
android:id="@+id/undo"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="50dp"
android:layout_toRightOf="@+id/sava"
android:background="@drawable/ret" />
</RelativeLayout>
<me.panavtec.drawableview.DrawableView
android:id="@+id/paintView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="1" />
</LinearLayout>
public class MainActivity extends Activity implements OnClickListener {
private DrawableView drawableView;
private DrawableViewConfig config;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
config = new DrawableViewConfig();
drawableView = (DrawableView) findViewById(R.id.paintView);
Button addWidth = (Button) findViewById(R.id.addWidth);
Button subWidth = (Button) findViewById(R.id.subWidth);
Button PaintColor = (Button) findViewById(R.id.PaintColor);
Button undo = (Button) findViewById(R.id.undo);
Button sava = (Button) findViewById(R.id.sava);
addWidth.setOnClickListener(this);
subWidth.setOnClickListener(this);
PaintColor.setOnClickListener(this);
undo.setOnClickListener(this);
sava.setOnClickListener(this);
config.setStrokeColor(Color.RED); // 畫筆顏色
config.setShowCanvasBounds(true); // 顯示畫布邊界
config.setStrokeWidth(10.0f); // 畫筆寬度
config.setMinZoom(1.0f); // 縮放範圍
config.setMaxZoom(3.0f);
config.setCanvasHeight(3000); // 畫布尺寸
config.setCanvasWidth(2000);
drawableView.setConfig(config);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.addWidth:
config.setStrokeWidth(config.getStrokeWidth() + 10);
Toast.makeText(this, "當前畫筆寬度:" + config.getStrokeWidth(), 0).show();
break;
case R.id.subWidth:
if (config.getStrokeWidth() > 10) {
config.setStrokeWidth(config.getStrokeWidth() - 10);
Toast.makeText(this, "當前畫筆寬度:" + config.getStrokeWidth(), 0).show();
}
break;
case R.id.PaintColor:
// 測試期間隨機產生一種顏色
int r1 = (int) (Math.random() * 256);
int r2 = (int) (Math.random() * 256);
int r3 = (int) (Math.random() * 256);
config.setStrokeColor(Color.argb(255, r1, r2, r3));
Toast.makeText(this, "顏色生成成功", 0).show();
break;
case R.id.undo:
drawableView.undo();
break;
case R.id.sava:
try {
savaBitmapToSDCard();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
/** 將手繪結果存為圖片 */
private void savaBitmapToSDCard() throws IOException {
Bitmap bmp = drawableView.obtainBitmap();
File parentPath = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File f = new File(parentPath, "myDrawableView.png");
f.createNewFile();
Log.d("保存路徑", f.getAbsolutePath());
FileOutputStream fos = new FileOutputStream(f);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
Toast.makeText(this, "保存成功,路徑 " + f.getAbsolutePath(), 1).show();
}
}
DrawableView 在 debug 階段會在畫布上畫一些 log 資訊,正式環境通常不需要。修改原始碼中 CanvasDrawer 的兩行即可:
initLogger();
// ...
canvasLogger.log(canvas, canvasRect, viewRect, scaleFactor);
把這兩行註解掉重新編譯,畫面就乾淨了。

第一,寫入外部儲存需要執行期權限。 Android 6.0(API 23)起,WRITE_EXTERNAL_STORAGE 必須在執行期向使用者請求;Android 10 之後更受 Scoped Storage 限制,直接寫 DIRECTORY_PICTURES 可能失敗。建議改用 MediaStore.Images 或 Storage Access Framework。
第二,config 的變更可能不會即時生效。 部分版本的 DrawableView 在 setConfig() 之後才套用設定,執行中直接改 config 的 stroke 相關屬性,畫筆未必跟著變。若遇到這種情況,需重新呼叫 setConfig(config),但要注意這可能清除既有筆跡。
第三,畫布尺寸與記憶體。 範例設定 3000×2000 的畫布,等於約 24MB 的 ARGB_8888 記憶體。在中低階裝置上很容易觸發 OOM。實務上應依螢幕解析度與可用記憶體動態決定,並在 onTrimMemory() 中妥善處理。
Android 自定義 View 實作:模擬下載進度圓形動畫
Android 自定義 View 實作:模擬下載進度圓形動畫 本文實作一個自定義 View,用來模擬下載進度。畫面會動態顯示圓形進度條、百分比文字,完成後跳出提示對話框。
Android 6.0(棉花糖)帶來了哪些新特性
Android 6.0(棉花糖)帶來了哪些新特性 北京時間 9 月 30 日凌晨,美國舊金山舉行了 2015 年秋季新品發布會,代號為 Marshmallow(棉花糖)
使用 Messenger 實作 Android 跨進程通訊
Messenger 的跨進程溝通 在 Android 中,若需要讓不同進程的 Service 或元件互相傳遞訊息,Messenger 是一個相對簡單的選擇。它基於 Han
Android 安全專項:Apk 加固從零實戰
0x00 原理部分不再獻丑(前面幾篇已講得很清楚),本文直接實戰,講述如何從 0 開始最終實現 Apk 加固的整個過程,其間踩了不少坑。 0x01 准備被加固的源碼 Ap