从View Android保存照片拼贴

Iva*_*n B 0 java android view bitmap

我有下一个观点:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="350dp"
    android:layout_height="350dp"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="140dp"
        android:layout_height="match_parent"
        android:layout_marginRight="2.5dp"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="2.5dp"
            android:layout_weight="1"
            android:longClickable="true"
            android:scaleType="matrix"
            android:src="@drawable/a1" />


        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="2.5dp"
            android:layout_weight="1"
            android:longClickable="true"
            android:scaleType="matrix"
            android:src="@drawable/a2" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginLeft="2.5dp"
            android:longClickable="true"
            android:scaleType="matrix"
            android:src="@drawable/a4" />
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

看起来像: 在此处输入图片说明

在此视图中,用户可以编辑此图片的视图(缩放,旋转)

我必须保存编辑的照片拼贴。如何保存带有缩放和旋转照片的视图?是否可以将位图中的编辑视图保存到应用程序缓存中?

谢谢你的帮助!

Muk*_*ana 5

是的,您只需拍摄编辑图像的屏幕快照并创建一个位图即可保存到您想要的任何位置

下面是获取位图的功能

public Bitmap getBitMap() {
    try {
        yourEditedPhotoCollageLayout.buildDrawingCache();
        Bitmap bmp = Bitmap.createBitmap(yourEditedPhotoCollageLayout.getDrawingCache());
        return bmp;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

这样您就可以保存该位图

private void saveBitmap(Bitmap bitmap) {
    try {
        File storageDir = createImageFile();
        String path = storageDir.toString();
        OutputStream out = new FileOutputStream(path);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.close();

        MyMediaConnectorClient client = new MyMediaConnectorClient(path);
        MediaScannerConnection scanner = new MediaScannerConnection(
                Context, client);
        client.setScanner(scanner);
        scanner.connect();

    } catch (IOException e) {
        Log.e("save image", "failed to save image", e);
    }
}
Run Code Online (Sandbox Code Playgroud)