android 图片转换

Android常用的图片的转化方法

bitmap转 drawable

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

    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;

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值