【问题标题】:android - progress bar on splash screenandroid - 启动画面上的进度条
【发布时间】:2017-02-03 05:07:28
【问题描述】:

我的应用在加载时会显示启动画面。我想在初始屏幕上的图标下方放置一个动画进度条。我尝试使用 XML,但它崩溃了!说无效的标签进度条。

这是我在 styles.xml 中调用初始屏幕的代码

<style name="AppTheme.BrandedLaunch" parent="AppThemeDark">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <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
    android:drawable="@color/splash_screen_bg"/>

<item>
    <bitmap
        android:gravity="center"
        android:tileMode="disabled"
        android:src="@drawable/ic_launcher"/>
</item>

<item>
<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
/>
</item>
</layer-list>

我真的不想使用其他方法制作启动画面,因为这种方法非常简单。有什么想法吗?

【问题讨论】:

  • 为什么不把图标和进度条直接放在splash.xml文件中

标签: java android xml progress-bar splash-screen


【解决方案1】:

关键在本教程中: http://www.logicchip.com/android-splash-screen-progress-bar/ 您必须在可绘制文件夹中创建 un 文件:示例 splas_screen.xml

    <?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp">
    <item>
        <color android:color="@color/main_color_grey_50"/>
    </item>
    <item>
        <bitmap
            android:gravity="center"
            android:filter="true"
            android:src="@drawable/logomies"
            />
    </item>
</layer-list>

将新样式添加到文件styles.xml

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

AndroidManifest 文件,您应该像 LAUNCHER Activity 一样设置 SplashActivity。

<application
        android:name=".ActBenApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/ActivityTheme">

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

重要的部分来了:你必须创建一个布局,进度条所在的位置,这个布局没有背景,所以进度条出现在斜线的前面。

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/splash_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".main.ui.SplashActivity">


    <ProgressBar
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyleHorizontal"
        android:visibility="visible"
        android:indeterminateOnly="true"/>
</android.support.constraint.ConstraintLayout>

最后在你的 Activity.java 文件中:

public class SplashActivity extends AppCompatActivity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);


        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Intent i = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, 3000);
    }
}

设置为 setContentView 到创建的布局。就是这样......!

【讨论】:

  • 那些闪屏只会浪费用户时间。什么都没有加载时显示愚蠢的进度条
【解决方案2】:

不要将progressbar和imageview(bitmap)之类的widget放在drawable文件中,而是添加到你的布局文件中,其中父布局应该是相对布局,然后将relativelayout背景颜色设置为splash_screen_bg并在里面添加imageview和progressbar相对布局。

【讨论】:

    【解决方案3】:

    这是一个技巧。

    SplashActivity.java

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            final View rootView = getWindow().getDecorView().getRootView();
            rootView.setBackgroundDrawable(getResources().getDrawable(R.drawable.test));
            rootView.post(new Runnable() {
                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void run() {
                    ((AnimationDrawable) rootView.getBackground()).start();
                }
            });
        }
    

    test.xml

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/list"
            android:oneshot="false">
            <item
                android:drawable="@drawable/frame_1"
                android:duration="20" />
            <item
                android:drawable="@drawable/frame_2"
                android:duration="20" />
            <item
                android:drawable="@drawable/frame_3"
                android:duration="20" />
            <item
                android:drawable="@drawable/frame_4"
                android:duration="20" />
            <item
                android:drawable="@drawable/frame_5"
                android:duration="20" />
            <item
                android:drawable="@drawable/frame_6"
                android:duration="20" />
            <item
                android:drawable="@drawable/frame_7"
                android:duration="20" />
            <item
                android:drawable="@drawable/frame_8"
                android:duration="20" />
            <item
                android:drawable="@drawable/frame_9"
                android:duration="20" />
    
        </animation-list>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-26
      • 2010-11-26
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-10
      相关资源
      最近更新 更多