如何将位图图像转换为可绘制?
听起来你想要使用BitmapDrawable
从文档中可以看到:
一个包装位图和罐头的Drawable 平铺、拉伸或对齐。你 可以创建一个BitmapDrawable 文件路径,一个输入流,通过 XML膨胀,或从位图 对象。
已经看到了大量的问题与位图不正确缩放时,转换为BitmapDrawable,一般的转换方式应该是:
Drawable d = new BitmapDrawable(getResources(), bitmap);
如果没有资源引用,位图可能无法正确呈现,即使缩放正确。这里有许多问题可以通过使用这个方法而不是直接调用位图参数来解决。
如果你有一个位图图像,你想用它来绘图,比如
Bitmap contact_pic; //a picture to show in drawable
drawable = new BitmapDrawable(contact_pic);
官方位图绘制文档
这是一个关于如何将位图转换为可绘制的示例
Bitmap bitmap;
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(drawable);
只要这样做:
private void setImg(ImageView mImageView, Bitmap bitmap) {
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
mImageView.setDrawable(mDrawable);
}
我使用了上下文
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
这是另一个:
Drawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
隐蔽位映射绘制在草图应用程序使用代码
android.graphics.drawable.BitmapDrawable d = new android.graphics.drawable.BitmapDrawable(getResources(), bitmap);
1)位图到可绘制:
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
// mImageView.setDrawable(mDrawable);
2)可绘制位图:
Bitmap mIcon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource);
// mImageView.setImageBitmap(mIcon);
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件