表面视图和缩放

时间:2015-01-29 09:17:09

标签: android surfaceview android-layout-weight

最重要的是,我为我的英语道歉。

我是Android应用程序编程的新手,我最近发现了Canvas和Bitmap,所以我决定制作一个只能使用横向的“游戏”。我也发现了SurfaceView以及如何在SurfaceView上实现背景,但首先是我的背景是图像496x288。

我正在网上搜索,了解我如何能够很好地填充我手机的所有屏幕背景和另一个屏幕。所以我找到了这段代码并尝试在我的应用中使用。

@SuppressLint("WrongCall")
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.fondo1escalado);
            float scale = (float) background.getHeight() / (float) getHeight();
            int newWidth = Math.round((background.getWidth() / scale));
            int newHeight = Math.round(background.getHeight() / scale);
            scaled = Bitmap.createScaledBitmap(background, newWidth, newHeight, true);
            gameLoopThread.setRunning(true);
            gameLoopThread.start();


}

@SuppressLint("WrongCall")
protected void onDraw(Canvas canvas) {

    canvas.drawColor(Color.BLUE);
    canvas.drawBitmap(scaled, 0,0, null);
}

它有效,但效果不佳。我明白了:

enter image description here(不要担心角色)

它可以很好地缩放,但不适合所有屏幕(参见蓝色背景)。

所以,就像一个优秀的程序员一样,我尝试使用我设计的尺寸为296x912的GUI来解决这个问题,我使用相同的代码,并且它在高度上很好地扫描,但在所有屏幕的宽度上都没有,甚至是片剂。

enter image description here

这是GUI的代码

float scalaInterfazAltura = (float) interfaz.getHeight() / (float) getHeight();
        int newHeightInterfaz = Math.round(interfaz.getHeight() / scalaInterfazAltura);
        int newWidthInterfaz = Math.round(interfaz.getWidth() / scalaInterfazAltura);
        scaledinterfaz = Bitmap.createScaledBitmap(interfaz, newWidthInterfaz, newHeightInterfaz, true);
        canvas.drawBitmap(scaledinterfaz, getWidth() - interfaz.getWidth()/2, 0, null);

所以,我的问题是:Surfaceview有一个可以很好地扩展的东西吗?我做错了什么?

谢谢。

1 个答案:

答案 0 :(得分:1)

发生这种情况会导致您的显示高度超过查看高度。 尝试使用surfaceView.getHeight()代替interfaz.getHeight()

您也可以使用

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView()之前的活动中,让您的游戏全屏显示。