显示android启动画面,保留纵横比和裁剪边

时间:2016-05-26 12:33:06

标签: android xml imageview splash

我想在应用程序启动后立即显示启动画面,我希望它保持其原始宽高比,同时尽可能地适合屏幕,裁剪两侧。让我举个数字的例子。假设启动画面图像为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();
    }
}

styles.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>

我发现使用android:scaleType =“centerCrop”的ImageView可能会成功。但是如果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>

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

另请注意,我不使用setContentView(R.layout.splash)并且不会故意延迟启动活动,因为此启动画面的目的不是为播放器添加等待时间,而是在应用程序启动时,给玩家一些东西,而不是空白的屏幕。

UPD。 logcat的:

05-27 12:07:42.949   654   673 W WindowManager: Token{423c44b8 ActivityRecord{4225d620 u0 com.myapp.jq.google/com.myapp.jq.SplashActivity t15}} failed creating starting window
05-27 12:07:42.949   654   673 W WindowManager: android.content.res.Resources$NotFoundException: File res/layout/splash.xml from drawable resource ID #0x7f03002d
05-27 12:07:42.949   654   673 W WindowManager:     at android.content.res.Resources.loadDrawable(Resources.java:2152)
05-27 12:07:42.949   654   673 W WindowManager:     at android.content.res.Resources.getDrawable(Resources.java:710)
05-27 12:07:42.949   654   673 W WindowManager:     at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:3109)
05-27 12:07:42.949   654   673 W WindowManager:     at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3152)
05-27 12:07:42.949   654   673 W WindowManager:     at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:1718)
05-27 12:07:42.949   654   673 W WindowManager:     at com.android.internal.policy.impl.PhoneWindowManager.addStartingWindow(PhoneWindowManager.java:1850)
05-27 12:07:42.949   654   673 W WindowManager:     at com.android.server.wm.WindowManagerService$H.handleMessage(WindowManagerService.java:7825)
05-27 12:07:42.949   654   673 W WindowManager:     at android.os.Handler.dispatchMessage(Handler.java:110)
05-27 12:07:42.949   654   673 W WindowManager:     at android.os.Looper.loop(Looper.java:193)
05-27 12:07:42.949   654   673 W WindowManager:     at android.os.HandlerThread.run(HandlerThread.java:61)
05-27 12:07:42.949   654   673 W WindowManager: Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag LinearLayout
05-27 12:07:42.949   654   673 W WindowManager:     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:933)
05-27 12:07:42.949   654   673 W WindowManager:     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
05-27 12:07:42.949   654   673 W WindowManager:     at android.content.res.Resources.loadDrawable(Resources.java:2148)
05-27 12:07:42.949   654   673 W WindowManager:     ... 9 more

从这看起来似乎LinearLayout(或任何其他布局)不能用于android:windowBackground。 splash.xml中的layer-list工作正常:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
      <bitmap
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:src="@drawable/splash_bg_portrait"/>
    </item>

</layer-list>

但不幸的是,layer-list不支持ImageView,而bitmap既不支持android:scaleType,也没有类似的功能。

0 个答案:

没有答案