//方法一
Resources res;
InputStream is=res.openRawResource(R.drawable.pic);
BitmapDrawable bitmapDrawable=new BitmapDrawable(is);
Bitmap bmp=bitmapDrawable.getBitmap();
//方法二
Resources res;
BitmapDrawable bitmapDrawable=(BitmapDrawable)res.getDrawable(R.drawable.pic);
Bitmap bmp=bitmapDrawable.getBitmap();
//方法三
ImageView image;
image.setImageBitmap(BitmapFactory.decodeStream(~~~~));
BitmapDrawable bitmapDrawable=(BitmapDrawable)image.getDrawable();
Bitmap bmp=bitmapDrawable.getBitmap();
2. Bitmap --> Drawable
Drawable d = new BitmapDrawable(bitmap);Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
② 通过BitmapFactory.decodeStream方法,创建出一个bitmap (推荐)
InputStream is = context.getResources().openRawResource(R.drawable.app_sample_code);
Bitmap bmp = BitmapFactory.decodeStream(is);
4. Bitmap --> byte[]
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream byteos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, byteos);
return byteos.toByteArray();
}
5. byte[] --> Bitmap
private Bitmap Bytes2Bimap(byte[] byte){
if(byte.length!=0){
return BitmapFactory.decodeByteArray(byte, 0, byte.length);
}
else {
return null;
}
}
5.id转化graphic.drawable
Drawable drawable = activity.getResources().getDrawable(R.drawable.icon);
6.id转化成Bitmap
Bitmap bitmap = BitmapFactory. decodeResource (Resources res, int id)
public static Bitmap scaleBitmap(Bitmap bitmap, int scalWidth, int scaleHeight) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// 创建操作图片用的Matrix对象
Matrix matrix = new Matrix();
// 计算缩放比例
float sx= ((float) scaleWidth / w);
float sy= ((float) scaleHeight / h);
// 设置缩放比例
matrix.postScale(sx, sy);
// 建立新的bitmap,其内容是对原bitmap的缩放后的图
Bitmap scaleBmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
return scaleBmp;
}
Matrix类的其他典型方法。
boolean postScale(float sx, float sy)//缩放
boolean postSkew(float kx, float ky)//扭曲
boolean postTranslate(float dx, float dy)//转换
boolean preConcat(Matrix other)//合并
boolean preRotate(float degrees)//旋转