裁剪图像等于imageView并在imageView中显示

时间:2014-01-27 13:51:14

标签: android image crop

我想在imageview中只显示图像的某些部分。见下图。

enter image description here

我怎样才能实现它?我尝试了几件事。例如,我尝试使用代码来重新调整图像分辨率

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

但使用它可以拉伸我的图像

然后我尝试了另一个用非常相似的描述回答的问题

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

但不知怎的,我无法完成它。任何链接或答案将不胜感激

1 个答案:

答案 0 :(得分:1)

你检查过这个图书馆吗?

https://github.com/biokys/cropimage