How to create resizable ImageView in Android

I want the user of my application to be able to change the image at runtime. The user should be able to change the height and width of the image by clicking on the corner of the image and dragging it, as shown in the following diagram:

enter image description here

I spent a lot of time exploring this, and I found Creating a multi-touch feel and Resize a large raster image file to a scaled output file , such as Pinch Zoom, but none of them meet my requirements.

I am currently resizing the bitmap using this lower value and changing the width in the onprogress change method for the search, not the frame. Using this function, resizing an image does not work smoothly.

public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){ try { //Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(f),null,o); //The new size we want to scale to final int REQUIRED_WIDTH=WIDTH; final int REQUIRED_HIGHT=HIGHT; //Find the correct scale value. It should be the power of 2. int scale=1; while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT) scale*=2; //Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize=scale; return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); } catch (FileNotFoundException e) {} return null; } 

UPDATE:

Now we work smoothly, using the drawing of a raster image in the canvas, using the accepted answer to this question.

now the expanding part sets the frame around the image as something like a cropping frame.

Please help me achieve this.

+8
android bitmap image-resizing
source share
2 answers

you can do this using this library using cropimageview3 and set your image to float drawable and you're done

+1
source share

"You are doing it wrong."

Instead of creating a new Bitmap every time you have several options:

  • Resize ImageView .
  • Create your own View and draw a bitmap using Canvas . This will allow you to stretch Btimap as you wish.

If you go with Canvas , you can simply draw them on your canvas to display these β€œscraps”. Although, in order to achieve the desired behavior and appearance, you probably also have to override these methods:

  • onTouchEvent - to handle your resize behavior when the user touches crop points.
  • onMeasure - slightly increase the size of your View , because your "crop" a little out of the image.
+1
source share

All Articles