android - 生成 ImageView 可绘制的缩放版本
问题描述
我想生成来自 imageView 的可绘制的缩放版本,并将其显示为对话框的小图标。我正在使用此代码,但图片保持其初始大小
Drawable drw = image.getDrawable().mutate().getConstantState().newDrawable();
Drawable drwScal = new ScaleDrawable(drw, 0, 0.5f, 0.5f).getDrawable();
drwScal.setLevel(5000);
new MaterialDialog.Builder(context)
.title(String.format(getResources().getString(R.string.summary)))
.icon(drwScal)
.content(sommaire)
.forceStacking(true)
.show();
怎么了 ?
解决方案
您可以将 ImageView 的内容作为位图并像这样缩放位图本身:
BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
Bitmap bitmap = drawable.getBitmap();
private static Bitmap scaleBitmap(Bitmap bitmap, float scaleFactor) {
final int sizeX = Math.round(bitmap.getWidth() * scaleFactor);
final int sizeY = Math.round(bitmap.getHeight() * scaleFactor);
return Bitmap.createScaledBitmap(bitmap, sizeX, sizeY, false);
}
推荐阅读
- maven - com.google.api 导入“无法解决”AEM 6.4
- angular - Jasmine 错误:尽管实现了满足条件的逻辑,但测试失败
- java - 为什么会为相同的子字符串生成两个不同的哈希值,我能做些什么来解决这个问题?
- java - JAVA - 休眠 - 在 DAO 中创建新查询
- json - 解组并从 json 数组中获取第 n 项
- javascript - 使用 javascript 定期清除 PWA 的缓存
- node.js - Hyperledger Fabric - Fabcar 性能
- java - String intern() 方法有什么作用?
- python - Django 模板中的表单自定义
- sql - SQL表中如何按规则拆分行?