在SurfaceView上向下滾動屏幕位圖向下一行不起作用。在SurfaceView上向下滾動屏幕位圖,向下一行不起作用
請嘗試下面的示例代碼來證明Android Java圖形位圖系統中某些東西似乎已損壞,或者(更可能)有一些關於此操作的信息,我不明白,當滾動屏幕外位圖並在屏幕上繪製SurfaceView畫布時使用drawBitmap。
運行代碼一次,看看它是如何塗抹筆畫的,只有在圓形填充的時候纔會在頁面上向下循環。
然後將Y偏移量從1更改爲-1並重新運行代碼,並向上滾動一行(很難看,因爲它不是動畫)。
package com.example.SurfaceViewTest;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class SurfaceViewTest extends Activity implements SurfaceHolder.Callback {
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private int intSurfaceWidth, intSurfaceHeight;
Bitmap bmpOffScreen;
Canvas cnvOffScreen = new Canvas();
Canvas c = new Canvas();
private static Paint paint = new Paint();
private static final String TAG = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSurfaceView = (SurfaceView) this.findViewById(R.id.Surface);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
paint.setColor(0xFFFFFFFF);
paint.setStyle(Style.STROKE);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
intSurfaceWidth = width;
intSurfaceHeight = height;
bmpOffScreen = Bitmap.createBitmap(intSurfaceWidth, intSurfaceHeight, Bitmap.Config.ARGB_8888);
cnvOffScreen = new Canvas(bmpOffScreen);
cnvOffScreen.drawCircle(intSurfaceWidth/2, intSurfaceHeight/2, 100, paint);
c = null;
c = mSurfaceHolder.lockCanvas(null);
if (c == null) {
Log.e(TAG, "Canvas bu-huu");
} else {
// Scroll off screen canvas and hopefully underlying bitmap bitmap
Rect src = new Rect(0, 0, intSurfaceWidth, intSurfaceHeight);
Rect dst = new Rect(0, 0, intSurfaceWidth, intSurfaceHeight);
dst.offset(0, 1);
cnvOffScreen.drawBitmap(bmpOffScreen, src, dst, null);
// This produces exact same smear, but works fine scrolling up!?!
cnvOffScreen.translate(0,1);
cnvOffScreen.drawBitmap(bmpOffScreen, 0, 0, paint);
c.drawBitmap(bmpOffScreen, 0, 0, paint);
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {}
}
增添了這裏的解決方案:http://gamedev.stackexchange.com/questions/22070/how-to-update-off-screen-bitmap-in-a -surfaceview-thread – 2012-01-13 22:52:39