一、Drawable 转换成 Bitmap
通过 BitmapFactory 中的 decodeResource 方法,将资源文件中的 R.drawable.ic_drawable 转化成Bitmap
Resources res = getResources()
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.ic_drawable)
将 Drable 对象先转化成 BitmapDrawable ,然后调用 getBitmap 方法 获取
Resource res = gerResource()
Drawable drawable = res.getDrawable(R.drawable.ic_drawable)
BitmapDrawable bd = (BitmapDrawable) drawable
Bitmap bm = bd.getBitmap()
根据已有的Drawable创建一个新的Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
int w = drawable.getIntrinsicWidth()
int h = drawable.getIntrinsicHeight()
System.out.println("Drawable转Bitmap")
Bitmap.Config config =
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565
Bitmap bitmap = Bitmap.createBitmap(w, h, config)
//注意,下面三行代码要用到,否则在View或者SurfaceView里的canvas.drawBitmap会看不到图
Canvas canvas = new Canvas(bitmap)
drawable.setBounds(0, 0, w, h)
drawable.draw(canvas)
return bitmap
二、Bitmap 转换成 Drawable
使用 BitmapDrawable 对 Bitmap 进行强制转换
Drawable drawable = new BitmapDrawable(bmp);
三、Bitmap 转换成 byte[]
public static byte[] getBytes(Bitmap bitmap){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
四、byte[] 转化成 Bitmap
public static Bitmap Bytes2Bimap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
Drawable —> Bitmap获取资源文件(Drawable)中的 BitmapResources res = getResources();Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.ic_logo);或者Resource res = gerResource();Drawable drawable
BitmapDrawablebd=new BitmapDrawable(bm);
Android开发网提示因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
二、 Drawable转Bitmap
转成Bitmap对象后,可以将Drawable对象通过Android的SK库存成一个字节输出流,最终还可以保存成为jpg和png的文件。
Drawable ...
本文实例讲述了android图片类型之间相互转换实现代码。分享给大家供大家参考。具体如下:
android在处理一写图片资源的时候,会进行一些类型的转换,现在有空整理一下:
1、Drawable → Bitmap
Java代码如下:
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawa
很多开发者表示,不知道Android的Drawable和Bitmap之间如何相关转换。下面Android123给大家两种比较简单高效的方法。
一、Bitmap转Drawable 代码如下: Bitmap bm=xxx; //xxx根据你的情况获取
BitmapDrawable bd=BitmapDrawable(bm); Android开发网提示因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
二、 Drawable转Bitmap
转成Bitmap对象后,可以将Drawable对象通过Android的SK库存成一个字节输出流,最终还可以保
众所周知,决定一个Android应用是否可以被用户接受最重要的方面就是用户界面,为了让我们的Android给用户提供一个更友好的界面,就需要我们在应用中使用和插入图片了。Android系统提供了丰富的图片功能支持,其中就包括处理静态图片和动画等等。
一、使用简单的图片
(1)使用Drawable对象
为Android应用添加了Drawable资源之后,Android SDK会为这份资源在R...
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.sample_0);
Resources res=getResources();
private byte[] Bitmap2Bytes(Bitmap bm){
bitmap和Drawable间的区别:
Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565、RGB888。作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低。我们理解为一种存储对象比较好。
Drawable - 作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF、PNG、JPG,当然也支持BMP,当然还提供一些高
Resources res = getResources();
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.ic_drawable);
二、Bitmap 转换成 Drawable
Drawable drawable = new BitmapDrawable(bmp);...
``` java
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
其中,R.drawable.image是要转换的drawable资源。可以将其放在ImageView或Bitmap对象中使用。
Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated
app-release-unsigned.apk is not signed