add some basic drawing

This commit is contained in:
tibbi
2016-02-14 23:18:23 +01:00
parent 89ea039fb1
commit 89ccb7749f
4 changed files with 84 additions and 17 deletions

View 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;
}
}

View File

@ -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>

View File

@ -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>

View File

@ -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>