【问题标题】:Change size of Android custom SurfaceView更改 Android 自定义 SurfaceView 的大小
【发布时间】:2012-08-07 20:06:53
【问题描述】:

我正在尝试为 Android 应用创建 2D 游戏引擎。我关注了this tutorial,它可以很好地创建全屏显示,但我不希望这样。我想让我的视图占据屏幕的顶部 2/3(或其他),并用标准的 Android 小部件(按钮、文本输入等)填充底部的三分之一。我不能让它工作。我能得到的最好的结果是一个空白屏幕。我尝试了很多排列,包括使用外部 LinerLayout,然后将自定义 SurfaceView 嵌入到嵌套的 RelativeLayout 中,并将 Android 小部件放入嵌套的 LinearLayout 中,但它不起作用。

例如,这会产生一个白屏,当我觉得它应该是 SurfaceView 的 50%,TextView 的 50% 时:

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:

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;
    }
}

有什么想法吗?

【问题讨论】:

    标签: android 2d surfaceview surfaceholder


    【解决方案1】:

    通过大量谷歌搜索和半相关问题弄清楚了。这是我如何让它在纵向模式下填充 2/3 高度(100% 宽度)和在横向模式下填充 2/3 宽度(100% 高度)的示例。

    在自定义表面视图中,覆盖onLayout方法:

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

    在类构造函数中,添加以下代码:

    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
    }
    

    【讨论】:

      【解决方案2】:

      拥有自定义 SurfaceView 大小 surfaceView!!.layoutParams = ConstraintLayout.LayoutParams(width, height)

      ConstraintLayout 取决于你的父布局

      【讨论】:

        【解决方案3】:

        现在,constraintLayout 出来了。可以通过将“layout_constraintHeight_percent”设置为“0.5”来做到这一点。 surfaceView 如下所示:

        <SurfaceView
            android:id="@+id/surface_camera"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintHeight_percent="0.5"
        />
        

        【讨论】:

          【解决方案4】:

          要更改 SurfaceView 的自定义高度和宽度,请使用“android.view.ViewGroup.LayoutParams” ViewGroup 的不同子类有 LayoutParams 的子类。例如,AbsoluteLayout 有自己的 LayoutParams 子类,它添加了 X 和 Y 值。通过这个你可以改变 SurfaceView 的 X 和 Y

          更改surfaceview高度宽度的代码在这里: http://agalaxycode.blogspot.in/2014/12/change-surfaceview-height-width.html

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-05-15
            • 1970-01-01
            • 1970-01-01
            • 2013-05-30
            • 2016-03-30
            • 2014-02-12
            相关资源
            最近更新 更多