当前位置:网站首页>Glide Mastery

Glide Mastery

2022-07-05 10:16:00 asahi_ xin

Exception logging

  RequestListener<String, GlideDrawable> requestListener = new RequestListener<String, GlideDrawable>() {
    
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
    
                Log.v("==========",model);
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
    
                return false;
            }
        };

        Glide.with(this)
                .load("'=====")
                .listener(requestListener)
                .error(R.drawable.b)
                .into(imageView);

requestListener The parameter type of should be the same as load Consistency of methods .


Picture transformation

When loading pictures , We can also process pictures , Such as rounding , Circularity , Black and white , Fuzziness, etc .

  • transform(bitmapTransform) This method can process pictures .

Take rounding as an example

public class CircleCrop extends BitmapTransformation {
    

    public CircleCrop(Context context) {
    
        super(context);
    }
    
    @Override
    public String getId() {
    
        return "test.nxx.myapplication";
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    
        int diameter = Math.min(toTransform.getWidth(), toTransform.getHeight());

        final Bitmap toReuse = pool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888);
        final Bitmap result;
        if (toReuse != null) {
    
            result = toReuse;
        } else {
    
            result = Bitmap.createBitmap(diameter, diameter, Bitmap.Config.ARGB_8888);
        }

        int dx = (toTransform.getWidth() - diameter) / 2;
        int dy = (toTransform.getHeight() - diameter) / 2;
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        BitmapShader shader = new BitmapShader(toTransform, BitmapShader.TileMode.CLAMP,
                BitmapShader.TileMode.CLAMP);
        if (dx != 0 || dy != 0) {
    
            Matrix matrix = new Matrix();
            matrix.setTranslate(-dx, -dy);
            shader.setLocalMatrix(matrix);
        }
        paint.setShader(shader);
        paint.setAntiAlias(true);
        float radius = diameter / 2f;
        canvas.drawCircle(radius, radius, radius, paint);

        if (toReuse != null && !pool.put(toReuse)) {
    
            toReuse.recycle();
        }
        return result;
    }
}

getId() Method is required to return a unique string as id, To distinguish from other image transformations . Usually , We can simply return the full class name of the current class .

 Glide.with(context).load(url)
                        .skipMemoryCache(true)
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .dontTransform()
                        .transform(new CircleCrop(context))
                        .into(imageView);

Image processing is more general , Here we can use third-party libraries to achieve more effects .

Project address

quote

implementation 'jp.wasabeef:glide-transformations:2.0.2'

Fuzzify

  Glide.with(context).load(url)
       .bitmapTransform(new BlurTransformation(context))
       .into(imageView);

Black and white

  Glide.with(context).load(url)
       .bitmapTransform(new GrayscaleTransformation(context))
       .into(imageView);

It can also be done at the same time

 Glide.with(context).load(url)
      .bitmapTransform(new BlurTransformation(context), new GrayscaleTransformation(context))
      .into(imageView);
原网站

版权声明
本文为[asahi_ xin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140534181869.html