Skip to content

Commit acd049c

Browse files
committed
Drawing Paths
1 parent 63b3121 commit acd049c

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.dragosholban.androiddrawing;
2+
3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.Paint;
6+
import android.graphics.Path;
7+
import android.os.Build;
8+
import android.support.annotation.Nullable;
9+
import android.support.annotation.RequiresApi;
10+
import android.util.AttributeSet;
11+
import android.view.View;
12+
13+
import java.util.ArrayList;
14+
15+
public class DrawingView extends View {
16+
17+
private ArrayList<Path> paths = new ArrayList<>();
18+
19+
public DrawingView(Context context) {
20+
super(context);
21+
}
22+
23+
public DrawingView(Context context, @Nullable AttributeSet attrs) {
24+
super(context, attrs);
25+
}
26+
27+
public DrawingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
28+
super(context, attrs, defStyleAttr);
29+
}
30+
31+
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
32+
public DrawingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
33+
super(context, attrs, defStyleAttr, defStyleRes);
34+
}
35+
36+
public void addPath(Path path) {
37+
paths.add(path);
38+
}
39+
40+
public Path getLastPath() {
41+
if (paths.size() > 0) {
42+
return paths.get(paths.size() - 1);
43+
}
44+
45+
return null;
46+
}
47+
48+
@Override
49+
protected void onDraw(Canvas canvas) {
50+
super.onDraw(canvas);
51+
52+
for (Path path : paths) {
53+
Paint paint = new Paint();
54+
paint.setColor(0X80000000);
55+
paint.setStyle(Paint.Style.STROKE);
56+
paint.setStrokeWidth(3f);
57+
canvas.drawPath(path, paint);
58+
}
59+
}
60+
}

app/src/main/java/com/dragosholban/androiddrawing/MainActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55

66
public class MainActivity extends AppCompatActivity {
77

8+
DrawingView drawingView;
9+
810
@Override
911
protected void onCreate(Bundle savedInstanceState) {
1012
super.onCreate(savedInstanceState);
1113
setContentView(R.layout.activity_main);
14+
15+
drawingView = findViewById(R.id.canvas);
16+
drawingView.setOnTouchListener(new TouchListener());
1217
}
1318
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.dragosholban.androiddrawing;
2+
3+
import android.graphics.Path;
4+
import android.view.MotionEvent;
5+
import android.view.View;
6+
7+
public class TouchListener implements View.OnTouchListener {
8+
9+
@Override
10+
public boolean onTouch(View view, MotionEvent event) {
11+
float x = event.getX();
12+
float y = event.getY();
13+
DrawingView drawingView = (DrawingView) view;
14+
Path path;
15+
16+
switch (event.getAction() & MotionEvent.ACTION_MASK) {
17+
case MotionEvent.ACTION_DOWN:
18+
path = new Path();
19+
path.moveTo(x, y);
20+
drawingView.addPath(path);
21+
break;
22+
case MotionEvent.ACTION_MOVE:
23+
path = drawingView.getLastPath();
24+
if (path != null) {
25+
path.lineTo(x, y);
26+
}
27+
break;
28+
}
29+
30+
drawingView.invalidate();
31+
32+
return true;
33+
}
34+
}
35+

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
android:layout_height="match_parent"
77
android:background="#AFAFAF"
88
tools:context=".MainActivity">
9-
<View
9+
<com.dragosholban.androiddrawing.DrawingView
1010
android:id="@+id/canvas"
1111
android:layout_width="0dp"
1212
android:layout_height="0dp"

0 commit comments

Comments
 (0)