我试过在我能找到的任何地方设置抖动。我还尝试将所有内容都设置为 ARGB_8888,但我的背景图像上的条纹仍然很糟糕。我的背景图像是 640x960,它在我的 720x1280 物理手机上运行良好,但在以 320x480 运行的模拟器上,我得到了不好的色带。我把我的代码放在下面。如果您有任何建议,请帮助!
public void onCreate(Bundle instanceBundle) {
super.onCreate(instanceBundle);
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.setFormat( PixelFormat.RGBA_8888 );
surfaceView = new SurfaceView(this);
setContentView(surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.setFormat( PixelFormat.RGBA_8888 );
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
if(width > height) {
setOffscreenSurface(height, width);
} else {
setOffscreenSurface(width, height);
}
surfaceView.setFocusableInTouchMode(true);
surfaceView.requestFocus();
surfaceView.setOnKeyListener(this);
background = decodeSampledBitmapFromResource( getResources(), R.drawable.background );
}
public Bitmap decodeSampledBitmapFromResource( Resources res, int resId )
{
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options );
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
options.inDither = true;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
return BitmapFactory.decodeResource(res, resId, options);
}
public int calculateInSampleSize( BitmapFactory.Options options )
{
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
int reqWidth = Math.round( width * vertDispRatio );
int reqHeight = Math.round( height * horiDispRatio );
if( height > reqHeight || width > reqWidth )
{
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round( (float) height / (float) reqHeight );
final int widthRatio = Math.round( (float) width / (float) reqWidth );
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
public void setOffscreenSurface(int width, int height) {
if(offscreenSurface != null) offscreenSurface.recycle();
offscreenSurface = Bitmap.createBitmap(width, height, Config.ARGB_8888);
canvas = new Canvas(offscreenSurface);
}
Paint paint = new Paint();
public void drawBitmap(Bitmap bitmap, float x, float y)
{
if(canvas != null)
{
int pixelX = (int)( x * getFramebufferWidth() );
int pixelY = (int)( y * getFramebufferHeight() );
paint.setDither(true);
canvas.drawBitmap(bitmap, pixelX, pixelY, paint);
}
}
public void run() {
int frames = 0;
long startTime = System.nanoTime();
long lastTime = System.nanoTime();
while(true) {
if( state == State.Running )
{
if( !surfaceHolder.getSurface().isValid() )
continue;
Canvas canvas = surfaceHolder.lockCanvas();
long currTime = System.nanoTime();
float deltaTime = (currTime - lastTime) / 1000000000.0f;
if( deltaTime > 0.1f )
deltaTime = 0.1f;
clearFramebuffer( Color.BLACK );
drawBitmap( background, 0, 0 );
src.left = 0;
src.top = 0;
src.right = offscreenSurface.getWidth() - 1;
src.bottom = offscreenSurface.getHeight() - 1;
dst.left = 0;
dst.top = 0;
dst.right = surfaceView.getWidth();
dst.bottom = surfaceView.getHeight();
canvas.drawBitmap(offscreenSurface, src, dst, null);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
}