Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android上绘制画布-屏幕闪烁/撕裂_Android - Fatal编程技术网

在Android上绘制画布-屏幕闪烁/撕裂

在Android上绘制画布-屏幕闪烁/撕裂,android,Android,我有一张画在画布上的等距图。当我试着移动地图时,这些黑线在一些瓷砖之间闪烁,使整个东西看起来相当劣质 下面是我的更新线程中的相关代码 public void run() { Canvas c; while (isRunning){ c = null; try { c = cellMap.getHolder().lockCanvas(null); synchronized (cellMap.getHol

我有一张画在画布上的等距图。当我试着移动地图时,这些黑线在一些瓷砖之间闪烁,使整个东西看起来相当劣质

下面是我的更新线程中的相关代码

public void run() {
    Canvas c;
    while (isRunning){
        c = null;
        try {
            c = cellMap.getHolder().lockCanvas(null);
            synchronized (cellMap.getHolder()) {
                cellMap.onDraw(c);
            }
        } finally {
            if( c != null){
                cellMap.getHolder().unlockCanvasAndPost(c);
            }
        }
    }
}
从我的
CellMap
类(扩展了SurfaceView)中:

public void onDraw(画布){
画布。drawColor(颜色。黑色);
int x=0;
int y=0;
对于(int i=0;i
mapCells[][]
是一个包含需要绘制的位图图像的对象数组。
draw()
函数仅为1行
canvas.drawBitmap(bitmapImage,x,y,null)


我发现删除Canvas.draw(Color.black)行
消除闪烁。但是,当我移动贴图时,画布没有被清除,因此我仍然可以看到前几帧中的图像。我可以想象这是因为绘制位图花费的时间太长?但是贴图目前只是非常小。

我发现了问题。移动贴图时,偏移值被更改。这导致使用不同的偏移值临时绘制地图的部分-创建撕裂。duh

我通过复制
onDraw()
方法开头的
xOffset
yOffset
值,并使用这些值进行更新,解决了这个问题

    public void onDraw(Canvas canvas){
    canvas.drawColor(Color.BLACK);
    int x = 0;
    int y = 0;

    for(int i = 0; i < mapSize; i++){
        for(int j = 0; j < mapSize; j++){
            x = (i-j) * 30 + xOffset;
            y = (i+j) * 15 + yOffset;
            mapCells[i][j].draw(canvas, paint, x, y);
        }
    }