How to create resizable ImageView in Android

荒凉一梦 提交于 2020-01-01 05:05:46

问题


I want a user of my app to be able to modify an image at run time. The user should be able to modify the image height and width by tapping on the corner of the image-view and dragging it as illustrated in the following diagram:

I have spent a lot of time researching this and I found Making Sense of Multitouch and Resize a large bitmap file to scaled output file something like Pinch Zoom but none of these quite match my requirements.

currently i am resizing bitmap using this below finction and changing width in onprogress change method of seekbar instead of frame. by using this function changing image size is not working smooth.

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 working smoothly using drawing bitmap in canvas using accepted answer of this question .

now reaming part is setting frame around image as something like crop frame.

Please help me to achieve this.


回答1:


you can accomplish this using this library use cropimageview3 and set your image in float drawable and you are done




回答2:


"You're doing it wrong".

Instead of creating new Bitmap each time, you have several options:

  1. Resize ImageView itself.
  2. Create your own View and draw bitmap using Canvas. This will let you stretch Btimap as you want.

If you'll go with Canvas, then to display those "crop-points" you can just also draw them on your canvas. Althought, to achieve desired behaviour and appearance you probably will need to override those methods as well:

  1. onTouchEvent - to handle your resize behaviour, when user touches crop points.
  2. onMeasure - to slightly increase size of your View, because your "crop-points" slightly comes out of image.


来源:https://stackoverflow.com/questions/17971884/how-to-create-resizable-imageview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!