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;
|
||||
}
|
||||
}
|
@ -4,14 +4,10 @@
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
tools:context="draw.simplemobiletools.com.MainActivity">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Hello World!"/>
|
||||
<draw.simplemobiletools.com.MyCanvas
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
@ -1,6 +0,0 @@
|
||||
<resources>
|
||||
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
|
||||
(such as screen margins) for screens with more than 820dp of available width. This
|
||||
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
|
||||
<dimen name="activity_horizontal_margin">64dp</dimen>
|
||||
</resources>
|
@ -1,5 +1,3 @@
|
||||
<resources>
|
||||
<!-- Default screen margins, per the Android Design guidelines. -->
|
||||
<dimen name="activity_horizontal_margin">16dp</dimen>
|
||||
<dimen name="activity_vertical_margin">16dp</dimen>
|
||||
<dimen name="activity_margin">16dp</dimen>
|
||||
</resources>
|
||||
|
Reference in New Issue
Block a user