Lalit Poptani
Lalit Poptani

Reputation: 67286

How to get scaled Bitmap from ImageView

I am having a Activity which has a custom ImageView defined in the XML for Pinch Zoom functionality on an image. Now, my problem is that I want to fetch the scaled Bitmap from the Pinch Zoom functionality. For example if the user peforms zooming on the Image, then I want to store the exact size and the position of the Image as Bitmap.

This is my custom ImageView declared in XML.

<com.cam.view.PinchZoom_ImageView 
  android:id="@+id/gallery_selected_image_view"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:src="@drawable/icon"
  android:scaleType="center"
 />

UPDATE:

I am trying the below code to get the scaled Image, but It returns a Blank Bitmap.

        pinchZoom.setDrawingCacheEnabled(true);
        pinchZoom.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        pinchZoom.layout(0, 0, pinchZoom.getMeasuredWidth(), pinchZoom.getMeasuredHeight());
        pinchZoom.buildDrawingCache(true);
        Bitmap_gallery_Item = Bitmap.createBitmap(pinchZoom.getDrawingCache());
        pinchZoom.setDrawingCacheEnabled(false);

Any help would be appreciated. Thanks

Upvotes: 14

Views: 5164

Answers (3)

Lalit Poptani
Lalit Poptani

Reputation: 67286

Well, finally I can't believe that it was just a one line code. I finally succeeded using the matrix. The solution is very simple that I was storing the translation and scaling of the Image in a matrix. So, I declared that final matrix as public static and used that matrix to create draw a Bitmap on canvas.

        Canvas comboImage = new Canvas(cs);
        background = Bitmap.createScaledBitmap(background, width, height, true);
        comboImage.drawBitmap(background, 0, 0, null);
        comboImage.drawBitmap(foreground, PinchZoom_ImageView.matrix, null);

As you can see here PinchZoom_ImageView.matrix. It is a matrix that contains my final position of the scaled and translated image which I had declared as public static.

Upvotes: 12

Venky
Venky

Reputation: 11107

This post includes Cropping the Image from the Gallery and allows user to crop the Image and save the cropped Image and Shows in ImageView.

Seems it will help you some extends

Image Crop and show cropped Image in View

Upvotes: 0

Graeme
Graeme

Reputation: 25864

If you have scaleType="fitxy" then you can use a scaling factor to enlarge or shrink the width/height of the ImageView - It also means that you can pull back the exact height/width of the image because it has the same dimensions as it's container.

HTH

Upvotes: 0

Related Questions