【问题标题】:Save a image View after zoom it保存图像放大后查看
【发布时间】:2014-09-26 08:02:26
【问题描述】:

在我的片段中我可以缩放图像,现在我要做的是保存缩放的图像

我该怎么做

PJ 1:左上角的图像是我从资源中获取的正常图像,中间的第二个图像是我要关联到第一个图像的图像。

PJ 2:缩放后我想将此关联保存在本地

类:

public class MaskFragment extends AbstractFragment{
    @ViewById
    ImageView imageToEdit;
    Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();
    PointF startPoint = new PointF();
    PointF midPoint = new PointF();
    float oldDist = 1f;
    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int mode = NONE;

    @AfterViews
    void initialise() {
    /** * set on touch listner on image */
    imageToEdit.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ImageView view = (ImageView) v;
            System.out.println("matrix=" + savedMatrix.toString());
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                savedMatrix.set(matrix);
                startPoint.set(event.getX(), event.getY());
                mode = DRAG;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                oldDist = spacing(event);
                if (oldDist > 10f) {
                    savedMatrix.set(matrix);
                    midPoint(midPoint, event);
                    mode = ZOOM;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                break;
            case MotionEvent.ACTION_MOVE:
                if (mode == DRAG) {
                    matrix.set(savedMatrix);
                    matrix.postTranslate(
                        event.getX() - startPoint.x,
                        event.getY() - startPoint.y
                    );
                } else if (mode == ZOOM) {
                    float newDist = spacing(event);
                    if (newDist > 10f) {
                        matrix.set(savedMatrix);
                        float scale = newDist / oldDist;
                        matrix.postScale(
                            scale, scale, midPoint.x,
                            midPoint.y
                        );
                    }
                }
                break;
            }
            view.setImageMatrix(matrix);
            return true;
        }

        @SuppressLint("FloatMath")
        private float spacing(MotionEvent event) {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
            return FloatMath.sqrt(x * x + y * y);
        }

        private void midPoint(PointF point, MotionEvent event) {
            float x = event.getX(0) + event.getX(1);
            float y = event.getY(0) + event.getY(1);
            point.set(x / 2, y / 2);
        }
    });
}

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:gesture-image="http://schemas.polites.com/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9" >

    <ImageView
        android:id="@+id/imageToEdit"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:scaleType="matrix"
        android:src="@drawable/fake_user" />

    <ImageView
        android:id="@+id/mask"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="center"
        android:layout_centerInParent="true"
        android:src="@drawable/camera_native" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="0.1"
    android:background="@android:color/black" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Valider"
        android:textColor="@android:color/white"
        android:textSize="25sp" />
</RelativeLayout>

如何将第二张图片保存在本地?

感谢您的回复。

【问题讨论】:

  • 看看这个SO Post
  • 正是我需要的,非常感谢。

标签: android xml imageview mask


【解决方案1】:

查看 android api 文档中的位图和文件。它将帮助您了解一切是如何工作的,而不仅仅是复制和粘贴。

    //use image from cache
    yourImageView.setDrawingCacheEnabled(true);
    yourImageView.buildDrawingCache(true); //this might hamper performance use hardware acc if available. see: http://developer.android.com/reference/android/view/View.html#buildDrawingCache(boolean)

    //create the bitmaps
    Bitmap zoomedBitmap = Bitmap.createScaledBitmap(yourImageView.getDrawingCache(true), outputSize, outputSize, true);

    yourImageView.setDrawingCacheEnabled(false);

现在您有了放大的图像,只需将其保存到文件中。有很多关于保存到文件的信息。简而言之:

File myFile = new File(thePathOfYourFile); //have a look at the android api docs for File for proper explanation
FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(myFile);
        zoomedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
        fileOutputStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

【讨论】:

  • getDrawingCache() 返回 null。
【解决方案2】:

@Sagar Pilkhwal 的解决方案

         public void saveAsJpeg(View view, File file) {
    view.setDrawingCacheEnabled(true);
    Bitmap _b = view.getDrawingCache();
    OutputStream _out = null;
    try {
        _out = new FileOutputStream(file);
        _b.compress(Bitmap.CompressFormat.JPEG, 100, _out);             
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            _out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

【讨论】:

    猜你喜欢
    • 2014-04-17
    • 2018-08-17
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2021-10-11
    • 2022-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多