Bitmap和Drawable之间的转换
来源:互联网 发布:coc暗黑训练营升级数据 编辑:程序博客网 时间:2024/05/22 03:15
文件布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>
源代码:
package com.example.bitmapanddrawable;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat
;import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView image;
@Overrideprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
image.setImageBitmap(convertDrawable2BitmapByCanvas(drawable));
}
public Bitmap convertDrawable2BitmapByCanvas(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),drawable.getOpacity() !=
PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());drawable.draw(canvas);return bitmap;
}
public Drawable convertBitmap2Drawable(Bitmap bitmap) {
BitmapDrawable bd = new BitmapDrawable(this.getResources(), bitmap);
// 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
return bd;
}
public Bitmap convertDrawable2BitmapSimple(Drawable drawable) {
BitmapDrawable bd = (BitmapDrawable) drawable;
return bd.getBitmap();
}
// 从资源中获取Bitmap
public static Bitmap getBitmapFromResources(Activity act, int resId) {
Resources res = act.getResources();
return BitmapFactory.decodeResource(res, resId);
}
// byte[] → Bitmap
public static Bitmap convertBytes2Bimap(byte[] b) {
if (b.length == 0) {
return null;
}
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
// Bitmap → byte[]
public static byte[] convertBitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
}
- Drawable、Bitmap和byte[]之间的转换
- Bitmap和Drawable之间的相互转换
- Resource,Drawable和Bitmap之间的转换
- Bitmap和Drawable之间的转换
- Resource,Drawable和Bitmap之间的转换
- Resource,Drawable和Bitmap之间的转换
- 简单谈谈Resource,Drawable和Bitmap之间的转换
- android中bitmap和drawable之间的转换
- Android Bitmap 与 Drawable之间的区别和转换
- 获取系统图片资源 drawable和bitmap之间的转换
- Android开发实用技巧:Drawable和Bitmap之间的转换
- Android Bitmap 与 Drawable之间的区别和转换
- Android Bitmap 与 Drawable之间的区别和转换
- Android Bitmap 与 Drawable之间的区别和转换
- 简单谈谈Resource,Drawable和Bitmap之间的转换
- 简单谈谈Resource,Drawable和Bitmap之间的转换
- Android Drawable和Bitmap图片之间转换
- Android中Drawable和Bitmap 之间转换
- 一些设计图--关于代码生成
- java中的Math类
- 算法随想
- 支持向量机SVM分析
- poj 3159 Candies------spfa算法
- Bitmap和Drawable之间的转换
- Linux下安装启动多个Tomcat
- 介绍几本专业的书籍,一起学习
- Gift1解题报告
- JSTL-fmt标签库
- linux md5sum命令用法
- BMP文件结构
- Scheduler and Timer Callback
- Dualpal解题报告