Android 将本地资源图片转换成Drawable,进行设置大小操作
作者:欢乐的乐 时间:2023-06-19 17:38:55
前言:
因为项目中显示图片是用Picasso,设置placeholder和error图片的时候发现,本地图片的大小无法满足我的需求,需要先对图片大小改变再显示。
Picasso的placeholder和error的参数也只有int resId和Drawable drawable
于是打算将改变过大小的Drawable传进入显示,咦,效果很满意!
整个过程的思路:
将本地图片(R.drawable.image)变成Drawable对象
将Drawable对象转换成Bitmap对象
将Bitmap对象根据指定大小创建一个新的Bitmap对象
将Bitmap对象转换成Drawable对象
代码:
1. 将本地图片(R.drawable.image)变成Drawable对象
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.image);
2. 将Drawable对象转换成Bitmap对象
/**
* 将Drawable转换为Bitmap
* @param drawable
* @return
*/
private Bitmap drawableToBitmap(Drawable drawable) {
//取drawable的宽高
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
//取drawable的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE
? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
//创建对应的bitmap
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
//创建对应的bitmap的画布
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, width, height);
//把drawable内容画到画布中
drawable.draw(canvas);
return bitmap;
}
3. 整个流程的执行
后来发现一个更简洁的方法
/**
* 将本地资源图片大小缩放
* @param resId
* @param w
* @param h
* @return
*/
public Drawable zoomImage(int resId, int w, int h){
Resources res = mContext.getResources();
Bitmap oldBmp = BitmapFactory.decodeResource(res, resId);
Bitmap newBmp = Bitmap.createScaledBitmap(oldBmp,w, h, true);
Drawable drawable = new BitmapDrawable(res, newBmp);
return drawable;
}
原来复杂的思路
/**
* 缩放Drawable
*@drawable 原来的Drawable
*@w 指定的宽
*@h 指定的高
*/
public Drawable zoomDrawable(Drawable drawable, int w, int h){
//获取原来Drawable的宽高
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
//将Drawable转换成Bitmap
Bitmap oldbmp = drawableToBitmap(drawable);
//计算scale
Matrix matrix = new Matrix();
float scaleWidth = ((float)w/width);
float scaleHeight = ((float)h/height);
matrix.postScale(scaleWidth, scaleHeight);
//生成新的Bitmap
Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);
//设置bitmap转成drawable后尺寸不变
//这个很关键后面解释!!
DisplayMetrics metrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(metrics);
Resources resources = new Resources(mContext.getAssets(), metrics, null);
return new BitmapDrawable(resources, newbmp);
}
学习中遇到的问题
看网上的教程是没有下面
DisplayMetrics metrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(metrics);
Resources resources = new Resources(mContext.getAssets(), metrics, null);
这段代码的。
假如我指定的宽高是200,生成的Drawable的宽高却只有100。
原来Bitmap转换成Drawable的尺寸是会变小的。
那段代码就能解决尺寸变小的问题。
如果路过的大神有更好的方法,希望能指点一下小白。
补充知识:Android中如何将res里的图片转换成Bitmap.
1.复制代码即可:
Resources res = MainActivity.this.getResources();
Bitmap bmp= BitmapFactory.decodeResource(res, R.mipmap.flower);
来源:https://www.jianshu.com/p/d3ff021b7fec
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android中Blade的使用方法
Java中EasyPoi多sheet导出功能实现
![](https://img.aspxhome.com/file/2023/5/108945_0s.png)
C#设计模式之适配器模式与装饰器模式的实现
![](https://img.aspxhome.com/file/2023/8/108718_0s.png)
Android仿支付宝支付从底部弹窗效果
![](https://img.aspxhome.com/file/2023/7/123307_0s.gif)
Android开发之TabActivity用法实例详解
![](https://img.aspxhome.com/file/2023/0/139210_0s.jpg)
Android开发返回键明暗点击效果的实例代码
![](https://img.aspxhome.com/file/2023/2/137802_0s.png)
Android App开发中自定义View和ViewGroup的实例教程
![](https://img.aspxhome.com/file/2023/5/138555_0s.png)
Spring Boot 集成MyBatis 教程详解
![](https://img.aspxhome.com/file/2023/4/64114_0s.jpg)
Android实现音量调节的方法
![](https://img.aspxhome.com/file/2023/9/123299_0s.png)
Spring Boot高级教程之使用Redis实现session共享
JavaWeb搭建网上图书商城毕业设计
![](https://img.aspxhome.com/file/2023/6/70976_0s.png)
Android 实现会旋转的饼状统计图实例代码
![](https://img.aspxhome.com/file/2023/7/137687_0s.gif)
Android编程实现将ButtonBar放在屏幕底部的方法
![](https://img.aspxhome.com/file/2023/6/106876_0s.png)
Java面试题冲刺第二十三天--分布式
![](https://img.aspxhome.com/file/2023/8/58588_0s.png)
Android 复制文本内容到系统剪贴板的最简单实例(分享)
![](https://img.aspxhome.com/file/2023/7/138957_0s.png)
浅谈Java方法调用的优先级问题
android 网络请求库volley方法详解
![](https://img.aspxhome.com/file/2023/4/137194_0s.png)
C#实现自定义单选和复选按钮样式
![](https://img.aspxhome.com/file/2023/2/87032_0s.png)
详解如何热更新线上的Java服务器代码
C#实现拼手气红包算法
![](https://img.aspxhome.com/file/2023/1/104301_0s.jpg)