I have a view that contains an ImageView. The ImageView is populated with a png resource, and fills the width and height of the image view.
Instead of this configuration, I would like to display the image zoomed in by an arbitrary percentage, and translated by an arbitrary coordinate. My question is, how can I configure an image view to display a sub-region of it's src bitmap, with arbitrary translation?
You can do this with matrix scaleType
Make a ImageView to have matrix scaleType, android:scaleType="matrix"
Create and manipulate a Matrix like this,
Matrix matrix = new Matrix();
matrix.postTranslate(-10, -10);
matrix.postScale(2, 2);
set this matrix to ImageView, imageView.setImageMatrix(matrix)
Related
I'm able to do a scale animation on an imageview but the view crops the image when it's set.
I need to accomplish a scale animation on the image itself because scaling down the view reveals the cropped edges
Is this possible without a frame-by-frame animated drawable?
I don't have any code to share because I'm not even sure where to start
You should scale it's bitmap like this:
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
//replace first `120` with your target width and second `120` with your target height
imageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 120, 120, false));
I have an ImageView which I apply a transformation matrix too. The same ImageView also has a frame background (a nine patch drawable, assigned with setBackgroundResource) but the background just stays the original size and position while the image is transformed correctly (scale, rotation and translate).
What is the correct way to make the background transform the same way as the image?
AdjustViewBounds didn't make any difference.
I want to have a ImageView of fixed x / y ratio on all of the screens.
for example famous 16/9 ratio.
And I also want that The ImageView be as large as possible.
how could I do it in android?
Thanks a lot.
The above answer will alter the aspect ratio.
You cannot achieve your goal by only modifying the xml layout file. You have to do this in Java code.
The basic steps are:
read your image file into a Bitmap, and obtain the initial width and height and aspect ratio of the Bitmap.
calculate the scaling factors for x and y dimension respectively, and use the smaller one of the two factors to scale your image
factorX= ScreenWitdhInPixel/ImgWidth
factorY= ScreenHeightInPixel/ImgHeight
factor= (factorX<factorY?factorX:factorY)
scale your image up using the factor value calculated in step 2
Matrix matrix = new Matrix();
matrix.postScale(factor, factor);
Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
Please refer to this post for a full example: ImageView fit without stretching the image
You must set android:scaleType="fitXY" to your imageView.
And if you want your ImageView be as large as its parent, you can write this lines of codes:
android:layout_width="match_parent"
android:layout_height="match_parent"
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
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.
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
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
I have an activity which consists of just an ImageView. The drawable contained in the ImageView is a Bitmap that was captured from the camera. The image is automatically scaled (maintaining the Bitmap's aspect ratio) so that the scaled width matches the width of the ImageView, and the image is centered on the screen vertically.
I need to figure out the coordinates of the top-left pixel of the actual drawable (not of the ImageView itself), but the ImageView class doesn't seem to give me a way of doing that.
I feel like I could potentially calculate it based on the dimensions of the original bitmap and the dimensions of the ImageView, but that would involve a lot of math that should be unnecessary, and would be prone to floating point errors.
Does anyone know of a way to find the coordinates of the top-left pixel of the Drawable relative to the ImageView?
Thanks.
Looking at the ImageView source code, it translates a Matrix halfway the vertical size (i.e., center it vertically), so you could retrieve it with the ImageView getImageMatrix() method and check the vertical translation by Matrix#getValues(float[]). If I read it right, it will be the sixth value.
I can't test it right now, but it would be something like:
Matrix matrix = iv.getImageMatrix ();
float[] values = new float[];
matrix.getValues(values);
float startY = values[5];