mirror of
https://github.com/SimpleMobileTools/Simple-Draw.git
synced 2025-06-05 21:59:17 +02:00
add some basic drawing
This commit is contained in:
79
app/src/main/java/draw/simplemobiletools/com/MyCanvas.java
Normal file
79
app/src/main/java/draw/simplemobiletools/com/MyCanvas.java
Normal file
@ -0,0 +1,79 @@
|
||||
package draw.simplemobiletools.com;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Path;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
|
||||
public class MyCanvas extends View {
|
||||
private static final float THRESHOLD = 5;
|
||||
private Paint paint;
|
||||
private Path path;
|
||||
private float startX;
|
||||
private float startY;
|
||||
|
||||
public MyCanvas(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
|
||||
path = new Path();
|
||||
paint = new Paint();
|
||||
paint.setColor(Color.BLACK);
|
||||
paint.setStyle(Paint.Style.STROKE);
|
||||
paint.setStrokeWidth(5f);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
canvas.drawPath(path, paint);
|
||||
}
|
||||
|
||||
private void actionDown(float x, float y) {
|
||||
path.moveTo(x, y);
|
||||
startX = x;
|
||||
startY = y;
|
||||
}
|
||||
|
||||
private void actionMove(float x, float y) {
|
||||
final float dx = Math.abs(x - startX);
|
||||
final float dy = Math.abs(y - startY);
|
||||
if (dx >= THRESHOLD || dy >= THRESHOLD) {
|
||||
path.quadTo(startX, startY, (x + startX) / 2, (y + startY) / 2);
|
||||
startX = x;
|
||||
startY = y;
|
||||
}
|
||||
}
|
||||
|
||||
private void actionUp() {
|
||||
path.lineTo(startX, startY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent event) {
|
||||
final float x = event.getX();
|
||||
final float y = event.getY();
|
||||
|
||||
switch (event.getAction()) {
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
actionDown(x, y);
|
||||
invalidate();
|
||||
break;
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
actionMove(x, y);
|
||||
invalidate();
|
||||
break;
|
||||
case MotionEvent.ACTION_UP:
|
||||
actionUp();
|
||||
invalidate();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user