diff --git a/README.md b/README.md index 0752afd4..0e21b159 100644 --- a/README.md +++ b/README.md @@ -80,11 +80,12 @@ To use the image editing feature we need to build a PhotoEditor which requires a //Use custom font using latest support library Typeface mTextRobotoTf = ResourcesCompat.getFont(this, R.font.roboto_medium); -//loading font from assest +//loading font from asset Typeface mEmojiTypeFace = Typeface.createFromAsset(getAssets(), "emojione-android.ttf"); mPhotoEditor = new PhotoEditor.Builder(this, mPhotoEditorView) .setPinchTextScalable(true) + .setClipSourceImage(true) .setDefaultTextTypeface(mTextRobotoTf) .setDefaultEmojiTypeface(mEmojiTypeFace) .build(); @@ -93,7 +94,8 @@ We can customize the properties in the PhotoEditor as per our requirement | Property | Usage | | ------------- | ------------- | -| `setPinchTextScalable()` | set false to disable pinch to zoom on text insertion.By default its true +| `setPinchTextScalable()` | set false to disable pinch to zoom on text insertion. Default: true. | +| `setClipSourceImage()` | set true to clip the drawing brush to the source image. Default: false. | | `setDefaultTextTypeface()` | set default text font to be added on image | | `setDefaultEmojiTypeface()` | set default font specifc to add emojis | diff --git a/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditor.java b/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditor.java index 364fe2f8..fdf1951e 100644 --- a/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditor.java +++ b/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditor.java @@ -358,6 +358,16 @@ public Builder setPinchTextScalable(boolean isTextPinchScalable) { public PhotoEditor build() { return new PhotoEditorImpl(this); } + + /** + * Set true true to clip the drawing brush to the source image. + * + * @param clip a boolean to indicate if brush drawing is clipped or not. + */ + public Builder setClipSourceImage(boolean clip) { + parentView.setClipSourceImage(clip); + return this; + } } diff --git a/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditorView.java b/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditorView.java index da91390b..69b51c03 100644 --- a/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditorView.java +++ b/photoeditor/src/main/java/ja/burhanrashid52/photoeditor/PhotoEditorView.java @@ -6,16 +6,16 @@ import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.os.Build; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.annotation.RequiresApi; import android.util.AttributeSet; import android.util.Log; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; + /** *
* This ViewGroup will have the {@link DrawingView} to draw paint on it with {@link ImageView} @@ -34,6 +34,7 @@ public class PhotoEditorView extends RelativeLayout { private FilterImageView mImgSource; private DrawingView mDrawingView; private ImageFilterView mImageFilterView; + private boolean clipSourceImage; private static final int imgSrcId = 1, shapeSrcId = 2, glFilterId = 3; public PhotoEditorView(Context context) { @@ -57,16 +58,47 @@ public PhotoEditorView(Context context, AttributeSet attrs, int defStyleAttr, in init(attrs); } - @SuppressLint("Recycle") private void init(@Nullable AttributeSet attrs) { //Setup image attributes mImgSource = new FilterImageView(getContext()); + RelativeLayout.LayoutParams sourceParam = setupImageSource(attrs); + + mImgSource.setOnImageChangedListener(new FilterImageView.OnImageChangedListener() { + @Override + public void onBitmapLoaded(@Nullable Bitmap sourceBitmap) { + mImageFilterView.setFilterEffect(PhotoFilter.NONE); + mImageFilterView.setSourceBitmap(sourceBitmap); + Log.d(TAG, "onBitmapLoaded() called with: sourceBitmap = [" + sourceBitmap + "]"); + } + }); + + //Setup GLSurface attributes + mImageFilterView = new ImageFilterView(getContext()); + RelativeLayout.LayoutParams filterParam = setupFilterView(); + + //Setup drawing view + mDrawingView = new DrawingView(getContext()); + RelativeLayout.LayoutParams brushParam = setupDrawingView(); + + //Add image source + addView(mImgSource, sourceParam); + + //Add Gl FilterView + addView(mImageFilterView, filterParam); + + //Add brush view + addView(mDrawingView, brushParam); + } + + + @SuppressLint("Recycle") + private RelativeLayout.LayoutParams setupImageSource(@Nullable AttributeSet attrs) { mImgSource.setId(imgSrcId); mImgSource.setAdjustViewBounds(true); - mImgSource.setScaleType(ImageView.ScaleType.CENTER_INSIDE); - RelativeLayout.LayoutParams imgSrcParam = new RelativeLayout.LayoutParams( - ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); - imgSrcParam.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); + //if (clipSourceImage) { + mImgSource.setScaleType(ImageView.ScaleType.CENTER_INSIDE); + //} + if (attrs != null) { TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.PhotoEditorView); Drawable imgSrcDrawable = a.getDrawable(R.styleable.PhotoEditorView_photo_src); @@ -75,50 +107,48 @@ private void init(@Nullable AttributeSet attrs) { } } - //Setup drawing view - mDrawingView = new DrawingView(getContext()); + int widthParam = ViewGroup.LayoutParams.MATCH_PARENT; + if (clipSourceImage) { + widthParam = ViewGroup.LayoutParams.WRAP_CONTENT; + } + RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( + widthParam, ViewGroup.LayoutParams.WRAP_CONTENT); + params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); + + return params; + } + + + private RelativeLayout.LayoutParams setupDrawingView() { mDrawingView.setVisibility(GONE); mDrawingView.setId(shapeSrcId); - //Align brush to the size of image view - RelativeLayout.LayoutParams brushParam = new RelativeLayout.LayoutParams( + // Align drawing view to the size of image view + RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); - brushParam.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); - brushParam.addRule(RelativeLayout.ALIGN_TOP, imgSrcId); - brushParam.addRule(RelativeLayout.ALIGN_BOTTOM, imgSrcId); - brushParam.addRule(RelativeLayout.ALIGN_LEFT, imgSrcId); - brushParam.addRule(RelativeLayout.ALIGN_RIGHT, imgSrcId); + params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); + params.addRule(RelativeLayout.ALIGN_TOP, imgSrcId); + params.addRule(RelativeLayout.ALIGN_BOTTOM, imgSrcId); + //if (clipSourceImage) { + params.addRule(RelativeLayout.ALIGN_LEFT, imgSrcId); + params.addRule(RelativeLayout.ALIGN_RIGHT, imgSrcId); + //} + return params; + } - //Setup GLSurface attributes - mImageFilterView = new ImageFilterView(getContext()); - mImageFilterView.setId(glFilterId); + + private RelativeLayout.LayoutParams setupFilterView() { mImageFilterView.setVisibility(GONE); + mImageFilterView.setId(glFilterId); //Align brush to the size of image view - RelativeLayout.LayoutParams imgFilterParam = new RelativeLayout.LayoutParams( + RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); - imgFilterParam.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); - imgFilterParam.addRule(RelativeLayout.ALIGN_TOP, imgSrcId); - imgFilterParam.addRule(RelativeLayout.ALIGN_BOTTOM, imgSrcId); - - mImgSource.setOnImageChangedListener(new FilterImageView.OnImageChangedListener() { - @Override - public void onBitmapLoaded(@Nullable Bitmap sourceBitmap) { - mImageFilterView.setFilterEffect(PhotoFilter.NONE); - mImageFilterView.setSourceBitmap(sourceBitmap); - Log.d(TAG, "onBitmapLoaded() called with: sourceBitmap = [" + sourceBitmap + "]"); - } - }); - + params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); + params.addRule(RelativeLayout.ALIGN_TOP, imgSrcId); + params.addRule(RelativeLayout.ALIGN_BOTTOM, imgSrcId); - //Add image source - addView(mImgSource, imgSrcParam); - - //Add Gl FilterView - addView(mImageFilterView, imgFilterParam); - - //Add brush view - addView(mDrawingView, brushParam); + return params; } @@ -169,5 +199,11 @@ void setFilterEffect(CustomEffect customEffect) { mImageFilterView.setFilterEffect(customEffect); } + void setClipSourceImage(boolean clip) { + clipSourceImage = clip; + RelativeLayout.LayoutParams param = setupImageSource(null); + mImgSource.setLayoutParams(param); + } + // endregion }