我希望将某个特定的 Drawable
设为设备的壁纸,但所有的壁纸功能只接受 Bitmap
。由于我的环境版本低于2.1,因此无法使用 WallpaperManager
。
此外,我的绘图对象是从网络下载而来的,而不是存储在 R.drawable
中的。
我希望将某个特定的 Drawable
设为设备的壁纸,但所有的壁纸功能只接受 Bitmap
。由于我的环境版本低于2.1,因此无法使用 WallpaperManager
。
此外,我的绘图对象是从网络下载而来的,而不是存储在 R.drawable
中的。
这段代码会有所帮助。
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource);
这里是一个版本,其中图片被下载。
String name = c.getString(str_url);
URL url_value = new URL(name);
ImageView profile = (ImageView)v.findViewById(R.id.vdo_icon);
if (profile != null) {
Bitmap mIcon1 =
BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
profile.setImageBitmap(mIcon1);
}
BitmapFactory.decodeResource()
会返回null。 - John Glenpublic static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
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);
return bitmap;
}
getIntrinsicWidth()
和getIntrinsicHeight()
将返回-1。 - S.D.这将BitmapDrawable转换为Bitmap。
Drawable d = ImagesArrayList.get(0);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
BitmapDrawable
类型:if (d instanceof BitmapDrawable) {
Bitmap bitmap = ((BitmapDrawable)d).getBitmap(); }
- Tony ChanDrawable
可以绘制在Canvas
上,而Canvas
可以由Bitmap
支持:
(更新以处理BitmapDrawable
的快速转换并确保创建的Bitmap
具有有效大小)
public static Bitmap drawableToBitmap (Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
}
int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 1;
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 1;
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
BitmapDrawable
的快速路径会忽略可能的着色器、颜色过滤器和透明度。 - Miha_x64方法1:你可以直接像这样转换为位图
Bitmap myLogo = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
方法二: 您甚至可以将资源转换为drawable,然后从中获取位图,就像这样:
Bitmap myLogo = ((BitmapDrawable)getResources().getDrawable(R.drawable.logo)).getBitmap();
对于 API > 22,getDrawable
方法已经被移动到了 ResourcesCompat
类中,因此你可以像这样做:
对于 API > 22,getDrawable
方法已经被移动到了 ResourcesCompat
类中,因此你可以通过以下方式实现:
Bitmap myLogo = ((BitmapDrawable) ResourcesCompat.getDrawable(context.getResources(), R.drawable.logo, null)).getBitmap();
android.graphics.drawable.VectorDrawable 无法转换为 android.graphics.drawable.BitmapDrawable
。 - AdamHurwitzandroid-ktx有Drawable.toBitmap
方法:https://android.github.io/android-ktx/core-ktx/androidx.graphics.drawable/android.graphics.drawable.-drawable/to-bitmap.html
来自 Kotlin
val bitmap = myDrawable.toBitmap()
Bitmap mIcon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon);
// mImageView.setImageBitmap(mIcon);
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
// mImageView.setDrawable(mDrawable);
非常简单
Bitmap tempBMP = BitmapFactory.decodeResource(getResources(),R.drawable.image);
Drawable.toBitmap(...)
,可以将Drawable转换为Bitmap。DrawableKt.toBitmap(...);
- Balazs BanyaiDrawableKt.toBitmap(
mCornerTopLeftDrawable,
mCornerTopLeftDrawable.getIntrinsicWidth(),
mCornerTopLeftDrawable.getIntrinsicHeight(),
null
)
- Tamim Attafi看了(和使用)其他答案后,似乎它们都没有正确处理ColorDrawable
和PaintDrawable
。(特别是在棒棒糖版本上)看起来Shader
被调整以使单色块颜色不正确地处理。
我现在正在使用以下代码:
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
// We ask for the bounds if they have been set as they would be most
// correct, then we check we are > 0
final int width = !drawable.getBounds().isEmpty() ?
drawable.getBounds().width() : drawable.getIntrinsicWidth();
final int height = !drawable.getBounds().isEmpty() ?
drawable.getBounds().height() : drawable.getIntrinsicHeight();
// Now we check we are > 0
final Bitmap bitmap = Bitmap.createBitmap(width <= 0 ? 1 : width, height <= 0 ? 1 : height,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
与其他方法不同的是,如果在要求将Drawable
转换为位图之前对其调用setBounds
,它将以正确的大小绘制位图!
Drawable
没有设置边界,则使用IntrinsicWidth/Height
。如果它们都小于等于0,则将画布设置为1像素。如果Drawable
没有边界,您是正确的,它将传递一些(在大多数情况下为1x1),但是对于像ColorDrawable
这样没有内在大小的东西,这是必需的。
如果我们不这样做,它会抛出一个异常,因为您无法将0x0绘制到画布上。 - Chris.Jenkinsmutate()
会创建一个副本,保留原始的可绘制对象,这将消除传回原始边界的问题。我很少根据这些点更改代码。如果您的用例需要,请添加另一个答案。我建议您为位图缩放创建另一个问题。 - Chris.Jenkins