Android常用的图片的转化方法
bitmap转 drawable
public static Drawable bitmap2Drawable(Context context,Bitmap bitmap){
DisplayMetrics metrics = context.getApplicationContext().getResources().getDisplayMetrics();
Resources resources = new Resources(context.getApplicationContext().getAssets(), metrics, null);
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;
}
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);
int roundRadius = dis;
int fillColor = null != solidcolor ? Color.parseColor(solidcolor) : Color.parseColor("#00000000");
int strokeColor = null != strokecolor ? Color.parseColor(strokecolor) : Color.parseColor("#00000000");
GradientDrawable gd = new GradientDrawable();
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);
int fillColor = null != solidcolor ? Color.parseColor(solidcolor) : Color.parseColor("#00000000");
int strokeColor = null != strokecolor ? Color.parseColor(strokecolor) : Color.parseColor("#00000000");
GradientDrawable gd = new GradientDrawable();
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);
int fillColor = null != solidcolor ? Color.parseColor(solidcolor) : Color.parseColor("#00000000");
int strokeColor = null != strokecolor ? Color.parseColor(strokecolor) : Color.parseColor("#00000000");
GradientDrawable gd = new GradientDrawable();
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);
int fillColor = null != solidcolor ? Color.parseColor(solidcolor) : Color.parseColor("#00000000");
int strokeColor = null != strokecolor ? Color.parseColor(strokecolor) : Color.parseColor("#00000000");
GradientDrawable gd = new GradientDrawable();
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;
}