Android基础之R.drawable.***生成Drawa与Bitmap

这篇博客介绍了在Android中如何将R.drawable资源转换为Bitmap的四种方法,包括使用Resources的getDrawable方法、通过Inputstream和BitmapDrawable、BitmapFactory.decodeResource以及通过InputStream和BitmapFactory.decodeStream。

R.drawable.***中的文件是我们常用到的,不过有时候又没有直接的方法通过R文件获得图片,这时候就需要我们直接来转换一下了

下面提供四种方法给大家参考:

1、

 Resources resources = mContext.getResources();
Drawable drawable = resources.getDrawable(R.drawable.***);

2、

 Resources resources  = this.getContext().getResources();
Inputstream is = resources .openRawResource(R.drawable***);
BitmapDrawable  bmpDraw = new BitmapDrawable(is);
Bitmap bmp = bmpDraw.getBitmap();

3、

 Resources resources  = this.getContext().getResources();

 Bitmap bmp=BitmapFactory.decodeResource( resources , R.drawable.***);

 Bitmap newb = Bitmap.createBitmap( 300, 300, Config.ARGB_8888 ); 

4、

 InputStream is = getResources().openRawResource(R.drawable.***);  

 Bitmap mBitmap = BitmapFactory.decodeStream(is);

<think> 我们被要求回答:在Android中,如何实现`android.widget.ImageView`和`android.graphics.Bitmap`之间的转换? 根据用户提供的引用,我们可以总结出以下信息: 引用[1]和[2]提到了Glide加载图片时可能出现的Bitmap回收问题,其中涉及了ImageView和Bitmap的关系。 引用[3]提到了一个异常,在操作Bitmap时调用了虚方法,特别是bitmap.getWidth(),这可能在转换过程中用到。 引用[4]介绍了Drawable,它是Bitmap在UI上表现的一种形式。 转换方法: 1. 从ImageView获取Bitmap: 通常,ImageView并不直接提供获取Bitmap的方法,但我们可以通过其Drawable获取。如果ImageView的DrawableBitmapDrawable,那么我们就可以得到Bitmap。 但是要注意,如果图片是通过Glide等库加载的,可能会使用自定义的Drawable,所以需要判断类型。 2.Bitmap设置到ImageView: 这个比较简单,直接调用ImageView的setImageBitmap方法即可。 步骤: 一、ImageView 转 Bitmap 方法1:如果ImageView当前显示的DrawableBitmapDrawable,则可以直接获取BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); if (drawable != null) { Bitmap bitmap = drawable.getBitmap(); } 方法2:如果Drawable不是BitmapDrawable(比如是一个ShapeDrawable),我们可以创建一个Bitmap,然后在上面绘制这个DrawableDrawable drawable = imageView.getDrawable(); if (drawable != null) { Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); } 注意:方法2可能会因为Drawable没有固有尺寸(intrinsicWidth和intrinsicHeight)而出现问题,所以需要先检查。 二、Bitmap 转 ImageView 这个比较简单,因为ImageView有相应的方法: imageView.setImageBitmap(bitmap); 但是,根据引用[1]和[2]的警告,我们需要注意Bitmap的回收问题。在设置新的Bitmap之前,最好先检查旧的Bitmap是否已经被回收,避免出现“Canvas: trying to use a recycled bitmap”错误。 另外,引用[3]提醒我们,在操作Bitmap时,要注意空指针,比如在调用bitmap.getWidth()之前确保bitmap不为null。 因此,我们给出以下代码示例: 示例1:从ImageView获取Bitmap(安全方式) ```java // 获取ImageView中的Bitmap public static Bitmap getBitmapFromImageView(ImageView imageView) { if (imageView == null) { return null; } Drawable drawable = imageView.getDrawable(); if (drawable == null) { return null; } if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else { // 对于非BitmapDrawable,我们创建一个Bitmap并绘制 int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); // 如果Drawable没有固有尺寸,使用ImageView的尺寸 if (width <= 0) width = imageView.getWidth(); if (height <= 0) height = imageView.getHeight(); if (width <= 0 || height <= 0) { return null; } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, width, height); drawable.draw(canvas); return bitmap; } } ``` 示例2:将Bitmap设置到ImageView ```java // 设置Bitmap到ImageView public static void setBitmapToImageView(ImageView imageView, Bitmap bitmap) { if (imageView == null) { return; } // 避免使用已回收的Bitmap if (bitmap == null || bitmap.isRecycled()) { // 可以设置一个默认图片或者清除图片 imageView.setImageResource(R.drawable.default_image); return; } imageView.setImageBitmap(bitmap); } ``` 注意事项: 1. 从ImageView获取Bitmap时,如果Drawable不是BitmapDrawable,我们通过绘制创建Bitmap,但这可能会消耗较多内,特别是对于大图。 2. 在设置Bitmap之前检查是否已被回收,避免崩溃。 3.Android中,Bitmap占用内较大,使用后需要妥善管理,避免内泄漏。 另外,根据引用[1]和[2]提到的Glide问题,如果使用Glide加载图片并设置了自定义的ViewTarget,在设置Bitmap时也要注意回收问题。最好使用Glide的标准方式加载,避免直接操作Bitmap生成的相关问题: 1. 如何避免在ImageView和Bitmap转换过程中出现内泄漏? 2. 如何处理非BitmapDrawable(如VectorDrawable)的转换? 3. 如何高效地实现大尺寸Bitmap的转换? 4. 在转换过程中如何避免Bitmap被回收导致的异常? 5. 如何将VectorDrawable转换为Bitmap? </think> ### Android中ImageViewBitmap的转换方法 在Android开发中,`android.widget.ImageView`和`android.graphics.Bitmap`的相互转换是常见需求。以下是安全可靠的转换方法: #### 一、ImageView 转 Bitmap ```java // 方法1:通过Drawable转换(推荐) public static Bitmap imageViewToBitmap(ImageView imageView) { if (imageView == null) return null; Drawable drawable = imageView.getDrawable(); if (drawable instanceof BitmapDrawable) { Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); if (bitmap != null && !bitmap.isRecycled()) { // 检查是否已被回收 return bitmap.copy(Bitmap.Config.ARGB_8888, true); // 拷贝新对象 } } // 方法2:通过View绘制转换(适用于非BitmapDrawableBitmap bitmap = Bitmap.createBitmap( imageView.getWidth(), imageView.getHeight(), Bitmap.Config.ARGB_8888 ); Canvas canvas = new Canvas(bitmap); imageView.draw(canvas); return bitmap; } ``` **注意事项**: 1. 必须检查`bitmap.isRecycled()`避免使用已回收的位图[^1][^2] 2. 使用`copy()`创建新对象避免原始位图被修改 3. 矢量图转换需特殊处理 #### 二、Bitmap 转 ImageView ```java // 安全设置方法 public static void setBitmapToImageView(ImageView imageView, Bitmap bitmap) { if (bitmap == null || bitmap.isRecycled()) { // 处理异常情况:恢复默认图或清空 imageView.setImageResource(R.drawable.default_placeholder); return; } // 设置新图片前清空旧资源 imageView.setImageDrawable(null); // 设置新位图 imageView.setImageBitmap(bitmap); // 可选:设置缩放类型 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); } ``` **关键点**: 1. 设置前调用`setImageDrawable(null)`释放旧资源[^1] 2. 必须验证`bitmap.isRecycled()`防止崩溃 3. 避免直接使用`getDrawingCache()`(已被废弃) #### 三、Glide加载中的安全转换 ```kotlin Glide.with(context) .asBitmap() .load(url) .into(object : CustomTarget<Bitmap>() { override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) { // 安全设置位图 setBitmapToImageView(imageView, resource) } override fun onLoadCleared(placeholder: Drawable?) { imageView.setImageDrawable(null) // 清除引用 } }) ``` **最佳实践**: 1. 使用`CustomTarget`替代匿名内部类 2. 在`onLoadCleared`中主动清除资源[^2] 3. 禁用内:`.skipMemoryCache(true)`(特殊场景) #### 四、常见问题解决方案 1. **回收位图异常**: ```java if (!bitmap.isRecycled()) { // 安全操作 int width = bitmap.getWidth(); // 避免NullPointerException[^3] } ``` 2. **矢量图转换**: ```java // VectorDrawableBitmap VectorDrawable vectorDrawable = (VectorDrawable) imageView.getDrawable(); Bitmap bitmap = Bitmap.createBitmap( vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888 ); Canvas canvas = new Canvas(bitmap); vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); vectorDrawable.draw(canvas); ``` 3. **内优化**: ```java // 使用后回收资源 @Override protected void onDestroy() { super.onDestroy(); Bitmap bitmap = imageViewToBitmap(imageView); if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); } } ``` > **重要提示**:Android 8.0+系统已优化Bitmap管理,不建议手动调用`recycle()`,系统会自动管理资源。但跨版本开发仍需注意兼容性[^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值