Sri
Sri

Reputation: 165

How to crop ImageView of drawable in Android

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.

Upvotes: 1

Views: 10841

Answers (4)

R3V0LUT10N 909
R3V0LUT10N 909

Reputation: 1560

<ImageView
    android:src="@drawable/image"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop" >
</ImageView>

Upvotes: 1

SceLus
SceLus

Reputation: 1107

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.

Upvotes: 0

Niranj Patel
Niranj Patel

Reputation: 33258

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);

Upvotes: 3

red_alert
red_alert

Reputation: 1728

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

Upvotes: 0

Related Questions