user3132603
user3132603

Reputation:

Crop image equal to imageView and show it in imageView

I want to show only some part of image in imageview. See following image .

enter image description here

How can i achieve it? i have tried several things. for example, i tried following code to re size the image resolution

// load the origial BitMap (500 x 500 px)
            Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
                   R.drawable.movie1);

            int width = bitmapOrg.getWidth();
            int height = bitmapOrg.getHeight();
            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);

            imageFav.setBackgroundDrawable(bmd);
                    imageFav.setScaleType(ScaleType.CENTER);

But using this stretches my image

Then i tried this thing from another question that was answered with pretty similar description

private static final Bitmap SOURCE_BITMAP = BitmapFactory.decodeFile(....); // Get the source Bitmap using your favorite method :-)
private static final int START_X = 10;
private static final int START_Y = 15;
private static final int WIDTH_PX = 100;
private static final int HEIGHT_PX = 100;

// Crop bitmap
Bitmap newBitmap = Bitmap.createBitmap(SOURCE_BITMAP, START_X, START_Y, WIDTH_PX, HEIGHT_PX, null, false);

// Assign new bitmap to ImageView
ImageView image = (ImageView)findViewById(R.id.image_view);
image.setImageBitmap(newBitmap);

But somehow i could not get it done. any links or answers would be appreciated

Upvotes: 0

Views: 3108

Answers (1)

slhddn
slhddn

Reputation: 1997

Have you checked this library?

https://github.com/biokys/cropimage

Upvotes: 1

Related Questions