package com.lee.demo.view;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.lee.demo.interfaces.ISketchPadCallback;
import com.lee.demo.interfaces.ISketchPadTool;
import com.lee.demo.interfaces.IUndoCommand;
import com.lee.demo.utils.BitmapUtil;
import com.lee.demo.utils.CommonDef;
import com.lee.demo.utils.SketchPadEraser;
import com.lee.demo.utils.SketchPadPen;
/**
* The SketchPadView class provides method to draw strokes on it like as a canvas or sketch pad.
* We use touch event to draw strokes, when user touch down and touch move, we will remember these
* point of touch move and set them to a Path object, then draw the Path object, so that user can see the
* strokes are drawing real time. When touch up event is occurring, we draw the path to a bitmap which
* is hold by a canvas, and then draw the bitmap to canvas to display these strokes to user.
*
* @author Li Hong
*
* @date 2010/07/30
*
*/
public class SketchPadView extends View implements IUndoCommand
{
public static final int STROKE_NONE = 0;
public static final int STROKE_PEN = 1;
public static final int STROKE_ERASER = 2;
public static final int UNDO_SIZE = 20;
private boolean m_isEnableDraw = true;
private boolean m_isDirty = false;
private boolean m_isTouchUp = false;
private boolean m_isSetForeBmp = false;
private int m_bkColor = Color.WHITE;
private int m_strokeType = STROKE_PEN;
private int m_strokeColor = Color.BLACK;
private int m_penSize = CommonDef.MIDDLE_PEN_WIDTH;
private int m_eraserSize = CommonDef.MIDDLE_ERASER_WIDTH;
private int m_canvasWidth = 100;
private int m_canvasHeight = 100;
private boolean m_canClear = true;
private Paint m_bitmapPaint = null;
private Bitmap m_foreBitmap = null;
private SketchPadUndoStack m_undoStack = null;
private Bitmap m_tempForeBitmap = null;
private Bitmap m_bkBitmap = null;
private Canvas m_canvas = null;
private ISketchPadTool m_curTool = null;
private ISketchPadCallback m_callback = null;
public SketchPadView(Context context)
{
this(context, null);
}
public SketchPadView(Context context, AttributeSet attrs)
{
super(context, attrs);
initialize();
}
public SketchPadView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
initialize();
}
public boolean isDirty()
{
return m_isDirty;
}
public void setDrawStrokeEnable(boolean isEnable)
{
m_isEnableDraw = isEnable;
}
public void setBkColor(int color)
{
if (m_bkColor != color)
{
m_bkColor = color;
invalidate();
}
}
public void setForeBitmap(Bitmap foreBitmap)
{
if (foreBitmap != m_foreBitmap && null != foreBitmap)
{
// Recycle the bitmap.
if (null != m_foreBitmap)
{
m_foreBitmap.recycle();
}
m_isSetForeBmp = true;
// Remember the temporary fore bitmap.
m_tempForeBitmap = BitmapUtil.duplicateBitmap(foreBitmap);
// Here create a new fore bitmap to avoid crashing when set bitmap to canvas.
m_foreBitmap = BitmapUtil.duplicateBitmap(foreBitmap);
if (null != m_foreBitmap && null != m_canvas)
{
m_canvas.setBitmap(m_foreBitmap);
}
m_canClear = true;
invalidate();
}
}
public Bitmap getForeBitmap()
{
return m_foreBitmap;
}
public void setBkBitmap(Bitmap bmp)
{
if (m_bkBitmap != bmp)
{
m_bkBitmap = bmp;
invalidate();
}
}
public Bitmap getBkBitmap()
{
return m_bkBitmap;
}
public void setStrokeType(int type)
{
switch(type)
{
case STROKE_PEN:
m_curTool = new SketchPadPen(m_penSize, m_strokeColor);
break;
case STROKE_ERASER:
m_curTool = new SketchPadEraser(m_eraserSize);
break;
}
m_strokeType = type;
}
public void setStrokeSize(int size, int type)
{
switch(type)
{
case STROKE_PEN:
m_penSize = size;
break;
case STROKE_ERASER:
m_eraserSize = size;
break;
}
}
public void setStrokeColor(int color)
{
m_strokeColor = color;
}
public int getStrokeSize()
{
return m_penSize;
}
public int getStrokeColor()
{
return m_strokeColor;
}
public void clearAllStrokes()
{
if (m_canClear)
{
// Clear the undo stack.
m_undoStack.clearAll();
// Recycle the temporary fore bitmap
if (null != m_tempForeBitmap)
{
m_tempForeBitmap.recycle();
m_tempForeBitmap = null;
}
// Create a new fore bitmap and set to canvas.
createStrokeBitmap(m_canvasWidth, m_canvasHeight);
invalidate();
m_isDirty = true;
m_canClear = false;
}
}
public Bitmap getCanvasSnapshot()
{
setDrawingCacheEnabled(true);
buildDrawingCache(true);
Bitmap bmp = getDrawingCache(true);
if (null == bmp)
{
android.util.Log.d("leehong2", "getCanvasSnapshot getDrawingCache == null");
}
return BitmapUtil.duplicateBitmap(bmp);
}
public void setCallback(ISketchPadCallback callback)
{
m_callback = callback;
}
public ISketchPadCallback getCallback()
{
return m_callback;
}
@Override
public void onDeleteFromRedoStack()
{
// Do nothing currently.
}
@Override
public void onDeleteFromUndoStack()
{
// Do nothing currently.
}
@Override
public void redo()
{
if (null != m_undoStack)
{
m_undoStack.redo();
}
}
@Override
public void undo()
{
if (null != m_undoStack)
{
m_undoStack.undo();
}
}
@Override
public boolean canUndo()
{
if (null != m_undoStack)
{
return m_undoStack.canUndo();
}
return false;
}
@Override
public boolean canRedo()
{
if (null != m_undoStack)
{
return m_undoStack.canRedo();
}
return false;
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
if (null != m_callback)
{
int action = event.getAction();
if (MotionEvent.ACTION_DOWN == action)
{
m_callback.onTouchDown(this, event);
}
else if (MotionEvent.ACTION_UP == action)
{
m_callback.onTouchUp(this, event);
}
}
if (m_isEnableDraw)
{
m_isTouchUp = false;
switch(event.getAction())
{
case Motio
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
功能说明: 1、能画线(Pen),能擦除(Eraser),清除(Clear)。 2、Undo,Redo功能。 3、保存,加载笔迹(Stroke)。 4、画笔颜色。 5、画板背景色。 6、画笔,橡皮大小。 7、选择画景图片。 说明:当时做的时候就按照WVGA800的模拟器做的,所以分辨率应当设置为800 * 480,横屏模式。
资源推荐
资源详情
资源评论
收起资源包目录
SketchPadDemo.zip (59个子文件)
SketchPadDemo
.project 849B
src
com
lee
demo
view
SketchPadView.java.bak 17KB
SketchPadView.java 17KB
ColorSelector.java 5KB
utils
BitmapUtil.java 7KB
CommonDef.java 376B
SketchPadPen.java 2KB
MediaUtil.java 12KB
SketchPadEraser.java 2KB
activity
PictureSelectActivity.java 7KB
MainActivity.java 10KB
interfaces
ISketchPadTool.java 346B
ISketchPadCallback.java 339B
IUndoCommand.java 271B
AndroidManifest.xml 984B
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 3KB
layout
sketchpad.xml 4KB
pictureselect.xml 3KB
pictureitem.xml 713B
values
strings.xml 1KB
assets
gen
com
lee
demo
activity
R.java 3KB
default.properties 364B
.classpath 280B
bin
resources.ap_ 15KB
com
lee
demo
view
SketchPadView$SketchPadUndoStack.class 3KB
ColorSelector$ColorSelectorCallback.class 366B
SketchPadView.class 9KB
ColorSelector$1.class 2KB
ColorSelector.class 4KB
utils
CommonDef.class 545B
SketchPadEraser.class 3KB
BitmapUtil.class 7KB
MediaUtil$MediaUtilWorker.class 2KB
SketchPadPen.class 2KB
MediaUtil.class 8KB
MediaUtil$ImageInfo.class 1KB
MediaUtil$IMediaCallback.class 483B
MediaUtil$VideoInfo.class 2KB
MediaUtil$MediaUtilHandler.class 2KB
activity
R$layout.class 480B
PictureSelectActivity$PictureAdapter.class 3KB
PictureSelectActivity$3.class 3KB
PictureSelectActivity$1.class 1023B
R$drawable.class 409B
R$attr.class 349B
PictureSelectActivity.class 6KB
R$id.class 1020B
PictureSelectActivity$2.class 2KB
R.class 552B
R$string.class 1KB
MainActivity$1.class 2KB
MainActivity.class 9KB
interfaces
IUndoCommand.class 271B
ISketchPadTool.class 294B
ISketchPadCallback.class 307B
SketchPadDemo.apk 37KB
classes.dex 41KB
共 59 条
- 1
资源评论
- yudex2014-05-23Undo,Redo功能 保存加载功能非常不错
xinxianzhongndsc
- 粉丝: 2
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Picasso_v3.1 2.ipa
- chromedriver-mac-arm64.zip
- 蓝zapro.apk
- chromedriver-linux64.zip
- UCAS研一深度学习实验-MNIST手写数字识别python源码+详细注释(高分项目)
- 基于Python和PyTorch框架完成的一个手写数字识别实验源码(带MINIST手写数字数据集)+详细注释(高分项目)
- 基于Matlab在MNIST数据集上利用CNN完成手写体数字识别任务,并实现单层CNN反向传播算法+源代码+文档说明(高分项目)
- NVIDIA驱动、CUDA和Pytorch及其依赖
- 基于SVM多特征融合的微表情识别python源码+项目说明+详细注释(高分课程设计)
- html动态爱心代码一(附源码)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功