Skip to content

Instantly share code, notes, and snippets.

@Sirelon
Created April 3, 2017 10:59
Show Gist options
  • Save Sirelon/515a66c535df5f0c8bd91fff12e209eb to your computer and use it in GitHub Desktop.
Save Sirelon/515a66c535df5f0c8bd91fff12e209eb to your computer and use it in GitHub Desktop.
The UCrop View with some trick option for reuse CropImagreView. Related issue https://github.com/Yalantis/uCrop/issues/271#issuecomment-291105984
package com.sirelon;
import android.content.Context;
import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import com.yalantis.ucrop.callback.CropBoundsChangeListener;
import com.yalantis.ucrop.callback.OverlayViewChangeListener;
import com.yalantis.ucrop.view.GestureCropImageView;
import com.yalantis.ucrop.view.UCropView;
/**
* Created on 20/03/2017 16:57.
*/
public class ReusableUCropView extends UCropView {
private GestureCropImageView reusableCropImageView;
public ReusableUCropView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ReusableUCropView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void resetCropImageView() {
removeView(getCropImageView());
reusableCropImageView = new GestureCropImageView(getContext());
reusableCropImageView.setCropBoundsChangeListener(new CropBoundsChangeListener() {
@Override
public void onCropAspectRatioChanged(float cropRatio) {
getOverlayView().setTargetAspectRatio(cropRatio);
}
});
getOverlayView().setOverlayViewChangeListener(new OverlayViewChangeListener() {
@Override
public void onCropRectUpdated(RectF cropRect) {
getCropImageView().setCropRect(cropRect);
}
});
reusableCropImageView.setCropRect(getOverlayView().getCropViewRect());
addView(reusableCropImageView, 0);
}
@NonNull
@Override
public GestureCropImageView getCropImageView() {
return reusableCropImageView != null ? reusableCropImageView : super.getCropImageView();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment