- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
-
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.graphics.Bitmap.CompressFormat;
- import android.os.Environment;
- import android.view.MotionEvent;
- import android.view.View;
-
-
- public class TuyaView extends View {
-
- private Bitmap mBitmap;
- private Canvas mCanvas;
- private Path mPath;
- private Paint mBitmapPaint;
- private Paint mPaint;
- private float mX, mY;
- private static final float TOUCH_TOLERANCE = 4;
-
-
- private static List<DrawPath> savePath;
-
- private DrawPath dp;
-
- private int screenWidth, screenHeight;
-
- private class DrawPath {
- public Path path;
- public Paint paint;
- }
-
- public TuyaView(Context context, int w, int h) {
- super(context);
- screenWidth = w;
- screenHeight = h;
-
- mBitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888);
-
- mCanvas = new Canvas(mBitmap);
-
- mBitmapPaint = new Paint(Paint.DITHER_FLAG);
- mPaint = new Paint();
- mPaint.setAntiAlias(true);
- mPaint.setStyle(Paint.Style.STROKE);
- mPaint.setStrokeJoin(Paint.Join.ROUND);
- mPaint.setStrokeCap(Paint.Cap.ROUND);
- mPaint.setStrokeWidth(5);
-
- savePath = new ArrayList<DrawPath>();
- }
-
- @Override
- public void onDraw(Canvas canvas) {
- canvas.drawColor(0xFFAAAAAA);
-
- canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
- if (mPath != null) {
-
- canvas.drawPath(mPath, mPaint);
- }
- }
-
- private void touch_start(float x, float y) {
- 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(mY - y);
- 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);
- mCanvas.drawPath(mPath, mPaint);
-
- savePath.add(dp);
- mPath = null;
- }
-
- public void undo() {
- if (savePath != null && savePath.size() > 0) {
- savePath.remove(savePath.size() - 1);
- redrawOnBitmap();
- }
- }
-
- public void redo(){
- if (savePath != null && savePath.size() > 0) {
- savePath.clear();
- redrawOnBitmap();
- }
- }
-
- private void redrawOnBitmap(){
- mBitmap = Bitmap.createBitmap(screenWidth, screenHeight,
- Bitmap.Config.ARGB_8888);
- mCanvas.setBitmap(mBitmap);
- Iterator<DrawPath> iter = savePath.iterator();
- while (iter.hasNext()) {
- DrawPath drawPath = iter.next();
- mCanvas.drawPath(drawPath.path, drawPath.paint);
- }
- invalidate();
- }
-
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- float x = event.getX();
- float y = event.getY();
-
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
-
- mPath = new Path();
-
- dp = new DrawPath();
- dp.path = mPath;
- dp.paint = mPaint;
- touch_start(x, y);
- invalidate();
- break;
- case MotionEvent.ACTION_MOVE:
- touch_move(x, y);
- invalidate();
- break;
- case MotionEvent.ACTION_UP:
- touch_up();
- invalidate();
- break;
- }
- return true;
- }
-
- public void saveToSDCard(){
- String fileUrl = Environment.getExternalStorageDirectory()
- .toString() + "/android/data/test.png";
- try {
- FileOutputStream fos = new FileOutputStream(new File(fileUrl));
- mBitmap.compress(CompressFormat.PNG, 100, fos);
- fos.flush();
- fos.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.DisplayMetrics;
- import android.util.Log;
- import android.view.KeyEvent;
-
- public class TuyaActivity extends Activity {
-
- private TuyaView tuyaView = null;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- DisplayMetrics dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
-
- tuyaView = new TuyaView(this, dm.widthPixels, dm.heightPixels);
- setContentView(tuyaView);
- }
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- tuyaView.undo();
- return true;
- }else if(keyCode == KeyEvent.KEYCODE_MENU){
- tuyaView.redo();
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
-
- }
转载于:https://www.cnblogs.com/Free-Thinker/p/4750391.html