本文将介绍几种Android常用的图片的转化方法
(一)bitmap转换成drawable
Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB565、RGB888。作为一种逐像素的显示对象执行效率高,但是缺点也很明显存储效率低。我们理解为一种存储对象比较好。
Drawable - 作为Android平下通用的图形对象,它可以装载常用格式的图像,比如GIF、PNG、JPG,当然也支持BMP,当然还提供一些高级的可视化对象,比如渐变、图形等。
两者间的简单对比:
对比项 | 清晰度 | 占用内存 | 支持缩放 | 支持羞色相差调整 | 支持旋转 | 支持透明色 | 绘制速度 | 支持像素操作 |
---|---|---|---|---|---|---|---|---|
Bitmap | 相同 | 大 | 是 | 是 | 是 | 是 | 慢 | 是 |
Drawable | 相同 | 小 | 是 | 否 | 是 | 是 | 快 | 否 |
Drawable在内存占用和绘制速度这两个非常关键的点上胜过Bitmap;
public static Drawable bitmap2Drawable(Context context,Bitmap bitmap){
// 设置bitmap转成drawable后尺寸不变
// DisplayMetrics metrics = new DisplayMetrics();
DisplayMetrics metrics = context.getApplicationContext().getResources().getDisplayMetrics();
// ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
Resources resources = new Resources(context.getApplicationContext().getAssets(), metrics, null);
// bitmap转drawable
Drawable drawable = new BitmapDrawable(resources, bitmap);
int scaleW = (int) (drawable.getIntrinsicWidth() / 2 * metrics.density);
int scaleH = (int) (drawable.getIntrinsicHeight() / 2 * metrics.density);
drawable.setBounds(0, 0, scaleW, scaleH);
return drawable;
}
(二)InputStream转换成Bitmap
InputStream的作用是标志那些从不同数据起源产生输入的类。这些数据起源包括(每个都有一个相关的InputStream子类)。
public static Bitmap InputStream2Bitmap(InputStream is) {
BitmapFactory.Options opt = new Options();
opt.inTempStorage = new byte[100 * 1024];
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
try {
return BitmapFactory.decodeStream(is,null,opt);
} catch (Exception e) {
e.printStackTrace();
}catch (OutOfMemoryError e) {
e.printStackTrace();
}
return null;
}
(三)Java生成 shape和select
public static StateListDrawable pressedAndEnabledBg(Drawable pressed,Drawable enabled,
Drawable normal) {
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[] { android.R.attr.state_pressed }, pressed);
drawable.addState(new int[] { android.R.attr.state_enabled }, enabled);
drawable.addState(new int[] {}, normal);
return drawable;
}
public static Drawable shapeDrawable(Context context, float radiussize,
double strokewidth, String solidcolor, String strokecolor) {
int dis = DensityUtil.dip2px(context, radiussize);
float sw = (float) strokewidth;
int strokeWidth = DensityUtil.dip2px(context, sw); // 3dp 边框宽度
int roundRadius = dis; // 8dp 圆角半径
int fillColor = null != solidcolor ? Color.parseColor(solidcolor) : Color.parseColor("#00000000");// 内部填充颜色
int strokeColor = null != strokecolor ? Color.parseColor(strokecolor) : Color.parseColor("#00000000");// 边框颜色
GradientDrawable gd = new GradientDrawable();// 创建drawable
gd.setColor(fillColor);
gd.setCornerRadius(roundRadius);
gd.setStroke(strokeWidth, strokeColor);
return gd;
}
public static Drawable shapeDrawable(Context context, int tlradiussize,int trradiussize,int blradiussize,int brradiussize,
double strokewidth, String solidcolor, String strokecolor) {
float tldis = DensityUtil.dip2px(context, tlradiussize);
float trdis = DensityUtil.dip2px(context, trradiussize);
float bldis = DensityUtil.dip2px(context, blradiussize);
float brdis = DensityUtil.dip2px(context, brradiussize);
float sw = (float) strokewidth;
int strokeWidth = DensityUtil.dip2px(context, sw); // 3dp 边框宽度
int fillColor = null != solidcolor ? Color.parseColor(solidcolor) : Color.parseColor("#00000000");// 内部填充颜色
int strokeColor = null != strokecolor ? Color.parseColor(strokecolor) : Color.parseColor("#00000000");// 边框颜色
GradientDrawable gd = new GradientDrawable();// 创建drawable
gd.setColor(fillColor);
gd.setCornerRadii(new float[]{tldis,tldis,trdis,trdis,bldis,bldis,brdis,brdis});
gd.setStroke(strokeWidth, strokeColor);
return gd;
}
public static Drawable shapeDrawable(Context context, int radiussize,
double strokewidth, String solidcolor, String strokecolor,int dashGap,int dashWidth) {
float sw = (float) strokewidth;
int strokeWidth = DensityUtil.dip2px(context, sw); // 3dp 边框宽度
int fillColor = null != solidcolor ? Color.parseColor(solidcolor) : Color.parseColor("#00000000");// 内部填充颜色
int strokeColor = null != strokecolor ? Color.parseColor(strokecolor) : Color.parseColor("#00000000");// 边框颜色
GradientDrawable gd = new GradientDrawable();// 创建drawable
gd.setColor(fillColor);
gd.setCornerRadius(DensityUtil.dip2px(context, radiussize));
gd.setStroke(strokeWidth, strokeColor);
gd.setStroke(strokeWidth, strokeColor, DensityUtil.dip2px(context, dashWidth), DensityUtil.dip2px(context, dashGap));
return gd;
}
public static Drawable shapeDrawable(Context context, int tlradiussize,int trradiussize,int blradiussize,int brradiussize,
double strokewidth, String solidcolor, String strokecolor,int dashGap,int dashWidth) {
float tldis = DensityUtil.dip2px(context, tlradiussize);
float trdis = DensityUtil.dip2px(context, trradiussize);
float bldis = DensityUtil.dip2px(context, blradiussize);
float brdis = DensityUtil.dip2px(context, brradiussize);
float sw = (float) strokewidth;
int strokeWidth = DensityUtil.dip2px(context, sw); // 3dp 边框宽度
int fillColor = null != solidcolor ? Color.parseColor(solidcolor) : Color.parseColor("#00000000");// 内部填充颜色
int strokeColor = null != strokecolor ? Color.parseColor(strokecolor) : Color.parseColor("#00000000");// 边框颜色
GradientDrawable gd = new GradientDrawable();// 创建drawable
gd.setColor(fillColor);
gd.setCornerRadii(new float[]{tldis,tldis,trdis,trdis,bldis,bldis,brdis,brdis});
gd.setStroke(strokeWidth, strokeColor);
gd.setStroke(strokeWidth, strokeColor, DensityUtil.dip2px(context, dashWidth), DensityUtil.dip2px(context, dashGap));
return gd;
}