Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement eraser using PorterDuff Xfer mode #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android {

defaultConfig {
applicationId "com.rm.freedrawsample"
minSdkVersion 9
minSdkVersion 11
targetSdkVersion 25
versionCode 1
versionName "1.0.0"
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/com/rm/freedrawsample/ActivityDraw.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ActivityDraw extends AppCompatActivity

private FreeDrawView mFreeDrawView;
private View mSideView;
private Button mBtnRandomColor, mBtnUndo, mBtnRedo, mBtnClearAll;
private Button mBtnRandomColor, mBtnUndo, mBtnRedo, mBtnClearAll, mBtnEraser;
private SeekBar mThicknessBar, mAlphaBar;
private TextView mTxtRedoCount, mTxtUndoCount;

Expand All @@ -56,13 +56,15 @@ protected void onCreate(Bundle savedInstanceState) {
mBtnUndo = (Button) findViewById(R.id.btn_undo);
mBtnRedo = (Button) findViewById(R.id.btn_redo);
mBtnClearAll = (Button) findViewById(R.id.btn_clear_all);
mBtnEraser = (Button) findViewById(R.id.btn_eraser);
mThicknessBar = (SeekBar) findViewById(R.id.slider_thickness);
mAlphaBar = (SeekBar) findViewById(R.id.slider_alpha);

mBtnRandomColor.setOnClickListener(this);
mBtnUndo.setOnClickListener(this);
mBtnRedo.setOnClickListener(this);
mBtnClearAll.setOnClickListener(this);
mBtnEraser.setOnClickListener(this);

mAlphaBar.setMax((ALPHA_MAX - ALPHA_MIN) / ALPHA_STEP);
mAlphaBar.setProgress(mFreeDrawView.getPaintAlpha());
Expand Down Expand Up @@ -141,6 +143,17 @@ public void onClick(View v) {
if (id == mBtnClearAll.getId()) {
mFreeDrawView.undoAll();
}

if (id == mBtnEraser.getId()) {
// toggle pen/eraser
if (mFreeDrawView.isEraser()) {
mFreeDrawView.setEraser(false);
mBtnEraser.setText("Eraser");
} else {
mFreeDrawView.setEraser(true);
mBtnEraser.setText("Pen");
}
}
}

// SliderListener
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/layout-land/activity_draw.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@
android:layout_weight="1"
android:orientation="horizontal">


<Button
android:id="@+id/btn_eraser"
style="@style/Base.Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Erase"
android:textColor="@android:color/white" />

<Button
android:id="@+id/btn_color"
style="@style/Base.Widget.AppCompat.Button.Borderless"
Expand Down
45 changes: 27 additions & 18 deletions app/src/main/res/layout/activity_draw.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white"
tools:context="com.rm.freedrawsample.ActivityDraw">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical"
tools:context="com.rm.freedrawsample.ActivityDraw">

<ImageView
android:id="@+id/img_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:visibility="gone" />
android:visibility="gone"/>

<com.rm.freedrawview.FreeDrawView
android:id="@+id/free_draw_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white" />
android:background="@color/white"/>

<RelativeLayout
android:id="@+id/side_view"
Expand Down Expand Up @@ -49,12 +49,12 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stroke width"
android:textColor="@android:color/white" />
android:textColor="@android:color/white"/>

<SeekBar
android:id="@+id/slider_thickness"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"/>
</LinearLayout>

<LinearLayout
Expand All @@ -68,12 +68,12 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stroke alpha"
android:textColor="@android:color/white" />
android:textColor="@android:color/white"/>

<SeekBar
android:id="@+id/slider_alpha"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>

Expand All @@ -94,7 +94,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Undo"
android:textColor="@android:color/white" />
android:textColor="@android:color/white"/>

<TextView
android:id="@+id/txt_undo_count"
Expand All @@ -104,7 +104,7 @@
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:padding="12dp"
android:textColor="@color/white" />
android:textColor="@color/white"/>
</RelativeLayout>

<RelativeLayout
Expand All @@ -118,7 +118,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="redo"
android:textColor="@android:color/white" />
android:textColor="@android:color/white"/>

<TextView
android:id="@+id/txt_redo_count"
Expand All @@ -128,7 +128,7 @@
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:padding="12dp"
android:textColor="@color/white" />
android:textColor="@color/white"/>
</RelativeLayout>
</LinearLayout>

Expand All @@ -138,14 +138,23 @@
android:layout_weight="1"
android:orientation="horizontal">

<Button
android:id="@+id/btn_eraser"
style="@style/Base.Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Eraser"
android:textColor="@android:color/white"/>

<Button
android:id="@+id/btn_clear_all"
style="@style/Base.Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="clear all"
android:textColor="@android:color/white" />
android:textColor="@android:color/white"/>

<Button
android:id="@+id/btn_color"
Expand All @@ -154,7 +163,7 @@
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Random color"
android:textColor="@android:color/white" />
android:textColor="@android:color/white"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Expand Down
2 changes: 1 addition & 1 deletion freedrawview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ android {
buildToolsVersion "25.0.2"

defaultConfig {
minSdkVersion 9
minSdkVersion 11
targetSdkVersion 25
versionCode 1
versionName "1.0.2"
Expand Down
26 changes: 26 additions & 0 deletions freedrawview/src/main/java/com/rm/freedrawview/FreeDrawView.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import android.graphics.ComposePathEffect;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.AsyncTask;
import android.os.Parcelable;
import android.support.annotation.ColorInt;
Expand Down Expand Up @@ -50,6 +52,8 @@ public class FreeDrawView extends View implements View.OnTouchListener {

private boolean mFinishPath = false;

private boolean mEraser = false;

// Needed to draw points
private Paint mFillPaint;

Expand All @@ -69,6 +73,11 @@ public FreeDrawView(Context context, AttributeSet attrs, int defStyleAttr) {

setOnTouchListener(this);

// Required for implementing ERASE feature
// See http://stackoverflow.com/a/33483016/990066
// setLayerType only available in SDK 11 and above
setLayerType(LAYER_TYPE_HARDWARE, mCurrentPaint);

TypedArray a = null;
try {

Expand All @@ -83,6 +92,7 @@ public FreeDrawView(Context context, AttributeSet attrs, int defStyleAttr) {
a.recycle();
}
}

}

@Override
Expand Down Expand Up @@ -425,6 +435,7 @@ private void setupFillPaint(Paint from) {

@Override
protected void onDraw(Canvas canvas) {

if (mPaths.size() == 0 && mPoints.size() == 0) {
return;
}
Expand Down Expand Up @@ -483,6 +494,7 @@ protected void onDraw(Canvas canvas) {
if (finishedPath && mPoints.size() > 0) {
createHistoryPathFromPoints();
}

}

// Create a path from the current points
Expand Down Expand Up @@ -676,4 +688,18 @@ protected void onPostExecute(Void aVoid) {
}
}
}

public boolean isEraser() {
return mEraser;
}

public void setEraser(boolean mEraser) {
this.mEraser = mEraser;
if (mEraser) {
mCurrentPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
} else {
mCurrentPaint.setXfermode(null);
}
}

}