Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Drawing Paths
- Loading branch information
1 parent
63b3121
commit acd049c
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
app/src/main/java/com/dragosholban/androiddrawing/DrawingView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.dragosholban.androiddrawing; | ||
|
||
import android.content.Context; | ||
import android.graphics.Canvas; | ||
import android.graphics.Paint; | ||
import android.graphics.Path; | ||
import android.os.Build; | ||
import android.support.annotation.Nullable; | ||
import android.support.annotation.RequiresApi; | ||
import android.util.AttributeSet; | ||
import android.view.View; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class DrawingView extends View { | ||
|
||
private ArrayList<Path> paths = new ArrayList<>(); | ||
|
||
public DrawingView(Context context) { | ||
super(context); | ||
} | ||
|
||
public DrawingView(Context context, @Nullable AttributeSet attrs) { | ||
super(context, attrs); | ||
} | ||
|
||
public DrawingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
} | ||
|
||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) | ||
public DrawingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { | ||
super(context, attrs, defStyleAttr, defStyleRes); | ||
} | ||
|
||
public void addPath(Path path) { | ||
paths.add(path); | ||
} | ||
|
||
public Path getLastPath() { | ||
if (paths.size() > 0) { | ||
return paths.get(paths.size() - 1); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Override | ||
protected void onDraw(Canvas canvas) { | ||
super.onDraw(canvas); | ||
|
||
for (Path path : paths) { | ||
Paint paint = new Paint(); | ||
paint.setColor(0X80000000); | ||
paint.setStyle(Paint.Style.STROKE); | ||
paint.setStrokeWidth(3f); | ||
canvas.drawPath(path, paint); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
app/src/main/java/com/dragosholban/androiddrawing/TouchListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.dragosholban.androiddrawing; | ||
|
||
import android.graphics.Path; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
|
||
public class TouchListener implements View.OnTouchListener { | ||
|
||
@Override | ||
public boolean onTouch(View view, MotionEvent event) { | ||
float x = event.getX(); | ||
float y = event.getY(); | ||
DrawingView drawingView = (DrawingView) view; | ||
Path path; | ||
|
||
switch (event.getAction() & MotionEvent.ACTION_MASK) { | ||
case MotionEvent.ACTION_DOWN: | ||
path = new Path(); | ||
path.moveTo(x, y); | ||
drawingView.addPath(path); | ||
break; | ||
case MotionEvent.ACTION_MOVE: | ||
path = drawingView.getLastPath(); | ||
if (path != null) { | ||
path.lineTo(x, y); | ||
} | ||
break; | ||
} | ||
|
||
drawingView.invalidate(); | ||
|
||
return true; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters