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
没有合适的资源?快使用搜索试试~ 我知道了~
Android 画图板
共59个文件
class:31个
java:13个
xml:5个
5星 · 超过95%的资源 需积分: 9 40 下载量 146 浏览量
2013-11-23
11:42:30
上传
评论
收藏 143KB ZIP 举报
温馨提示
这个代码是我博客中的源码: http://blog.csdn.net/leehong2005/article/details/7334959 实现的功能也不全,也是两年前的代码了,供大家一个参考。
资源推荐
资源详情
资源评论
收起资源包目录
SketchPadDemo.zip (59个子文件)
SketchPadDemo
bin
classes.dex 41KB
com
lee
demo
view
SketchPadView$SketchPadUndoStack.class 3KB
SketchPadView.class 9KB
ColorSelector$ColorSelectorCallback.class 366B
ColorSelector.class 4KB
ColorSelector$1.class 2KB
activity
MainActivity.class 9KB
MainActivity$1.class 2KB
PictureSelectActivity$1.class 1023B
R$layout.class 480B
PictureSelectActivity.class 6KB
R$id.class 1020B
R$drawable.class 409B
R$attr.class 349B
R.class 552B
PictureSelectActivity$PictureAdapter.class 3KB
PictureSelectActivity$2.class 2KB
R$string.class 1KB
PictureSelectActivity$3.class 3KB
interfaces
IUndoCommand.class 271B
ISketchPadCallback.class 307B
ISketchPadTool.class 294B
utils
MediaUtil$MediaUtilHandler.class 2KB
MediaUtil.class 8KB
MediaUtil$MediaUtilWorker.class 2KB
MediaUtil$VideoInfo.class 2KB
MediaUtil$IMediaCallback.class 483B
CommonDef.class 545B
MediaUtil$ImageInfo.class 1KB
SketchPadEraser.class 3KB
SketchPadPen.class 2KB
BitmapUtil.class 7KB
resources.ap_ 15KB
SketchPadDemo.apk 37KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
values
strings.xml 1KB
drawable-mdpi
icon.png 3KB
layout
sketchpad.xml 4KB
pictureselect.xml 3KB
pictureitem.xml 713B
assets
default.properties 364B
gen
com
lee
demo
activity
R.java 3KB
src
com
lee
demo
view
SketchPadView.java 17KB
ColorSelector.java 5KB
SketchPadView.java.bak 17KB
activity
PictureSelectActivity.java 7KB
MainActivity.java 10KB
interfaces
IUndoCommand.java 271B
ISketchPadCallback.java 339B
ISketchPadTool.java 346B
utils
BitmapUtil.java 7KB
MediaUtil.java 12KB
SketchPadEraser.java 2KB
SketchPadPen.java 2KB
CommonDef.java 376B
.project 849B
.classpath 280B
AndroidManifest.xml 984B
共 59 条
- 1
资源评论
- 善良的卡库2014-07-01嗯,学习了,好东西。。。。
- 黑羽狐2013-11-24感謝 參考內容非常受用~
- 「已注销」2014-08-08不错的,初步了解下这方面还是很有用~
leehong2005
- 粉丝: 949
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功