我试过在我能找到的任何地方设置抖动。我也尝试将所有内容设置为ARGB_8888,并且STILL在我的背景图像上存在非常糟糕的条带。我的背景图像是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);
}
}
}
答案 0 :(得分:0)
我在代码中的任何地方都没有看到您尝试设置抗锯齿的地方。您也可以使用画布中使用的颜料来完成此操作。应该有帮助,如果不是,你使用的设备可能很糟糕? :P
编辑:
我发誓你可以通过bitmapfactory设置抗锯齿,但我猜不是。你绝对可以从油漆中做到这一点。您可以使用paint.setAntiAlias(aa)方法设置它。