【问题标题】:How to get the whole bitmap attached to an ImageView?如何将整个位图附加到 ImageView?
【发布时间】:2012-03-16 17:22:26
【问题描述】:

我尝试使用ImageView.getDrawingCache(); 将位图附加到 ImageView,但我发现返回的位图与我想从 ImageView 中获取的位图不同。它总是比真实图像小。

我知道,getDrawingCache() 方法不应该有视图,如果它比屏幕大,因为视图的可见部分只被绘制,缓存只保存绘制的内容。

我可以将整个位图附加到 ImageView 吗?

【问题讨论】:

    标签: android bitmap imageview


    【解决方案1】:

    如果您只想要来自ImageViewBitmap,以下代码可能对您有用:-

    Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
    

    我想这就是你想要的。

    【讨论】:

    • 我总是得到 NPE:尝试调用虚拟方法 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()'。
    • 有时 getDrawable 可能会返回其他对象..您可能会以对象转换异常结束。
    • @josher932 您可能需要检查 mipreamble 对此的回答。但是,我建议您检查为什么会收到 Null 指针,因为它不正常。
    • @Ravi 它显然会返回一个Drawable,但它可能不是BitmapDrawable类的对象。因此,您需要检查您得到的是哪种 Drawable。 mipremble 的答案在这方面要好得多。
    【解决方案2】:

    如果你的drawble并不总是BitmapDrawable的实例

    注意:在执行此操作之前应设置 ImageView。

    Bitmap bitmap;
    if (mImageView.getDrawable() instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
    } else {
        Drawable d = mImageView.getDrawable();
        bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        d.draw(canvas);
    }
    

    您的位图存储在位图中。

    瞧!

    【讨论】:

      【解决方案3】:

      最简单的方法是在ImageView中设置标签。

      imageView.setImageBitmap(bitmap);
      imageView.setTag(bitmap); 
      

      从中获取标签

      imageView.getTag();
      

      【讨论】:

        猜你喜欢
        • 2012-01-08
        • 2011-06-10
        • 2012-12-14
        • 2011-03-17
        • 1970-01-01
        • 2015-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多