implement Undo functionality

This commit is contained in:
tibbi 2016-02-15 22:27:40 +01:00
parent 31ee707919
commit 92b45d87b0
5 changed files with 34 additions and 3 deletions

View File

@ -23,4 +23,5 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.jakewharton:butterknife:7.0.1'
} }

View File

@ -1,13 +1,24 @@
package draw.simplemobiletools.com; package draw.simplemobiletools.com;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
@Bind(R.id.my_canvas) MyCanvas myCanvas;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.undo)
public void undo() {
myCanvas.undo();
} }
} }

View File

@ -36,12 +36,22 @@ public class MyCanvas extends View {
paths.add(path); paths.add(path);
} }
public void undo() {
if (paths.size() <= 0)
return;
paths.remove(paths.size() - 1);
invalidate();
}
@Override @Override
protected void onDraw(Canvas canvas) { protected void onDraw(Canvas canvas) {
super.onDraw(canvas); super.onDraw(canvas);
for (Path p : paths) { for (Path p : paths) {
canvas.drawPath(p, paint); canvas.drawPath(p, paint);
} }
canvas.drawPath(path, paint);
} }
private void actionDown(float x, float y) { private void actionDown(float x, float y) {
@ -60,15 +70,15 @@ public class MyCanvas extends View {
private void actionUp() { private void actionUp() {
path.lineTo(curX, curY); path.lineTo(curX, curY);
// drawing dots // drawing dots on click
if (startX == curX && startY == curY) { if (startX == curX && startY == curY) {
path.lineTo(curX, curY + 2); path.lineTo(curX, curY + 2);
path.lineTo(curX + 1, curY + 2); path.lineTo(curX + 1, curY + 2);
path.lineTo(curX + 1, curY); path.lineTo(curX + 1, curY);
} }
path = new Path();
paths.add(path); paths.add(path);
path = new Path();
} }
@Override @Override

View File

@ -7,7 +7,16 @@
tools:context="draw.simplemobiletools.com.MainActivity"> tools:context="draw.simplemobiletools.com.MainActivity">
<draw.simplemobiletools.com.MyCanvas <draw.simplemobiletools.com.MyCanvas
android:id="@+id/my_canvas"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>
<ImageView
android:id="@+id/undo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="@dimen/activity_margin"
android:src="@mipmap/undo"/>
</RelativeLayout> </RelativeLayout>

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB