SurfaceView绘图时刷新问题,尝试各种办法无法解决,请教高手

时间:2022-06-13 08:45:22

SurfaceView绘图时刷新问题,尝试各种办法无法解决,请教高手

/**
*
*/

源码:http://pan.baidu.com/s/1i3FtdZZ

画图时最左面,第一帧总是出现一个黑条,其它的帧没有问题
package com.macrosoft.testewaveview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

/**
* @author a1
*
*/
public class WaveView extends SurfaceView implements Callback,Runnable{

private Context mContext;
private SurfaceHolder surfaceHolder;
private boolean flag = false;//线程标识
private Bitmap bitmapBackground;

private float mSurfaceWidth,mSurfaceHeight;//屏幕宽高
private int mBitmapPos;

private Canvas mCanvas;

private Thread thread;

Paint[] paints = new Paint[3];
//背景移动状态
private enum State{
LEFT,RIGHT;
}

private State state = State.LEFT;
private final int BITMAP_STEP = 1;//背景画布移动步伐

public WaveView(Context context,AttributeSet attrs) {
super(context,attrs);
//setBackgroundColor(Color.GREEN);
paints[0] = new Paint();
paints[1] = new Paint();
paints[2] = new Paint();
paints[0].setColor(Color.RED);
paints[0].setStrokeWidth(2);
paints[1].setStrokeWidth(2);
paints[2].setStrokeWidth(2);
paints[1].setColor(Color.BLACK);
paints[2].setColor(Color.BLUE);
mBitmapPos = 0;
flag = true;
this.mContext = context;
setFocusable(true);
setFocusableInTouchMode(true);
surfaceHolder = getHolder();
surfaceHolder.addCallback(this);
}

public Bitmap GenerateBackBitmap(int width, int height) {

Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);
// 准备画笔
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(3);
Paint paintPoint = new Paint();
paintPoint.setColor(Color.rgb(220, 190, 50));
Paint paintLine = new Paint();
paintLine.setColor(Color.rgb(0, 0, 0));
// Paint paintBack = new Paint();
// paintBack.setColor(Color.rgb(255, 255, 255));

int cx = canvas.getWidth();// - canvas.getWidth()%25;
int cy = canvas.getHeight();// - (canvas.getHeight() - 21) % 25 - 21;

Rect r = new Rect(0, 0, cx - 1, cy - 1);
canvas.drawRect(r, paint);
// ////////////画点///////////////
for (int x = 0; x < width; x += 5) {
for (int y = 0; y < height; y += 5) {
canvas.drawPoint(x, y, paintPoint);
}
}
// 画纵轴
for (int x = 0; x < width; x += 25) {
canvas.drawLine(x, 0, x, height, paintLine);
}
// 画横轴
for (int y = 0; y < height; y += 25) {
canvas.drawLine(0, y, width, y, paintLine);
}
// 绘制坐标轴
canvas.drawLine(0, mSurfaceHeight / 6, mSurfaceWidth,
mSurfaceHeight / 6, paints[0]);
canvas.drawLine(0, 3*mSurfaceHeight / 6, mSurfaceWidth,
3*mSurfaceHeight / 6, paints[1]);
canvas.drawLine(0, 5*mSurfaceHeight / 6, mSurfaceWidth,
5*mSurfaceHeight / 6, paints[2]);
return bitmap;
}

protected void OnDraw(){
drawBG();
//updateBG();
}

private void drawBG() {
mCanvas.drawColor(Color.WHITE,PorterDuff.Mode.ADD);
//mCanvas.drawColor(Color.BLACK);
mCanvas.drawBitmap(bitmapBackground, mBitmapPos, 0,null);
}
/**
* 更新背景
*
*/
private void updateBG() {
/**图片滚动效果**/
switch(state){
case LEFT:
mBitmapPos -= BITMAP_STEP;
break;
case RIGHT:
mBitmapPos += BITMAP_STEP;
break;
default:
break;
}
if(mBitmapPos <= -mSurfaceWidth/2){
state = State.RIGHT;
}
if(mBitmapPos >= 0){
state = State.LEFT;
}
}

int xpos = 0;
int oldX = 0,x;
int oldY1,oldY2,oldY3,y1,y2,y3;

@Override
public void run() {
//OnDraw();
while(flag){
synchronized(surfaceHolder){
if(xpos > mSurfaceWidth){
xpos = 0;
}
if(xpos == 00){
mCanvas = surfaceHolder.lockCanvas();
OnDraw();
surfaceHolder.unlockCanvasAndPost(mCanvas);
}

Canvas canvas = surfaceHolder.lockCanvas(new Rect(xpos, 0,
xpos + 50, (int) mSurfaceHeight));

for (int i = 0; i < 50; i++) {
x = xpos + i;
y1 = (int) ((int) (mSurfaceHeight/6*Math.sin(2*Math.PI*x/50) + mSurfaceHeight/6) + 0);
y2 = (int) ((int) (mSurfaceHeight/6*Math.cos(2*Math.PI*x/50) + mSurfaceHeight/6) + 2*mSurfaceHeight/6);
y3 = (int) ((int) (mSurfaceHeight/6*Math.sin(2*Math.PI*x/50) + mSurfaceHeight/6) + 4*mSurfaceHeight/6);
canvas.drawLine(oldX, oldY1, x, y1, paints[0]);
canvas.drawLine(oldX, oldY2, x, y2, paints[1]);
canvas.drawLine(oldX, oldY3, x, y3, paints[2]);
oldX = x;
oldY1 = y1;
oldY2 = y2;
oldY3 = y3;

}

surfaceHolder.unlockCanvasAndPost(canvas);

xpos+=50;

try{
Thread.sleep(100);

}catch(InterruptedException e){
e.printStackTrace();
}
}
}

}

public void ClearDraw(){
Canvas canvas = null;
try{
canvas = surfaceHolder.lockCanvas(null);
canvas.drawColor(Color.WHITE);
canvas.drawColor(Color.TRANSPARENT,PorterDuff.Mode.SRC);
}catch(Exception e){

}finally{
if(canvas != null){
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
mSurfaceWidth = getWidth();
mSurfaceHeight = getHeight();
int mWidth = (int)(mSurfaceWidth*3/2);
/**将图片缩放到屏幕的3/2倍**/
bitmapBackground = GenerateBackBitmap((int)mSurfaceWidth,(int)mSurfaceHeight);
//bitmapBackground = GenerateBackBitmap((int)mWidth,(int)mSurfaceHeight);
//ClearDraw();

mCanvas = surfaceHolder.lockCanvas();
OnDraw();
surfaceHolder.unlockCanvasAndPost(mCanvas);
thread = new Thread(this);
thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
flag = false;

}

}