Framework Glide realizes image loading

I learned several frameworks for image loading, and wrote down what I encountered here

First Glide loads the image

01

If there are no special requirements, the image processing code is as follows:

 

Glide.with(this).load(imgurl)
					.placeholder(R.drawable.logo_s)//Default image
					.error(R.drawable.logo_s)//The default image where an error occurs
					.into(coupon_ad_iv);

 02

 

To load a circle chart, you need to customize a circle class to encapsulate

 

Glide.with(ShequActivity.this).load(faceurl).placeholder(R.drawable.xyzd).bitmapTransform(new GlideCircleTransform(this)).into(userbut);

 03

 

To load a rounded corner image, you also need to encapsulate a rounded corner class

 

DiskCacheStrategy.ALL here means that all four corners are rounded

 

Glide.with(mContext)
					.load(t.getCover().toString())
					.placeholder(R.drawable.logo_s)
					.error(R.drawable.logo_s)
					.transform(new CenterCrop(mContext),
							new GlideRoundTransform(mContext))
					.diskCacheStrategy(DiskCacheStrategy.ALL).crossFade()
					.into(coupon_ad_iv);

 

Glide.with(mContext)
						.load(mHotList.get(position).getPic().toString())
						.placeholder(R.drawable.logo_s).error(R.drawable.logo_s)
						.bitmapTransform(
								new CenterCrop(mContext),
								new RoundedCornersTransformation(
										mContext, 20, 0,CornerType.TOP))
						.crossFade(1000).into(holder.coupon_ad_iv);

CornerType.TOP here means that the top two of the four corners are rounded corners

 

Here is the custom encapsulated class:

public class GlideCircleTransform extends BitmapTransformation {

	  public GlideCircleTransform(Context context) {
	        super(context);
	    }

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

	    private static Bitmap circleCrop(BitmapPool pool, Bitmap source) {
	        if (source == null) return null;
	        int size = Math.min(source.getWidth(), source.getHeight());
	        int x = (source.getWidth() - size) / 2;
	        int y = (source.getHeight() - size) / 2;
	        Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
	        Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
	        if (result == null) {
	            result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
	        }
	        Canvas canvas = new Canvas(result);
	        Paint paint = new Paint();
	        paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
	        paint.setAntiAlias(true);
	        float r = size / 2f;
	        canvas.drawCircle(r, r, r, paint);
	        return result;
	    }

	    @Override
	    public String getId() {
	        return getClass().getName();
	    }

}

 

* Copyright (C) 2017 Wasabeef

import android.content.Context;

public class RoundedCornersTransformation implements Transformation<Bitmap> {

  public enum CornerType {
    ALL,
    TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT,
    TOP, BOTTOM, LEFT, RIGHT,
    OTHER_TOP_LEFT, OTHER_TOP_RIGHT, OTHER_BOTTOM_LEFT, OTHER_BOTTOM_RIGHT,
    DIAGONAL_FROM_TOP_LEFT, DIAGONAL_FROM_TOP_RIGHT
  }

  private BitmapPool mBitmapPool;
  private int mRadius;
  private int mDiameter;
  private int mMargin;
  private CornerType mCornerType;

  public RoundedCornersTransformation(Context context, int radius, int margin) {
    this(context, radius, margin, CornerType.ALL);
  }

  public RoundedCornersTransformation(BitmapPool pool, int radius, int margin) {
    this(pool, radius, margin, CornerType.ALL);
  }

  public RoundedCornersTransformation(Context context, int radius, int margin,
      CornerType cornerType) {
    this(Glide.get(context).getBitmapPool(), radius, margin, cornerType);
  }

  public RoundedCornersTransformation(BitmapPool pool, int radius, int margin,
      CornerType cornerType) {
    mBitmapPool = pool;
    mRadius = radius;
    mDiameter = mRadius * 2;
    mMargin = margin;
    mCornerType = cornerType;
  }

  @Override
  public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
    Bitmap source = resource.get();

    int width = source.getWidth();
    int height = source.getHeight();

    Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
    if (bitmap == null) {
      bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
    drawRoundRect(canvas, paint, width, height);
    return BitmapResource.obtain(bitmap, mBitmapPool);
  }

  private void drawRoundRect(Canvas canvas, Paint paint, float width, float height) {
    float right = width - mMargin;
    float bottom = height - mMargin;

    switch (mCornerType) {
      case ALL:
        canvas.drawRoundRect(new RectF(mMargin, mMargin, right, bottom), mRadius, mRadius, paint);
        break;
      case TOP_LEFT:
        drawTopLeftRoundRect(canvas, paint, right, bottom);
        break;
      case TOP_RIGHT:
        drawTopRightRoundRect(canvas, paint, right, bottom);
        break;
      case BOTTOM_LEFT:
        drawBottomLeftRoundRect(canvas, paint, right, bottom);
        break;
      case BOTTOM_RIGHT:
        drawBottomRightRoundRect(canvas, paint, right, bottom);
        break;
      case TOP:
        drawTopRoundRect(canvas, paint, right, bottom);
        break;
      case BOTTOM:
        drawBottomRoundRect(canvas, paint, right, bottom);
        break;
      case LEFT:
        drawLeftRoundRect(canvas, paint, right, bottom);
        break;
      case RIGHT:
        drawRightRoundRect(canvas, paint, right, bottom);
        break;
      case OTHER_TOP_LEFT:
        drawOtherTopLeftRoundRect(canvas, paint, right, bottom);
        break;
      case OTHER_TOP_RIGHT:
        drawOtherTopRightRoundRect(canvas, paint, right, bottom);
        break;
      case OTHER_BOTTOM_LEFT:
        drawOtherBottomLeftRoundRect(canvas, paint, right, bottom);
        break;
      case OTHER_BOTTOM_RIGHT:
        drawOtherBottomRightRoundRect(canvas, paint, right, bottom);
        break;
      case DIAGONAL_FROM_TOP_LEFT:
        drawDiagonalFromTopLeftRoundRect(canvas, paint, right, bottom);
        break;
      case DIAGONAL_FROM_TOP_RIGHT:
        drawDiagonalFromTopRightRoundRect(canvas, paint, right, bottom);
        break;
      default:
        canvas.drawRoundRect(new RectF(mMargin, mMargin, right, bottom), mRadius, mRadius, paint);
        break;
    }
  }

  private void drawTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, mMargin + mDiameter),
        mRadius, mRadius, paint);
    canvas.drawRect(new RectF(mMargin, mMargin + mRadius, mMargin + mRadius, bottom), paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), paint);
  }

  private void drawTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, mMargin + mDiameter), mRadius,
        mRadius, paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), paint);
    canvas.drawRect(new RectF(right - mRadius, mMargin + mRadius, right, bottom), paint);
  }

  private void drawBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, mMargin + mDiameter, bottom),
        mRadius, mRadius, paint);
    canvas.drawRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom - mRadius), paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), paint);
  }

  private void drawBottomRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(right - mDiameter, bottom - mDiameter, right, bottom), mRadius,
        mRadius, paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), paint);
    canvas.drawRect(new RectF(right - mRadius, mMargin, right, bottom - mRadius), paint);
  }

  private void drawTopRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right, bottom), paint);
  }

  private void drawBottomRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right, bottom - mRadius), paint);
  }

  private void drawLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom), paint);
  }

  private void drawRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom), paint);
  }

  private void drawOtherTopLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius,
        paint);
    canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom - mRadius), paint);
  }

  private void drawOtherTopRightRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius,
        paint);
    canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, right, bottom), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin, right, bottom - mRadius), paint);
  }

  private void drawOtherBottomLeftRoundRect(Canvas canvas, Paint paint, float right, float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius,
        paint);
    canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, bottom), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right - mRadius, bottom), paint);
  }

  private void drawOtherBottomRightRoundRect(Canvas canvas, Paint paint, float right,
      float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, right, mMargin + mDiameter), mRadius, mRadius,
        paint);
    canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, bottom), mRadius, mRadius,
        paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin + mRadius, right, bottom), paint);
  }

  private void drawDiagonalFromTopLeftRoundRect(Canvas canvas, Paint paint, float right,
      float bottom) {
    canvas.drawRoundRect(new RectF(mMargin, mMargin, mMargin + mDiameter, mMargin + mDiameter),
        mRadius, mRadius, paint);
    canvas.drawRoundRect(new RectF(right - mDiameter, bottom - mDiameter, right, bottom), mRadius,
        mRadius, paint);
    canvas.drawRect(new RectF(mMargin, mMargin + mRadius, right - mDiameter, bottom), paint);
    canvas.drawRect(new RectF(mMargin + mDiameter, mMargin, right, bottom - mRadius), paint);
  }

  private void drawDiagonalFromTopRightRoundRect(Canvas canvas, Paint paint, float right,
      float bottom) {
    canvas.drawRoundRect(new RectF(right - mDiameter, mMargin, right, mMargin + mDiameter), mRadius,
        mRadius, paint);
    canvas.drawRoundRect(new RectF(mMargin, bottom - mDiameter, mMargin + mDiameter, bottom),
        mRadius, mRadius, paint);
    canvas.drawRect(new RectF(mMargin, mMargin, right - mRadius, bottom - mRadius), paint);
    canvas.drawRect(new RectF(mMargin + mRadius, mMargin + mRadius, right, bottom), paint);
  }

  @Override public String getId() {
    return "RoundedTransformation(radius=" + mRadius + ", margin=" + mMargin + ", diameter="
        + mDiameter + ", cornerType=" + mCornerType.name() + ")";
  }
}

 

 in addition

Although the above methods can achieve the effect of processing pictures, the loading pictures will be deformed, and the necessary processing must be added.

Add class:

public class MyBitmapImageViewTarget extends BitmapImageViewTarget {

	 public MyBitmapImageViewTarget(ImageView view) {
	      super(view);
	   }

	   @Override
	   public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
	      if (bitmap != null && view.getScaleType() != ScaleType.FIT_XY) {
	         view.setScaleType(ScaleType.FIT_XY);
	      }
	      super.onResourceReady(bitmap, anim);
	   }

	   @Override
	   protected void setResource(Bitmap resource) {
	      super.setResource(resource);
	   }

	   @Override
	   public void onLoadFailed(Exception e, Drawable errorDrawable) {
	      if (errorDrawable != null && view != null && view.getScaleType() != ScaleType.CENTER_CROP) {
	         view.setScaleType(ScaleType.CENTER_CROP);
	      }
	      super.onLoadFailed(e, errorDrawable);
	   }

	   @Override
	   public void onLoadStarted(Drawable placeholder) {
	      if (placeholder != null && placeholder != null && view != null && view.getScaleType() != ScaleType.CENTER_CROP) {
	         view.setScaleType(ScaleType.CENTER_CROP);
	      }
	      super.onLoadStarted(placeholder);
	   }

	   @Override
	   public void onLoadCleared(Drawable placeholder) {
	      if (placeholder != null && placeholder != null && view != null && view.getScaleType() != ScaleType.CENTER_CROP) {
	         view.setScaleType(ScaleType.CENTER_CROP);
	      }
	      super.onLoadCleared(placeholder);
	   }

}

 

Then put it in Glide:

For example: This method is to achieve normal loading of pictures and circular pictures, and then not deformed

public static void glideDisplayImg(Context mContext, String imgurl,
			ImageView img, int drawable, boolean isCircle) {
		if (isCircle) {
			Glide.with(mContext).load(imgurl).asBitmap().placeholder(drawable)
					.error(drawable).centerCrop()
					.transform(new GlideCircleTransform(mContext))
					.into(new MyBitmapImageViewTarget(img));
		} else {
			Glide.with(mContext).load(imgurl).asBitmap().placeholder(drawable).centerCrop()
					.error(drawable).into(new MyBitmapImageViewTarget(img));
		}
	}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326268053&siteId=291194637