4

我使用 ImageView 裁剪位图。裁剪后,我想获得裁剪后的图像,但得到的图像与原始图像相同。有没有办法只获取图像的可见部分?

ImageView iv = new  ImageView(this);                        
iv.setImageBitmap(OriginalBitmap);
iv.setScaleType(ScaleType.CENTER_CROP);
Bitmap CroppedBitmap = ((BitmapDrawable) iv.getDrawable()).getBitmap();

CroppedBitmap 的值与 OriginalBitmap 相同。我怎样才能得到裁剪的?

4

2 回答 2

1

这个例子对我有用 iv.setDrawingCacheEnabled(true); Bitmap croppedBitmap = iv.getDrawingCache()

于 2015-08-24T08:31:06.537 回答
0

以下代码用于裁剪图像 200x200

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

    iv.setImageDrawable(bmd);
于 2012-06-07T17:16:47.710 回答