0

私のアプリケーションはある種の「ミニ ペイント」であり、現在のビューをデバイス メモリに保存したいと考えています...逆のプロセスも実行したいと考えています (デバイス メモリから画像をロードし、それを現在のビューとして設定します)。 )

ミニペイント

ええ、それはフラミンゴだと思います、私は芸術家です!

4

2 に答える 2

2

自分で試したことはありませんが、この回答は、ルートビューを取得して描画キャッシュを保存することにより、プログラムでスクリーンショットを撮ることを示しています。絵を保存するために必要なのはこれだけかもしれません。

編集:リンクを修正

于 2012-01-23T03:58:09.910 に答える
1

まず、View オブジェクトの onDraw() メソッドをオーバーライドしてこの描画を実行していると仮定します。このメソッドは、Canvas オブジェクトを渡し、それに対していくつかの描画操作を実行します。

これは、この問題に取り組むための非常に基本的な方法です。読み書きするファイル形式や、I/O コードでの追加のエラー処理など、考慮すべき追加の考慮事項がおそらくたくさんあります。しかし、これでうまくいくはずです。

現在持っている図面を保存するには、View の drawingCache を Picture オブジェクトに書き出してから、Picture の writeToStream メソッドを使用します。

既存の画像を読み込むには、Picture.readFromStream メソッドを使用してから、onDraw 呼び出しで、読み込まれた画像を Canvas に描画します。

ウィットに:

/**
 * Saves the current drawing cache of this View object to external storage.
 * @param filename a file to be created in the device's Picture directory on the SD card
 */
public void SaveImage(String filename) {
    // Grab a bitmap of what you've drawn to this View object so far
    Bitmap b = this.getDrawingCache();
    // It's easy to save a Picture object to disk, so we copy the contents
    // of the Bitmap into a Picture
    Picture pictureToSave = new Picture();
    // To copy the Bitmap into the Picture, we have to use a Canvas
    Canvas c = pictureToSave.beginRecording(b.getWidth(), b.getHeight());
    c.drawBitmap(b, 0, 0, new Paint());
    pictureToSave.endRecording();

    // Create a File object where we are going to write the Picture to
    File file = new File(this.getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES), filename);
    try {
        file.createNewFile();
    }
    catch (IOException ioe) {
        ioe.printStackTrace();
    }
    // Write the contents of the Picture object to disk
    try {
        OutputStream os = new FileOutputStream(file);
        pictureToSave.writeToStream(os);
        os.close();
    }
    catch (FileNotFoundException fnfe) {
        fnfe.printStackTrace();
    }
}

/**
 * Returns a Picture object loaded from external storage
 * @param filename the name of the file in the Pictures directory on the SD card
 * @return null if the file is not found, or a Picture object.
 */
public Picture LoadImage(String filename) {
    // Load a File object where we are going to read the Picture from
    File file = new File(this.getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES), filename);
    Picture pictureToLoad = null;
    try {
        InputStream is = new FileInputStream(file);
        pictureToLoad = Picture.createFromStream(is);
        is.close();
    }
    catch (FileNotFoundException fnfe) {
        fnfe.printStackTrace();
    }
    // Return the picture we just loaded. Draw the picture to canvas using the
    // Canvas.draw(Picture) method in your View.onDraw(Canvas) method
    return pictureToLoad;
}

これを理解するために私が読んだ便利なリンク:

于 2012-01-23T04:57:23.373 に答える