Skip to content
Himanshu Dudhat edited this page Nov 19, 2019 · 4 revisions

//Load image using glide

CircleView.java

public class CircleView extends AppCompatImageView implements
    ProteusView {
    Manager viewManager;

    public CircleView(Context context) {
        super(context);
        init();
    }

    public CircleView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    public Manager getViewManager() {
        return viewManager;
    }

    @Override
    public void setViewManager(@NonNull Manager viewManager) {
        this.viewManager = viewManager;
    }

    private void init() {

        setOnMeasureCallback();
    }

    private void setOnMeasureCallback() {
        ViewTreeObserver vto = getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                removeOnGlobalLayoutListener(this);
            }
        });
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    void removeOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener listener) {
        if (Build.VERSION.SDK_INT < 16) {
            //noinspection deprecation
            getViewTreeObserver().removeGlobalOnLayoutListener(listener);
        } else {
            getViewTreeObserver().removeOnGlobalLayoutListener(listener);
        }
    }

    @NonNull
    @Override
    public View getAsView() {
        return this;
    }
}

CircleViewParser.java

public class CircleViewParser extends ViewTypeParser<CircleView> {

	private String masking = "";
	private Drawable custommask;
	private static Paint paint = new Paint();

	static {
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
	}

	@NonNull
	@Override
	public String getType() {
		return "CircleView";
	}

	@Nullable
	@Override
	public String getParentType() {
		return "View";
	}

	@NonNull
	@Override
	public ProteusView createView(@NonNull ProteusContext context, @NonNull Layout layout, @NonNull ObjectValue data, @Nullable ViewGroup parent, int dataIndex) {
		return new CircleView(context);
	}

	@Override
	protected void addAttributeProcessors() {

		addAttributeProcessor("background", new DrawableResourceProcessor<CircleView>() {
			@Override
			public void setDrawable(CircleView view, Drawable drawable) {
				custommask=drawable;
			  view.setBackground(drawable);
			}
		});

		addAttributeProcessor("mask", new StringAttributeProcessor<CircleView>() {
			@Override
			public void setString(CircleView view, String value) {
				masking = value;
			}
		});


		addAttributeProcessor("url", new StringAttributeProcessor<CircleView>() {
			@Override
			public void setString(CircleView view, String value) {
				GlideApp.with(view)
						.load(value)
						.apply(provideMask())
						.placeholder(R.drawable.placeholder)
						.error(R.drawable.image_broken)
						.into(view);
			}
		});
	}

	private RequestOptions provideMask() {
		switch (masking) {
			case "round":
				return RequestOptions.bitmapTransform(new CircleCrop());
			case "rounded_corner":
				return RequestOptions.bitmapTransform(new RoundedCornersTransformation(10, 0));
			case "custom":
				return RequestOptions.bitmapTransform(new MaskTransformation(R.drawable.image_broken));
			case "custommask":
				if (custommask != null)
					return RequestOptions.bitmapTransform(new BlurTransformation(custommask));
				else
					return RequestOptions.bitmapTransform(new MaskTransformation(R.drawable.image_broken));

			default:
				return RequestOptions.bitmapTransform(new MaskTransformation(R.drawable.image_broken));
		}

	}


	 class BlurTransformation extends BitmapTransformation {

		 private static final int VERSION = 1;
		 private static final String ID =
				 "jp.wasabeef.glide.transformations.MaskTransformation." + VERSION;

		 private  Paint paint = new Paint();
		 Drawable mask;

		 public BlurTransformation(Drawable mask) {
			 this.mask=mask;
			 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		 }

		 private int maskId;

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

			 Bitmap bitmap = pool.get(width, height, Bitmap.Config.ARGB_8888);
			 bitmap.setHasAlpha(true);


			 Canvas canvas = new Canvas(bitmap);

			// mask.setBounds(0, 0, width, height);
			 mask.draw(canvas);
			 canvas.drawBitmap(toTransform, 0, 0, paint);

			 return bitmap;
		}

		@Override
		public void updateDiskCacheKey(MessageDigest messageDigest) {
			messageDigest.update("blur transformation".getBytes());
		}
	}
}