Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 缩放后保存图像视图_Android_Xml_Imageview_Mask - Fatal编程技术网

Android 缩放后保存图像视图

Android 缩放后保存图像视图,android,xml,imageview,mask,Android,Xml,Imageview,Mask,在我的片段中,我可以缩放图像,现在我要做的是将图像保存为僵尸 我该怎么做 < P> 1:左上角的图像是我从ReStand中提取的正常图像,中间的第二个图像是我想联想到第一个图像的图像。 PJ 2:缩放后,我希望将此关联保存在本地 类别: public class MaskFragment extends AbstractFragment{ @ViewById ImageView imageToEdit; Matrix matrix = new Matrix();

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

我该怎么做

< P> 1:左上角的图像是我从ReStand中提取的正常图像,中间的第二个图像是我想联想到第一个图像的图像。 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:


如何将第二个图像保存到本地


感谢您的回复。

关于@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();
        }
    }
}

看看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。
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();
    }