David_Zhang 发表于 2012-2-21 20:42 
对于surfaceview使用buildDrawingCache是不行的
可以通过SurfaceHolder来变通地截屏
你在线程里面是否使用 ...
你好 首先谢谢你的回答 ,我按照你的方法做了。没有使用canvas = mHolder.lockCanvas(); 而是自己创建的canvas,但是这个时候,屏幕上就是一片黑的啊 什么也看不见 而且 我在保存自己生成Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); 的时候,还是看不见图像。。
package com.pica.whiteboard.view;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.pica.whiteboard.activity.R;
import com.pica.whiteboard.util.PaintHolder;
public class PaletteView extends SurfaceView implements Runnable, SurfaceHolder.Callback{
private Paint mPaint = null;
//定义集合,存储画笔动作
private ArrayList<Action> actionList;
private Action curAction = null;
boolean mLoop = true;
SurfaceHolder mSurfaceHolder = null;
Bitmap bgBitmap = null; //背景
Bitmap tempBitmap = null; //临时画板
public PaletteView(Context context) {
super(context);
this.mPaint = new Paint();
this.actionList = new ArrayList<Action>();
mSurfaceHolder = this.getHolder();
mSurfaceHolder.addCallback(this);
this.setFocusable(true);
mLoop = true;
// bgBitmap = BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.defaultbgimg));
bgBitmap = Bitmap.createBitmap(320, 320, Config.ARGB_8888);
tempBitmap = Bitmap.createBitmap(320, 320, Config.ARGB_8888);
new Thread(this).start();
}
private void Draw() {
// Canvas canvas = mSurfaceHolder.lockCanvas();
Canvas canvas = new Canvas(bgBitmap);
if (mSurfaceHolder == null || canvas == null) {
return;
}
// canvas.drawColor(Color.parseColor("#00000000"));
// 设置画笔没有锯齿,空心
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
// 画板绘图区背景图片
canvas.drawBitmap(bgBitmap, 0, 0, null);
canvas.drawColor(Color.parseColor("#FFFFFFFF"));
// tempBitmap = Bitmap.createBitmap(bgBitmap.getWidth(), bgBitmap.getHeight(), Config.ARGB_4444);
tempBitmap = Bitmap.createBitmap(320, 320, Config.ARGB_8888);
Canvas canvasTemp = new Canvas(tempBitmap);
canvasTemp.drawColor(Color.TRANSPARENT);
for(int i=0;i <= PaintHolder.curIndex; i++){
actionList.get(i).draw(canvasTemp);
}
// 画当前画笔痕迹
if (curAction != null) {
curAction.draw(canvasTemp);
}
// 在主画板上绘制临时画布上的图像
canvas.drawBitmap(tempBitmap, 0, 0, null);
// mSurfaceHolder.unlockCanvasAndPost(canvas);
}
//触屏事件
@Override
public boolean onTouchEvent(MotionEvent event) {
int mAction = event.getAction();
if(mAction == MotionEvent.ACTION_CANCEL) {
return false;
}
float touchX = event.getX();
float touchY = event.getY();
//点击时
if(mAction == MotionEvent.ACTION_DOWN) {
setCurAction(touchX, touchY);
clearSpareAction();
}
// 拖动时
if (mAction == MotionEvent.ACTION_MOVE) {
if (curAction != null) {
curAction.move(touchX, touchY);
}
}
// 抬起时
if (mAction == MotionEvent.ACTION_UP) {
if (curAction != null) {
curAction.move(touchX, touchY);
actionList.add(curAction);
PaintHolder.curIndex++;
curAction = null;
}
}
return super.onTouchEvent(event);
}
public void setCurAction(float x, float y) {
switch(PaintHolder.curTool) {
case 0:
curAction = new MyPath(x, y, PaintHolder.curSize, PaintHolder.curColor);
break;
case 1:
curAction = new MyLine(x, y, PaintHolder.curSize, PaintHolder.curColor);
break;
case 2:
curAction = new MyCircle(x, y, PaintHolder.curSize, PaintHolder.curColor);
break;
case 3:
curAction = new MyRect(x, y, PaintHolder.curSize, PaintHolder.curColor);
break;
case 4:
curAction = new MyFillRect(x, y, PaintHolder.curSize, PaintHolder.curColor);
break;
case 5:
curAction = new MyFillCircle(x, y, PaintHolder.curSize, PaintHolder.curColor);
break;
case 6:
curAction = new MyEraser(x, y, PaintHolder.curSize, PaintHolder.curColor);
break;
}
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void run() {
while (mLoop) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (mSurfaceHolder) {
Draw();
}
}
}
// 后退前进完成后,缓存的动作
private void clearSpareAction() {
for (int i = actionList.size() - 1; i > PaintHolder.curIndex; i--) {
actionList.remove(i);
}
}
public int getListLength() {
return this.actionList.size();
}
public Bitmap getBitMap(){
return this.bgBitmap;
}
}
|