Android Flash屏幕加载白屏

时间:2016-10-30 16:32:24

标签: android android-layout material-design

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

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

1 个答案:

答案 0 :(得分: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>

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