Back to project page TheFirstMyth01.
The source code is released under:
MIT License
If you think the Android project TheFirstMyth01 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.gametest1; // w w w . ja v a 2 s . co m import java.util.ArrayList; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PointF; import android.util.FloatMath; import android.view.MotionEvent; import android.view.View; class CustomView1 extends View { private Paint paint; private ArrayList<PointF> graphics = new ArrayList<PointF>(); private Paint lPaint; private Path mPath; private int of = 0; private Boolean over = false; private float mX, mY; private static final float TOUCH_TOLERANCE = 4; public CustomView1(Context context) { super(context); paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.RED); paint.setStrokeJoin(Paint.Join.ROUND); paint.setStrokeCap(Paint.Cap.ROUND); paint.setStrokeWidth(65); lPaint = new Paint(Paint.ANTI_ALIAS_FLAG); lPaint.setColor(Color.BLACK); lPaint.setStyle(Paint.Style.STROKE);// ?? lPaint.setStrokeJoin(Paint.Join.ROUND); lPaint.setStrokeCap(Paint.Cap.ROUND); lPaint.setStrokeWidth(5); mPath = new Path(); } @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: over = false; graphics.clear(); of = 0; graphics.add(new PointF(x, y)); touch_start(x, y); invalidate(); break; case MotionEvent.ACTION_MOVE: graphics.add(new PointF(x, y)); touch_move(x, y); invalidate(); break; case MotionEvent.ACTION_UP: over = true; touch_up(); invalidate(); break; } return true; } private float spacing(MotionEvent event) { float x = event.getX(0) - event.getX(1); float y = event.getY(0) - event.getY(1); return FloatMath.sqrt(x * x + y * y); } private void touch_start(float x, float y) { mPath.reset(); mPath.moveTo(x, y); mX = x; mY = y; } private void touch_move(float x, float y) { float dx = Math.abs(x - mX); float dy = Math.abs(y - mY); if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2); mX = x; mY = y; } } private void touch_up() { mPath.lineTo(mX, mY); } @Override protected void onDraw(Canvas canvas) { // canvas.drawColor(Color.TRANSPARENT,Mode.CLEAR); canvas.drawColor(Color.WHITE);// ???? canvas.drawPath(mPath, lPaint); if (over && graphics.size() > 0) { canvas.drawPoint(graphics.get(of).x, graphics.get(of).y, paint); of += 1; if (of < graphics.size()) { if (of == graphics.size() - 1) { mPath.reset();// ?????????????? } invalidate(); } } } }