Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

DrawableView 第三方開源畫板:手寫、調色、縮放與存檔

編輯:關於 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>

Java 程式碼

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() 中妥善處理。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved