【问题标题】:Android Flash screen loading white screenAndroid Flash 屏幕加载白屏
【发布时间】:2016-10-30 16:32:23
【问题描述】:

我的主要活动中有以下代码

public class MainActivity extends AppCompatActivity {



    ImageView my_flash_screen;
    DB db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        my_flash_screen= (ImageView) findViewById(R.id.my_flash_screen);



        MainTask task = new MainTask();
        task.execute();

       AnimationSet animation = new AnimationSet(true);
        animation.addAnimation(new AlphaAnimation(0.0F, 1.0F));
        animation.addAnimation(new ScaleAnimation(0.0f, 1, 0.0f, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)); // Change args as desired
        animation.setDuration(1500);

        my_flash_screen.startAnimation(animation);

        new CountDownTimer(5000, 1000) {

            public void onTick(long millisUntilFinished) {

            }

            public void onFinish() {


                Intent i = new Intent(MainActivity.this, NavigationActivity.class);
                startActivity(i);
                finish();
            }
        }.start();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    private class MainTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {

            if(FontUtil.storeSharedPreference(getApplicationContext()).getString("font", "").isEmpty())
            {
                FontUtil.savePrefs("font", "SKN.TTF",getApplicationContext());
                FontUtil.savesize("fb",22, getApplicationContext());

                Log.d("Test","insidefont");
            }

            try {
                if(db==null){
                    db=new DB(getApplicationContext());
                    db.createdatabase();
                    Log.d("Test","intialise first");
                }
                Log.d("Test","intialise second");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
        }
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="@drawable/mynew"
    android:gravity="center"
    android:layout_weight="1"
    android:orientation="vertical">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/mynew"

        android:id="@+id/my_flash_screen"
        android:contentDescription="loading image" />
</LinearLayout>

有时当我打开我的应用程序时,只会显示白屏。我使用 AsyncTask 将数据库从资产文件夹复制到设备,共享首选项将设置字体类型和大小。谁能帮我解决这个问题。谢谢.

在提问之前,我已经阅读了以下问题

White background when Android app start up

white background for few seconds before splash screen android

Splash screen activity background color

【问题讨论】:

    标签: android android-layout material-design


    【解决方案1】:

    是的,因为它无法立即识别您的图像。这需要一些时间。到那时它显示白屏。

    我会告诉你如何使用和避免这个问题。

    将您的图像设置为样式并将其用作主题。

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorText</item>
    </style>
    
    <!--SplashActivity theme.-->
    
    <style name="Theme.Splash" parent="AppTheme">
        <item name="android:windowBackground">@drawable/ic_splash</item>
    </style>
    

    并使用你的xml只是空白

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:orientation="vertical">
    </LinearLayout>
    

    然后在您的启动活动中使用该主题。

    <activity
            android:name=".module.splash"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.Splash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
    

    第二个选项:

    我的第一种方法是完美的,但您想使用一些动画或其他东西然后您使用这种样式并使用您在布局中设置图像的相同布局。

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    
        <item name="colorPrimary">@color/kitean_primary_color</item>
        <item name="colorPrimaryDark">@color/kitean_primary_dark</item>
        <item name="colorAccent">@color/kitean_color_accent</item>
    </style>
    

    这是避免此问题的最佳方法。谢谢

    【讨论】:

    • @Saveen.Thanks.我会尝试
    • @Saveen.如何在片段变化时释放内存
    • 。我在通过动画加载图像之前使用两个背景。你可以看到 xml
    • 但我稍后会改变我的意思是我使用不同的图像
    猜你喜欢
    • 2015-06-21
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 2011-07-15
    相关资源
    最近更新 更多