Android的定制SurfaceView改变大小大小、Android、SurfaceView

2023-09-07 14:36:54 作者:烟的寂寞

我试图创建一个2D游戏引擎Android应用程序。我已经按照本教程,它工作正常创建全屏显示,但我不希望出现这种情况。我想我的观点把屏幕上方2/3(或其他),并填写倒数第三与标准Android窗口小部件(按钮,文本输入等)。我不能得到这个工作。最好我能得到的是一个空白屏幕。我试过很多排列组合,包括使用外部LinerLayout,然后嵌入一个嵌套RelativeLayout的内部定制SurfaceView,并把Android部件在嵌套的LinearLayout,而这是行不通的。

I'm trying to create a 2D game engine for an Android app. I've followed this tutorial, which works fine for creating a full screen display, but I don't want that. I want to make my view take the top 2/3 (or whatever) of the screen, and fill the bottom third with standard Android widgets (buttons, text entry, etc.). I cannot get this to work. The best I can get is a blank white screen. I've tried many permutations, including using an outer LinerLayout, then embedding the custom SurfaceView inside a nested RelativeLayout, and putting the Android widgets in a nested LinearLayout, and it doesn't work.

例如,这将产生一个白色的屏幕,当我觉得应该是50%SurfaceView,50%为一个TextView:

For instance, this produces a white screen, when I feel like it should be 50% SurfaceView, 50% for a TextView:

activity_main.xml:

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" >

<RelativeLayout
    android:layout_width="0dip"
    android:layout_height="0dp"
    android:layout_weight="1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapContainer"
    >
</RelativeLayout> 

<LinearLayout
    android:layout_width="0dip" 
    android:layout_weight="1"
    android:layout_height="0dp"
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/white"
        android:text="@string/test_str" />
</LinearLayout>

</LinearLayout>

MainActivity.java:

MainActivity.java:

package com.removed.for.privacy;

import com.removed.for.privacy;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout mapContainer = (RelativeLayout) findViewById(R.id.mapContainer);

        JSMapView mapView = new JSMapView(this);
        mapContainer.addView(mapView);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

任何想法?

推荐答案

想通了通过大量的谷歌搜索和半相关的问题。下面是我如何得到它填补2/3肖像模式(100%的宽度)的高度和宽度的2/3(100%的高度)的景观的一个例子。

Figured it out through lots of googling and semi-related questions. Here's an example of how I got it to fill 2/3 the height in portrait mode (100% width), and 2/3 width (100% height) in landscape.

在自定义表面来看,覆盖onLayout方式:

In the custom surface view, override the onLayout method:

@Override
public void onLayout(boolean changed, int left, int top, int right, int bottom) {
    if (changed) {
        (this).layout(0, 0, viewWidth, viewHeight);
    }
}

在类的构造函数,添加此code:

In the class constructor, add this code:

public YourCustomSurfaceView(Context context) {
    super(context);


    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    int rotation = display.getRotation();

    // If vertical, we fill 2/3 the height and all the width. If horizontal,
    // fill the entire height and 2/3 the width
    if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) {
        screenWidth = display.getWidth();
        screenHeight = display.getHeight();
        viewHeight = 2 *  (screenHeight / 3);
        viewWidth = screenWidth;
    } else {
        screenWidth = display.getWidth();
        screenHeight = display.getHeight();
        viewWidth = 2 * (screenWidth / 3);
        viewHeight = screenHeight;
    }
    // Enter rest of code here
}