Question

I have two images, Image A which is the big background at the back and image B that is a small icon that will be merging on top of image A.

How it works

The user takes a photo from the camera and this photo will be Image A. The user selects the icon from the layout and that will be Image B. After selecting the image for image B, the user can move image B around the layout to adjust the position where the image B will overlay on top of image A.

After which the user pressed save, the canvas will merge two image, B on top of A, with the position the user wants and save it to SD card.

Problem

I have managed to get the image B to move around the layout but I do not know how to get it to merged at the position to the image A.

This is what i did to get the image B to move around the layout.

img_additionalImage = (ImageView) findViewById(R.id.img_additionalImage);
    img_additionalImage.setOnTouchListener(new OnTouchListener()
    {
        @SuppressLint("NewApi")
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    isImageMoving = true;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (isImageMoving)
                    {
                        x = event.getRawX() - img_additionalImage.getWidth() / 2;
                        y = event.getRawY() - img_additionalImage.getHeight() / 2;
                        img_additionalImage.setX(x);
                        img_additionalImage.setY(y);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    isImageMoving = false;
                    break;
            }
            return true;
        }
    });

I do not know how to merge two images together with position the user chose.

Was it helpful?

Solution

if you have use RealtiveLayout or LinearLayout as a parent layout of this 2 imageview than you can capture that view by this way..

view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
b.compress(CompressFormat.JPEG, 95, new FileOutputStream("/some/location/image.jpg"));

Where view is your View. The 95 is the quality of the JPG compression. And the file output stream is just that.

what does setDrawaingCacheEnabled?

Enables or disables the drawing cache. When the drawing cache is enabled, the next call to getDrawingCache() or buildDrawingCache() will draw the view in a bitmap. Calling draw(android.graphics.Canvas) will not draw from the cache when the cache is enabled. To benefit from the cache, you must request the drawing cache by calling getDrawingCache() and draw it on screen if the returned bitmap is not null.

Enabling the drawing cache is similar to setting a layer when hardware acceleration is turned off. When hardware acceleration is turned on, enabling the drawing cache has no effect on rendering because the system uses a different mechanism for acceleration which ignores the flag. If you want to use a Bitmap for the view, even when hardware acceleration is enabled, see setLayerType(int, android.graphics.Paint) for information on how to enable software and hardware layers.

from Android Docs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top