小编Law*_*tic的帖子

显示保留纵横比和裁剪边的 Android 初始屏幕

我想在应用程序启动后立即显示启动屏幕,并且希望它保留其原始的宽高比,同时尽可能地适合屏幕,裁剪侧面。让我用数字举一个例子。假设启动屏幕图像为 2048x1536(4x3 比例)。当它在 1280x800 屏幕 (16x10) 的设备上显示时,它应该缩小到 1280x960 并在顶部和底部裁剪 80px。

我的 SplashScreenActivity 如下所示:

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, GameActivity.class);
        startActivity(intent);
        finish();
    }
}
Run Code Online (Sandbox Code Playgroud)

样式.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="SplashTheme" parent="Theme.AppCompat.Light">
    <item name="android:windowBackground">@layout/splash</item>
</style>

</resources>
Run Code Online (Sandbox Code Playgroud)

我发现 ImageView 与 android:scaleType="centerCrop" 可能会成功。但如果layout/splash.xml是这样完成的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

  <ImageView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:scaleType="centerCrop"
      android:src="@drawable/splash_bg_portrait" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

该应用程序在启动时崩溃。像这样使用ImageView是完全不可能的还是某些参数错误?

另外要注意的是,我没有使用 setContentView(R.layout.splash) 也没有故意延迟启动画面 Activity,因为这个启动画面的目的不是为了增加玩家的等待时间,而是为了给玩家应用程序启动时,您可以看到一些东西,而不是空白屏幕。

UPD。 日志猫:

05-27 12:07:42.949   654   673 W …
Run Code Online (Sandbox Code Playgroud)

xml android splash-screen imageview

5
推荐指数
0
解决办法
1958
查看次数

标签 统计

android ×1

imageview ×1

splash-screen ×1

xml ×1