1

Is there any way to crop Drawable ImageView in Android. When I searched in google I found lot of examples to crop camera image or gallery image, but i didn't find any solution for ImageView and the source of that image is from my drawablefolder.

Please anyone help me to crop drawable image.

Thanks in advance.

Bhavin Nattar
  • 3,189
  • 2
  • 22
  • 30
Sri
  • 165
  • 2
  • 3
  • 12
  • http://androidroadies.blogspot.in/2013/05/crop-image-in-android.html. I already tried this but in this example it only crop the camera image and gallery image but not already displayed iamge. – Sri Jul 09 '13 at 10:33

4 Answers4

3

You can use createBitmap for crop and createScaledBitmap for scale bitmap.

Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width, height);

you can also scale image..

mBitmap  = Bitmap.createScaledBitmap(mBitmap, width, height, true);
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • What is the width and height here. I want the image same as default cropping, not static cropping. If we give static width and how user can crop? – Sri Jul 09 '13 at 10:46
  • I want dynamic cropping like in this example. http://androidroadies.blogspot.in/2013/05/crop-image-in-android.html – Sri Jul 09 '13 at 11:16
  • Any example on saving the server image on local folder and crop that image? – Sri Jul 15 '13 at 05:18
  • http://stackoverflow.com/questions/4875114/android-save-image-from-url-onto-sd-card – Niranj Patel Jul 15 '13 at 05:26
  • I am downloading image into "Myfolder" in sdcard and i want crop that image, any idea about crop image from particular folder not from gallery. – Sri Jul 16 '13 at 05:22
  • I searched but everyone gives the same answer like they took images from gallery but not from particular folder – Sri Jul 16 '13 at 06:46
1
<ImageView
    android:src="@drawable/image"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop" >
</ImageView>
Andrea Motto
  • 1,540
  • 21
  • 38
0

Have you tried to use createScaledBitmap? Take a look at this example https://stackoverflow.com/a/15789801/1342638

Community
  • 1
  • 1
red_alert
  • 1,738
  • 14
  • 24
  • I want to crop the image shown in this example. But the image should be from drawable. – Sri Jul 09 '13 at 10:35
  • In the example that I show to you, the Bitmap bmp is also loaded from drawables – red_alert Jul 09 '13 at 10:37
  • I want to crop the image dynamically not static. – Sri Jul 09 '13 at 10:47
  • http://www.youtube.com/watch?v=zhW7J_yK_yI like this but the image must be from drawable because later i will have an server image to crop. – Sri Jul 09 '13 at 11:31
  • I see...Take a look at this tutorial that I found: http://www.londatiga.net/featured-articles/how-to-select-and-crop-image-on-android/ – red_alert Jul 09 '13 at 13:08
  • how to crop image from particular folder in sdcard in android – Sri Jul 16 '13 at 06:26
0

Subclass the ImageView and override the onTouch method. Alternatively you can attach an onGestureDetector. Based on how you want it to behave use a matrix of transformation to scale or translate the image, loaded from your drawables. Finally enable the drawing cache of the view and use the getDrawingCache() method to obtain what you dynamically set for the ImageView to display.

SceLus
  • 1,117
  • 13
  • 18