Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get scaled Bitmap from ImageView

Tags:

android

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

like image 599
Lalit Poptani Avatar asked Oct 04 '11 12:10

Lalit Poptani


People also ask

How do I get bitmap from ImageView?

Bitmap bm=((BitmapDrawable)imageView. getDrawable()). getBitmap(); Try having the image in all drawable qualities folders (drawable-hdpi/drawable-ldpi etc.)

Can bitmap be scaled?

The larger dimension of the target bitmap size is going to match either maxWidth or maxHeight , the second dimension will be scaled proportionally. A new bitmap is created using createScaledBitmap with the correct targetWidth and targetHeight .


1 Answers

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.

like image 97
Lalit Poptani Avatar answered Sep 30 '22 09:09

Lalit Poptani