DrawView

package com.cinread.note.view;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Path;

import android.graphics.PorterDuff;

import android.graphics.PorterDuffXfermode;

import android.graphics.RectF;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

import android.view.WindowManager;

import com.cinread.note.utils.FileUtils;

import com.cinread.note.utils.LogUtils;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.util.ArrayList;

/**

* @project:Note

* @package:com.cinread.note.view

* @author:pengjf

* @update:2016/4/7

* @desc: TODO

*/

// Created by pengjf on 2016/4/7.

public class DrawView extends View {

private static final float TOUCH_TOLERANCE = 4; //thumb width

private boolean isPaint = true;

private Bitmap mBitmapInit;

private Bitmap mBitmap;

private Paint mPaint;

private Paint mErase;

private Paint mBitmapPaint;

private Canvas mCanvas;

private Path mPath;

private float mX;

private float mY;

private int mWidth;

private int mHeight;

private ArrayList paths = new ArrayList();

private ArrayList undonePaths = new ArrayList();

public DrawView(Context context) {

this(context, null);

}

public DrawView(Context context, AttributeSet attrs) {

super(context, attrs);

mPaint = new Paint();

mErase = new Paint();

initPaint(2);

initErase();

WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

mWidth = manager.getDefaultDisplay().getWidth();

mHeight = manager.getDefaultDisplay().getHeight();

mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);

mCanvas = new Canvas(mBitmap);

mPath = new Path();

mBitmapPaint = new Paint(Paint.DITHER_FLAG);

}

private void initPaint(int width) {

mPaint.setDither(true);

mPaint.setAntiAlias(true);

mPaint.setStrokeWidth(width);

mPaint.setColor(Color.BLACK);

mPaint.setStyle(Paint.Style.STROKE);

mPaint.setStrokeJoin(Paint.Join.ROUND);

mPaint.setStrokeCap(Paint.Cap.ROUND);

}

private void initErase() {

mErase.setAntiAlias(true);

mErase.setDither(true);

mErase.setStrokeWidth(20);

mErase.setColor(0xFFFFFFFF);

mErase.setStyle(Paint.Style.STROKE);

mErase.setStrokeJoin(Paint.Join.ROUND);

mErase.setStrokeCap(Paint.Cap.ROUND);

mErase.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

if (isPaint) {

for (Path p : paths) {

canvas.drawPath(p, mPaint);

LogUtils.d(" - -- - ");

}

//canvas.drawPath(mPath, mPaint);

} else

canvas.drawPoint(10, 10, mErase);

}

@Override

public boolean onTouchEvent(MotionEvent event) {

float x = event.getX();

float y = event.getY();

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

Touch_Down(x, y);

break;

case MotionEvent.ACTION_MOVE:

Touch_Move(x, y);

break;

case MotionEvent.ACTION_UP:

if (isPaint)

Touch_Up(mPaint);

else

Touch_Up(mErase);

break;

}

invalidate();

return true;

}

private void Touch_Down(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(Paint paint) {

mPath.lineTo(mX, mY);

mCanvas.drawPath(mPath, paint);

mPath = new Path();

paths.add(mPath);

mPath.reset();

}

public void undo() {

if (paths.size() > 0) {

LogUtils.d("undo " + paths.size());

clearDraw();

undonePaths.add(paths.remove(paths.size() - 1));

invalidate();

}

}

public void redo() {

if (undonePaths.size() > 0) {

LogUtils.d("redo " + undonePaths.size());

clearDraw();

paths.add(undonePaths.remove(undonePaths.size() - 1));

invalidate();

}

}

public void setPaint(int width) {

isPaint = true;

initPaint(width);

invalidate();

}

public void setErase() {

isPaint = false;

initErase();

invalidate();

}

public void setBitmap(String url) {

InputStream is = null;

try {

is = new FileInputStream(url);

mBitmapInit = BitmapFactory.decodeStream(is);

drawBitmapToCanvas(mBitmapInit);

} catch (FileNotFoundException e) {

e.printStackTrace();

} finally {

FileUtils.close(is);

}

invalidate();

}

public void clearDraw() {

mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);

mCanvas.setBitmap(mBitmap);

invalidate();

}

public void clear() {

paths.clear();

undonePaths.clear();

invalidate();

}

private void drawBitmapToCanvas(Bitmap bitmap) {

if (bitmap.getHeight() > mHeight || bitmap.getWidth() > mWidth) {

RectF rectF = new RectF(0, 0, mWidth, mHeight);

mCanvas.drawBitmap(bitmap, null, rectF, mBitmapPaint);

} else {

mCanvas.drawBitmap(bitmap, 0, 0, mBitmapPaint);

}

}

public void BitmapToPicture(File file, Bitmap bitmap) {

FileOutputStream fos = null;

try {

fos = new FileOutputStream(file);

if (null != fos) {

bitmap.compress(Bitmap.CompressFormat.PNG, 10, fos);

fos.flush();

}

} catch (Exception e) {

e.printStackTrace();

} finally {

FileUtils.close(fos);

}

}

}

xml

android:id="@+id/paintView"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

842098a0a29cbd457a30fb8324577d11.png

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐