tags:

views:

82

answers:

3

Hello friends I am in a serious problem I have an image in my res/drawable folder. I want to crop the image when loading in an imageview. But i don't know how to do that. i.e i want to slice out some part of the image. Please anyone help me to do that Thankyou.

+1  A: 

Hai this may help u,

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
           R.drawable.android);

    int width = bitmapOrg.width();
    int height = bitmapOrg.height();
    int newWidth = 200;
    int newHeight = 200;

    // calculate the scale - in this case = 0.4f
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
                      width, height, matrix, true); 

    // make a Drawable from Bitmap to allow to set the BitMap 
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

    ImageView imageView = new ImageView(this);

    // set the Drawable on the ImageView
    imageView.setImageDrawable(bmd);
Tilsan The Fighter
Thanks for the code.I ran the above code and i got a scaled image.Actually what i want was a cropped image.can you modify the code for that.please...
Jim
@Jim: u just remove these lines // calculate the scale - in this case = 0.4f float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // createa matrix for the manipulation Matrix matrix = new Matrix(); // resize the bit map matrix.postScale(scaleWidth, scaleHeight);
Tilsan The Fighter
its only resizing not cropping.......you are saying that there is no modification in the above code,just removing some lines.I can't understand.can u explain...
Jim
A: 

If you want to equally crop the outside of the image, you should check out the ScaleType attribute for an ImageView: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

In particular, you would be interested in the "centerCrop" option. It crops out part of the image that is larger than the defined size.

Here's an example of doing this in the XML layout:

<ImageView  android:id="@+id/title_logo"
            android:src="@drawable/logo"
            android:scaleType="centerCrop" android:padding="4dip"/>
Josh Clemm
A: 
 int targetWidth = 100;
 int targetHeight = 100;
 Bitmap targetBitmap = Bitmap.createBitmap(
 targetWidth, targetHeight,Bitmap.Config.ARGB_8888);
 Canvas canvas = new Canvas(targetBitmap);
 Path path = new Path();
 path.addRect(rectf, Path.Direction.CW);
 canvas.clipPath(path);
 canvas.drawBitmap(
 sourceBitmap,
 new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
 new Rect(0, 0, targetWidth, targetHeight),
 null);
 ImageView imageView = (ImageView)findViewById(R.id.my_image_view);
 imageView.setImageBitmap(targetBitmap);
Tilsan The Fighter