Skip to content

Commit

Permalink
PE-379 Add a PhotoEditor.Builder parameter to clip or not the drawing…
Browse files Browse the repository at this point in the history
… brush to the source image.
  • Loading branch information
stephanepechard authored and burhanrashid52 committed Jul 31, 2021
1 parent bf9727d commit f2d2e2c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 45 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* <p>
* This ViewGroup will have the {@link DrawingView} to draw paint on it with {@link ImageView}
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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;
}


Expand Down Expand Up @@ -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
}

0 comments on commit f2d2e2c

Please sign in to comment.