【问题标题】:Getting a cropped image from ImageView从 ImageView 获取裁剪的图像
【发布时间】:2012-06-07 17:10:08
【问题描述】:

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

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

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

【问题讨论】:

  • 位图 CroppedBitmap= Bitmap.createBitmap(originalbitmap,bitmap.getWidth()/2 - bitmap.getHeight()/2, 0, bitmap.getWidth()/2 + bitmap.getHeight()/2 , bitmap.getHeight() );
  • 我能得到什么?我的 OriginalBitmap 减少了一半?
  • 您的确切需求是什么,您必须在哪个维度进行裁剪
  • 好的,我告诉用户选择一张自己的照片。然后在 100x100 ImageView 中绘制该文件。这就是我编写 ScaleType.CENTER_CROP 或其他修饰符的方式。有了这个,我应该让图片放大或缩小并居中。然后我想得到那部分图片。

标签: android imageview crop


【解决方案1】:

这个example 为我工作 iv.setDrawingCacheEnabled(true); Bitmap croppedBitmap = iv.getDrawingCache()

【讨论】:

    【解决方案2】:

    以下代码用于将图像裁剪为 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);
    

    【讨论】:

    • 谢谢,但图片比例失调。它变形。我需要像 ImageView.setScaleType(ScaleType.CENTER_CROP) 那样保持比例。
    猜你喜欢
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多