//基础类
public class Action {
public int color;
Action() {
color=Color.BLACK;
}
Action(int color) {
this.color = color;
}
public void draw(Canvas canvas) {
}
public void move(float mx,float my){
}
}
// 点
class MyPoint extends Action {
public float x;
public float y;
MyPoint(float px, float py, int color) {
super(color);
this.x = px;
this.y = py;
}
public void draw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(color);
canvas.drawPoint(x, y, paint);
}
}
public class Palette extends Activity {
PaletteView paletteView=null;
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("", "...onTouchEvent");
return paletteView.onTouchEvent(event);
}
@Override
public void run() {
while (mLoop) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (mSurfaceHolder) {
Draw();
Log.i("View", "..run..");
}
}
}
// 画主画板
private void drawMainPallent(Canvas canvas) {
Log.i("View", "...drawMainPallent...");
// 设置画笔没有锯齿,空心
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
// 画板绘图区背景图片
canvas.drawBitmap(bgBitmap, bgBitmapX, bgBitmapY, null);
newbit=Bitmap.createBitmap(bgBitmapWidth, bgBitmapHeight,
Config.ARGB_4444);
Canvas canvasTemp = new Canvas(newbit);
canvasTemp.drawColor(Color.TRANSPARENT);
for(int i=0;i<=currentPaintIndex;i++){
actionList.get(i).draw(canvasTemp);
}
// 画当前画笔痕迹
if (curAction != null) {
curAction.draw(canvasTemp);
}
// 在主画板上绘制临时画布上的图像
canvas.drawBitmap(newbit, bgBitmapX, bgBitmapY, null);
// 画板绘图区边框
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(3);
canvas.drawRect(bgBitmapX - 2, bgBitmapY - 2, bgBitmapX + bgBitmapWidth
+ 2, bgBitmapY + bgBitmapHeight + 2, mPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.i("View", "...onTouchEvent..22.");
int antion = event.getAction();
if (antion == MotionEvent.ACTION_CANCEL) {
return false;
}
float touchX = event.getX();
float touchY = event.getY();
// 点击时
if (antion == MotionEvent.ACTION_DOWN) {
// 检测点击点是否在主绘图区
if (testTouchMainPallent(touchX, touchY)) {
setCurAction(getRealX(touchX), getRealY(touchY));
clearSpareAction();
}
// 检测点击点是否在画笔选择区
testTouchToolsPanel(touchX, touchY);
// 检测点击点是否在画笔大小选择区
testTouchSizePanel(touchX, touchY);
// 检测点击点是否在颜色选择区
testTouchColorPanel(touchX, touchY);
// 检测点击点是否在按钮上
testTouchButton(touchX, touchY);
}
// 拖动时
if (antion == MotionEvent.ACTION_MOVE) {
if (curAction != null) {
curAction.move(getRealX(touchX), getRealY(touchY));
}
}
// 抬起时
if (antion == MotionEvent.ACTION_UP) {
if (curAction != null) {
curAction.move(getRealX(touchX), getRealY(touchY));
actionList.add(curAction);
currentPaintIndex++;
curAction = null;
}
isBackPressed = false;
isForwardPressed = false;
}
return super.onTouchEvent(event);
}