Surfaceview问题。

| 我有一个闪烁的问题。 这是我的代码。
public class Tutorial2D3 extends Activity {

Panel panel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    panel = new Panel(this);
    setContentView(panel);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(1, 1, 1, \"Clean Canvas\");
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    panel.cleanCanvas();
    return true;
}

class Panel extends SurfaceView implements SurfaceHolder.Callback {
    TutorialThread thread;
    Bitmap icon;
    int iconWidth;
    int iconHeight;
    int touchX;
    int touchY;
    int mCount = 0;

    public Panel(Context context) {
        super(context);
        icon = BitmapFactory
                .decodeResource(getResources(), R.drawable.icon);
        iconWidth = icon.getWidth();
        iconHeight = icon.getHeight();
        getHolder().addCallback(this);
        thread = new TutorialThread(getHolder(), this);
        setFocusable(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int x = touchX - (iconWidth / 2);
        int y = touchY - (iconHeight / 2);
        if(mCount>0) {
            canvas.drawColor(Color.BLACK);
            mCount--;
        }
        canvas.drawBitmap(icon, (x > 0 ? x : 0), (y > 0 ? y : 0), null);    
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        thread.setRunning(true);
        thread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        thread.setRunning(false);
        do {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } while (retry);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            touchX = (int) event.getX();
            touchY = (int) event.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            touchX = (int) event.getX();
            touchY = (int) event.getY();
            break;
        case MotionEvent.ACTION_UP:
            break;
        default:
            break;
        }
        return true;
    }

    private void cleanCanvas() {
        mCount = 2;
    }
}



class TutorialThread extends Thread {
    private SurfaceHolder _surfaceHolder;
    private Panel _panel;
    private boolean _run = false;

    public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
        _surfaceHolder = surfaceHolder;
        _panel = panel;
    }

    public void setRunning(boolean run) {
        _run = run;
    }

    @Override
    public void run() {
        Canvas c;
        while (_run) {
            c = null;
            try {
                c = _surfaceHolder.lockCanvas(null);
                synchronized (_surfaceHolder) {
                    _panel.onDraw(c);
                }
            } finally {
                if (c != null) {
                    _surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}
} 绘制的图像闪烁。 它看起来像在一个点上绘制的位图是在一个表面上绘制的,而不是在另一个表面上绘制的,所以它看起来像是闪烁的,当我们触摸action_up时绘制的位图是一个实心图像,并且不会闪烁。有人可以帮我这个忙。 谢谢     
已邀请:
在SurfaceView的“画布”中进行绘制时,必须始终绘制表面的每个像素。 在这里,您并不总是清除
onDraw()
中的“画布”,因此闪烁。     
为了缓解这种情况(并有点与Guillaume矛盾),您可以做的一件事是使用surfaceholder.lockCanvas(rectangle),其中仅是画布上指定的矩形部分,然后再进行绘制(但是必须绘制每个像素)该矩形)。这是从LunarLandar示例中提取的:
@Override
    public void run() {
        while (mRun) {
            Canvas c = null;
            try {
                c = mSurfaceHolder.lockCanvas(Rectangle);
                synchronized (mSurfaceHolder) {
                    if (mMode == STATE_RUNNING) updatePhysics();
                    doDraw(c);
                }
            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don\'t leave the Surface in an
                // inconsistent state
                if (c != null) {
                    mSurfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
    
我没有阅读所有的代码,但是我认为本文会有所帮助。 本文的本质是闪烁是由于双重缓冲引起的,可以通过不绘制参数Canvas而是绘制用作画布的位图,然后将该位图绘制到arg Canvas来消除闪烁:
int myCanvas_w, myCanvas_h;
Bitmap myCanvasBitmap = null;
Canvas myCanvas = null;
Matrix identityMatrix;

@Override
public void surfaceCreated(SurfaceHolder holder) {

myCanvas_w = getWidth();
myCanvas_h = getHeight();
myCanvasBitmap = Bitmap.createBitmap(myCanvas_w, myCanvas_h, Bitmap.Config.ARGB_8888);
myCanvas = new Canvas();
myCanvas.setBitmap(myCanvasBitmap);

identityMatrix = new Matrix();
}

@Override
protected void onDraw(Canvas canvas) {

paint.setStyle(Paint.Style.STROKE);
 paint.setStrokeWidth(3);

 //int w = myCanvas.getWidth();
 //int h = myCanvas.getHeight();
 int x = random.nextInt(myCanvas_w-1);
 int y = random.nextInt(myCanvas_h-1);
 int r = random.nextInt(255);
 int g = random.nextInt(255);
 int b = random.nextInt(255);

 paint.setColor(0xff000000 + (r << 16) + (g << 8) + b);
 myCanvas.drawPoint(x, y, paint); // <--------- Here\'s where you draw on your bitmap

 canvas.drawBitmap(myCanvasBitmap, identityMatrix, null);
 // ^---------- And here\'s where you draw that bitmap to the canvas

}
    

要回复问题请先登录注册