Glide框架 结合glide-transformations使用或者自己重写Transformation使用

1
//gidle
compile ‘com.github.bumptech.glide:glide:3.7.0’

//glide-transformation(3.0.1的有点大的改变 不过我还是喜欢用2.0.1的本版  有需要的同学  可以自己去看一下(https://github.com/wasabeef/glide-transformations)

compile ‘jp.wasabeef:glide-transformations:2.0.1’
2 贴了一下 我平常用的Glide工具类

public class GlideUtil {
//圆角图片
public static class GlideRoundTransform extends BitmapTransformation {

    private static float radius = 0f;

    public GlideRoundTransform(Context context) {
        this(context, 4);
    }

    public GlideRoundTransform(Context context, int dp) {
        super(context);
        this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
    }

    @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        return roundCrop(pool, toTransform);
    }

    private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
        if (source == null) return null;

        Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        if (result == null) {
            result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rectF, radius, radius, paint);
        return result;
    }

    @Override public String getId() {
        return getClass().getName() + Math.round(radius);
    }
}
public static void downBlur(Context context,String url,ImageView imageView){
    Glide.with(context)
            .load(url)
            .crossFade(1000)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .bitmapTransform(new BlurTransformation(context,23,4)) // “23”:设置模糊度(在0.0到25.0之间),默认”25";"4":图片缩放比例,默认“1”。
            .into(imageView);

}



//直接设置给控件
public static void downImg(final Context context, String url, final ImageView imageView) {
    Glide
            .with(context.getApplicationContext())
            .load(url)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .thumbnail(0.1f)
            .into(imageView)
        ;
}
//直接设置给控件,加载错误显示图片
public static void downImgForError(final Context context, String url, final ImageView imageView,int id) {
    Glide
            .with(context.getApplicationContext())
            .load(url)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .placeholder(R.drawable.img_loading)
            .error(R.drawable.img_error_loading)
          /*  .thumbnail(0.1f)*/
            .into(imageView)
        ;
}
//直接设置给控件,加载错误显示图片
public static void downImgForErrorRundle( Context context, String url, ImageView imageView,int id) {
    Glide
            .with(context.getApplicationContext())
            .load(url)

            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .placeholder(R.drawable.img_loading)
            .error(R.drawable.img_error_loading)

          /*  .thumbnail(0.1f)*/
           /* .crossFade(1000)*/
            .bitmapTransform(new RoundedCornersTransformation(context.getApplicationContext(),30,0,RoundedCornersTransformation.CornerType.ALL))
            .crossFade(1000)
            .into(imageView)
    ;
}
//直接设置给控件,加载错误显示图片
public static void downImgForError1(final Context context, String url, final ImageView imageView,int id) {
    Glide
            .with(context.getApplicationContext())
            .load(url)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .error(R.drawable.img_error_loading)
          /*  .thumbnail(0.1f)*/
            .into(imageView)
    ;
}
//直接设置给控件,加载错误显示图片
public static void downImgForError2(final Context context, String url, final ImageView imageView,int id) {
    Glide
            .with(context.getApplicationContext())
            .load(url)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .error(id)
          /*  .thumbnail(0.1f)*/
            .into(imageView)
    ;
}
//直接设置给控件,加载错误显示图片
public static void downImgPhototViewForError(final Context context, String url, final ImageView imageView,int idp,int id) {
    Glide
            .with(context.getApplicationContext())
            .load(url)
            .diskCacheStrategy(DiskCacheStrategy.RESULT)
            .placeholder(idp)
            .error(id)
         /*   .thumbnail(0.1f)*/
            .into(imageView)
    ;
}
//根据图片的比例计算设置图片高度
public static void downImgForH(final Context context, String url, final ImageView imageView) {
    Glide
            .with(context.getApplicationContext())
            .load(url)
            .asBitmap()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .thumbnail(0.1f)
            .into(new SimpleTarget() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                    ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
                   /* imageView.measure(0,0);
                    int width= layoutParams.width;*/

                    //通过比例计算图片应该有的高度
                    int bitmapW = resource.getWidth();
                    int bitmapH = resource.getHeight();
                    int h ;// (int) (((double) bitmapH / bitmapW) * ScreenUtil.getScreenWidth(context));
                    h=(int) (bitmapH*(((double) 330/bitmapW)));
                    L.d(bitmapW + "   " + bitmapH + "----->" + h+"width");
                    //设置imageview的高度
                    layoutParams.height = h;
                    imageView.setLayoutParams(layoutParams);
                    //设置imageview显示的图片
                    imageView.setImageBitmap(resource);
                }
            });



}

//根据图片的比例计算设置图片高度
public static void downImgForHwith(final Context context, String url, final ImageView imageView) {
    Glide
            .with(context.getApplicationContext())
            .load(url)
            .asBitmap()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .thumbnail(0.1f)
            .into(new SimpleTarget() {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                    ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
                   /* imageView.measure(0,0);
                    int width= layoutParams.width;*/

                    //通过比例计算图片应该有的高度
                    int bitmapW = resource.getWidth();
                    int bitmapH = resource.getHeight();
                    int h ;// (int) (((double) bitmapH / bitmapW) * ScreenUtil.getScreenWidth(context));
                   // h=(int) (bitmapH*(((double) 120/bitmapW)));

                   // int w= (int) (((double) 120 / bitmapH) * bitmapW);
                    int w;
                    w= (int) (bitmapW*(((double) 120 / bitmapH)));
                    //设置imageview的高度
                    layoutParams.width=w;
                    L.d(bitmapW + "   " + bitmapW + "----->" + w+"width");

                    imageView.setLayoutParams(layoutParams);

                    //设置imageview显示的图片
                    imageView.setImageBitmap(resource);
                }
            });

}

}

3 在使用的时候遇到的问题

a.在使用生成圆角的图片的时候 遇到ImageView 如果设置了scaleType 就会 影响产生的效果

你可能感兴趣的:(Android)