从ImageView获取裁剪的图像

时间:2012-06-07 17:10:09

标签: android imageview crop

我使用ImageView裁剪位图。 一旦它被裁剪,我想得到裁剪的图像,但我得到与原始图像相同的图像。 有没有办法只获得图像的可视部分?

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

CroppedBitmap的值与OriginalBitmap一样。我怎样才能得到裁剪的那个?

2 个答案:

答案 0 :(得分:1)

example适用于我 iv.setDrawingCacheEnabled(true); Bitmap croppedBitmap = iv.getDrawingCache()

答案 1 :(得分: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);