android 图片转换

本文将介绍几种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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值