我想将某个设置Drawable
为设备的墙纸,但所有墙纸功能Bitmap
仅接受。我无法使用,WallpaperManager
因为我是2.1之前的版本。
另外,我的可绘制对象是从网络下载的,并不位于中R.drawable
。
我想将某个设置Drawable
为设备的墙纸,但所有墙纸功能Bitmap
仅接受。我无法使用,WallpaperManager
因为我是2.1之前的版本。
另外,我的可绘制对象是从网络下载的,并不位于中R.drawable
。
Answers:
这段代码有帮助。
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);
}
public 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()
,getIntrinsicHieght()
它将返回-1。
这会将BitmapDrawable转换为Bitmap。
Drawable d = ImagesArrayList.get(0);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
BitmapDrawable
强制类型转换之前的代码: if (d instanceof BitmapDrawable) { Bitmap bitmap = ((BitmapDrawable)d).getBitmap(); }
d
已经是 a的BitmapDrawable
情况下才有效,在这种情况下,将其作为位图进行检索很简单…… ClassCastException
在所有其他情况下都会崩溃。
一个Drawable
可以拉伸到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;
}
方法1:可以像这样直接转换为位图
Bitmap myLogo = BitmapFactory.decodeResource(context.getResources(), R.drawable.my_drawable);
方法2:您甚至可以将资源转换为可绘制对象,并从中获得像这样的位图
Bitmap myLogo = ((BitmapDrawable)getResources().getDrawable(R.drawable.logo)).getBitmap();
对于API> 22 getDrawable
方法,将其移至ResourcesCompat
该类,以便您执行以下操作
Bitmap myLogo = ((BitmapDrawable) ResourcesCompat.getDrawable(context.getResources(), R.drawable.logo, null)).getBitmap();
android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
很简单
Bitmap tempBMP = BitmapFactory.decodeResource(getResources(),R.drawable.image);
所以其他的答案中寻找(并使用)之后,他们似乎都处理ColorDrawable
和PaintDrawable
严重。(特别是在棒棒糖上)似乎已对Shader
s进行了调整,因此未正确处理纯色块。
我现在正在使用以下代码:
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;
}
不像其他人,如果你拨打setBounds
的Drawable
要求把它变成一个位图前,将以此为位图以正确的尺寸!
Drawable
没有设置边界,则使用IntrinsicWidth/Height
。如果它们均<= 0,则将画布设置为1px。您是正确的,如果Drawable
没有界限,它将通过一些(大多数情况下为1x1),但这对于诸如ColorDrawable
没有固有尺寸的事情是必需的。如果我们不这样做,它将抛出一个Exception
,您将无法在画布上绘制0x0。
mutate()
会制作一个副本,而将原始可绘制对象保留下来,这将消除在原始范围内传回的问题。我很少根据这些观点来更改代码。如果您的用例需要它,请添加另一个答案。我建议您为位图缩放创建另一个问题。
也许这会帮助某人...
从PictureDrawable到Bitmap,使用:
private Bitmap pictureDrawableToBitmap(PictureDrawable pictureDrawable){
Bitmap bmp = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(), pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawPicture(pictureDrawable.getPicture());
return bmp;
}
...的实现方式如下:
Bitmap bmp = pictureDrawableToBitmap((PictureDrawable) drawable);
Drawable
,在这种情况下为PictureDrawable
。
这是更好的分辨率
public static Bitmap drawableToBitmap (Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
public static InputStream bitmapToInputStream(Bitmap bitmap) {
int size = bitmap.getHeight() * bitmap.getRowBytes();
ByteBuffer buffer = ByteBuffer.allocate(size);
bitmap.copyPixelsToBuffer(buffer);
return new ByteArrayInputStream(buffer.array());
}
1)可绘制到位图:
Bitmap mIcon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon);
// mImageView.setImageBitmap(mIcon);
2)位图到Drawable:
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
// mImageView.setDrawable(mDrawable);
这是@ Chris.Jenkins在这里提供的答案的不错的Kotlin版本:https ://stackoverflow.com/a/27543712/1016462
fun Drawable.toBitmap(): Bitmap {
if (this is BitmapDrawable) {
return bitmap
}
val width = if (bounds.isEmpty) intrinsicWidth else bounds.width()
val height = if (bounds.isEmpty) intrinsicHeight else bounds.height()
return Bitmap.createBitmap(width.nonZero(), height.nonZero(), Bitmap.Config.ARGB_8888).also {
val canvas = Canvas(it)
setBounds(0, 0, canvas.width, canvas.height)
draw(canvas)
}
}
private fun Int.nonZero() = if (this <= 0) 1 else this
Android提供了一种非直截了当的解决方案:BitmapDrawable
。要获取位图,我们必须将资源ID提供R.drawable.flower_pic
给a BitmapDrawable
,然后将其转换为a Bitmap
。
Bitmap bm = ((BitmapDrawable) getResources().getDrawable(R.drawable.flower_pic)).getBitmap();
使用此代码。它将帮助您实现目标。
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.profileimage);
if (bmp!=null) {
Bitmap bitmap_round=getRoundedShape(bmp);
if (bitmap_round!=null) {
profileimage.setImageBitmap(bitmap_round);
}
}
public Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
int targetWidth = 100;
int targetHeight = 100;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1) / 2,
((float) targetHeight - 1) / 2,
(Math.min(((float) targetWidth),
((float) targetHeight)) / 2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth, targetHeight), new Paint(Paint.FILTER_BITMAP_FLAG));
return targetBitmap;
}
BitmapFactory.decodeResource()
自动缩放位图,因此您的位图可能变得模糊。为防止缩放,请执行以下操作:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(context.getResources(),
R.drawable.resource_name, options);
要么
InputStream is = context.getResources().openRawResource(R.drawable.resource_name)
bitmap = BitmapFactory.decodeStream(is);
如果您使用的是kotlin,请使用以下代码。会的
//用于使用图像路径
val image = Drawable.createFromPath(path)
val bitmap = (image as BitmapDrawable).bitmap
最新的androidx核心库(androidx.core:core-ktx:1.2.0)现在具有扩展功能:Drawable.toBitmap(...)
将Drawable转换为位图。
// get image path from gallery
protected void onActivityResult(int requestCode, int resultcode, Intent intent) {
super.onActivityResult(requestCode, resultcode, intent);
if (requestCode == 1) {
if (intent != null && resultcode == RESULT_OK) {
Uri selectedImage = intent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
//display image using BitmapFactory
cursor.close(); bmp = BitmapFactory.decodeFile(filepath);
iv.setBackgroundResource(0);
iv.setImageBitmap(bmp);
}
}
}
ImageWorker库可以将位图转换为drawable或base64,反之亦然。
val bitmap: Bitmap? = ImageWorker.convert().drawableToBitmap(sourceDrawable)
实作
在项目级别Gradle中
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
在应用程序级别中
dependencies {
implementation 'com.github.1AboveAll:ImageWorker:0.51'
}
您还可以从外部存储和检索位图/可绘制对象/ base64图像。
在这里检查。https://github.com/1AboveAll/ImageWorker/edit/master/README.md