如何修复Android中的图像绑定?

如何修复Android中的图像绑定?,android,canvas,bitmap,rendering,render,Android,Canvas,Bitmap,Rendering,Render,我试着在我能找到的任何地方设置抖动。我还试着把一切设置为ARGB_8888,但我的背景图像上仍然有非常糟糕的条纹。我的背景图像是640x960,在720x1280的物理手机上可以正常工作,但在320x480的模拟器上,我的色带不好。我把代码放在下面。如果您有任何建议,请帮助 public void onCreate(Bundle instanceBundle) { super.onCreate(instanceBundle); Window window = getWindow

我试着在我能找到的任何地方设置抖动。我还试着把一切设置为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);                
        }
    }        
}
public void onCreate(Bundle instanceBundle){
super.onCreate(instanceBundle);
Window=getWindow();
设置标志(WindowManager.LayoutParams.FLAG_全屏,WindowManager.LayoutParams.FLAG_全屏);
window.setFormat(PixelFormat.RGBA_8888);
surfaceView=新的surfaceView(本);
setContentView(surfaceView);
surfaceHolder=surfaceView.getHolder();
setFormat(PixelFormat.RGBA_8888);
Display Display=getWindowManager().getDefaultDisplay();
int width=display.getWidth();
int height=display.getHeight();
如果(宽度>高度){
设置偏移屏幕表面(高度、宽度);
}否则{
设置偏移屏幕表面(宽度、高度);
}
surfaceView.setFocusableInTouchMode(真);
surfaceView.requestFocus();
surfaceView.setOnKeyListener(此);
background=decodeSampledBitmapFromResource(getResources(),R.drawable.background);
}
公共位图解码SampleDbitMapFromResource(资源res,int resi)
{
//使用INJUSTDECBOUNDS首次解码=true检查尺寸
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码资源(res、resId、options);
//计算样本大小
options.inSampleSize=计算样本大小(选项);
//使用inSampleSize集合解码位图
options.inJustDecodeBounds=false;
options.inDither=true;
options.inPreferredConfig=Bitmap.Config.ARGB_8888;
返回BitmapFactory.decodeResource(res、resId、options);
}
public int calculateInSampleSize(BitmapFactory.Options)
{
//图像的原始高度和宽度
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
int reqWidth=数学圆(宽度*垂直比例);
内部要求高度=数学圆(高度*水平度);
如果(高度>要求高度| |宽度>要求宽度)
{
//计算高度和宽度与所需高度和宽度的比率
最终内部高度比=数学圆((浮动)高度/(浮动)要求高度);
最终整数宽度比=数学圆((浮动)宽度/(浮动)宽度);
//选择最小比率作为采样值,这将保证
//最终图像的两个尺寸均大于或等于
//要求的高度和宽度。
inSampleSize=高度比<宽度比?高度比:宽度比;
}
返回样本大小;
}
@凌驾
公共空间和附加的Towindow(){
super.onAttachedToWindow();
Window=getWindow();
setFormat(PixelFormat.RGBA_8888);
}
公共无效设置偏移屏幕表面(内部宽度、内部高度){
如果(offscreenSurface!=null)offscreenSurface.recycle();
offscreenSurface=Bitmap.createBitmap(宽度、高度、配置ARGB_8888);
画布=新画布(屏幕外表面);
}
油漆=新油漆();
公共空白绘图位图(位图位图、浮点x、浮点y)
{ 
if(canvas!=null)
{
int pixelX=(int)(x*getFramebufferWidth());
int像素=(int)(y*getFramebufferHeight());
绘制。设置抖动(真);
绘制位图(位图、像素X、像素Y、绘制);
}
}
公开募捐{
int帧=0;
long startTime=System.nanoTime();
long lastTime=System.nanoTime();
while(true){
if(state==state.Running)
{
如果(!surfaceHolder.getSurface().isValid())
持续
Canvas Canvas=surfaceHolder.lockCanvas();
long currTime=System.nanoTime();
浮动增量=(当前时间-上次时间)/100000000.0f;
如果(增量时间>0.1f)
deltaTime=0.1f;
clearFramebuffer(颜色为黑色);
drawBitmap(背景,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(画布);
}
}        
}

我看不到您的代码中有任何地方试图设置抗锯齿。你也可以用画布上的颜料来做。这应该会有帮助,如果不是的话,也许你正在使用的设备很差劲P

编辑:


我发誓你可以通过bitmapfactory设置抗锯齿,但我猜不行。不过,你绝对可以用油漆来做。您可以使用paint.setAntiAlias(aa)方法进行设置。

我在BitmapFactory.Options中看不到抗锯齿选项。你知道变量名吗?我确实尝试过油漆抗锯齿,这很有帮助,但看起来仍然很糟糕:(如果你担心仿真器上的绑定问题,那么如果可能的话,也许你应该在一个真实的设备上尝试。这就是仿真器的大小。可能只是仿真器破坏了图像。如果你无法访问较小的设备,那么有大量的测试站点允许你在真实的设备上远程发布你的应用程序,你可以设置一个免费帐户,并使用它来测试一次免费。