android - 如何使用图像和百分比位置创建 Android 启动画面

标签 android android-drawable splash-screen

我正在尝试创建一个包含两张图片的 Android 启动画面:一张位于屏幕中央,另一张距离屏幕底部约 20%。有什么办法吗?

到目前为止,我尝试的方法是将 SplashActivity 作为第一个 Activity , list 文件将主题设置为 SplashTheme:

在 AndroidManifest.xml 中:

<activity
        android:name=".SplashActivity"
        android:label="@string/app_name"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>

然后在 styles.xml 中我定义了这个主题:

    <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/background_splash</item>
    </style>

接下来我创建可绘制文件 background_splash.xml,这是我有问题的地方:

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

<item>
    <shape android:shape="rectangle">
        <solid android:color="@color/dark_gray" />
        <padding
            android:left="0dip"
            android:top="0dip"
            android:right="0dip"
            android:bottom="0dip" />
    </shape>
</item>

<item>
    <bitmap
        android:gravity="center"
        android:src="@mipmap/mainlogo"/>

</item>

<item android:bottom="@dimen/splash_bottom">
    <bitmap android:src="@mipmap/bottom_logo"
        android:gravity="bottom" />
</item>

</layer-list>

这实际上是一种工作方式,主 Logo 确实位于中心,而 bottom_logo 在底部上方有一段距离。问题是我需要在 dimens.xml 中为 splash_bottom 指定 dp 值。为 ldpi、mdpi、hdpi xhdpi 等准确地执行此操作真的很难做到正确。

在可绘制文件 background_splash.xml 中没有办法将 bottom_logo 放置在距离屏幕底部 20% 的位置吗?

所需的初始屏幕看起来像这样(不是实际屏幕 - 我从另一篇 SO 帖子中借来的 - 请参阅 Android: how to align 2 images on a splash screen ):

enter image description here

最佳答案

“layer-list & item”没有百分比属性来设置它们的位置。

要实现您的想法,您应该使用布局 XML 文件

在 AndroidManifest.xml 中

<activity
    android:name=".SplashActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

在 SplashActivity.java 中

public class SplashActivity extends Activity {

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

        // SET no title , full-screen mode
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // SET activity_splash.xml as layout
        final View viewSplash = View.inflate(this, R.layout.activity_splash, null);
        setContentView(viewSplash);

        // Gradient Animation
        AlphaAnimation anim = new AlphaAnimation(0.5f, 1.0f); // change alpha from 0.5 to 1.0
        anim.setDuration(5000); // animate in 5000ms
        viewSplash.setAnimation(anim);
        anim.setAnimationListener(
                new Animation.AnimationListener(){
                                    @Override
                                    public void onAnimationStart(Animation animation) {

                                    }

                                    @Override
                                    public void onAnimationRepeat(Animation animation) {

                                    }

                                    @Override
                                    public void onAnimationEnd(Animation animation) {
                                        // redirect to the other screen, such as MainActivity
                                        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                                        startActivity(intent);

                                        // close SplashActivity
                                        finish();
                                    }
                                });
    }
}

activity_splash.xml 可能是以下之一;

<强>1。 LinearLayout 的 layout_weight

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/dark_gray">

<ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@mipmap/mainlogo"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <View android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.4"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_weight="0.6"
        android:src="@mipmap/bottom_logo"/>
</LinearLayout>

used layout_weight of LinearLayout 截图.1

<强>2。指南的 layout_constraintGuide_percent

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/dark_gray">

    <ImageView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/mainlogo"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <android.support.constraint.Guideline
        android:id="@+id/bottom_guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintGuide_percent=".8"
        android:orientation="horizontal" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/bottom_logo"
        app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>

used layout_constraintGuide_percent of Guidline截图.2

<强>3。 layout_marginLeftPercent 的 PercentRelativeLayout

但是这个类在 API 级别 26.1.0 中被弃用了。所以我没有上传关于那个的 xml。

关于android - 如何使用图像和百分比位置创建 Android 启动画面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52088194/

相关文章:

android - 需要一个改变(SQLite)列数据类型的好方法

android - 如何在Paypal中实现拆分交易?

java - 以编程方式显示启动画面

react-native - 将导航启动画面 react 到登录屏幕

android - 如果应用程序正在运行,如何检查前台服务?

android - Android >=4.0 上的捕获媒体按钮(适用于 2.3)

android - 圆形图标图像模糊

Android矢量支持错误

android - Radiobutton drawable 对 state_checked 没有反应

java - 抽屉导航错误与闪屏