【问题标题】:Android Splash screen does not displayAndroid 启动画面不显示
【发布时间】:2016-12-20 07:30:06
【问题描述】:

我的应用程序中的启动画面不显示。仅显示白色背景。然后转到下一页。我在stackoverflow中看到了其他类似的问题,但它对我没有帮助。 飞溅.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash">

</RelativeLayout>

代码:

public class Splash extends AppCompatActivity {
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        Handler handler=new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent=new Intent(Splash.this,MainActivity.class);
                startActivity(intent);
                Splash.this.finish();
            }
        },2000);

    }
}

Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jobinsabu.ohxee">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Splash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"></activity>
    </application>

</manifest>

【问题讨论】:

  • 发布您的Manifest.xml
  • 好的。现在就做
  • 包括清单。请查看
  • 我认为您正在尝试加载大尺寸图像。 @drawable/飞溅。尝试一些其他图像而不是启动图像
  • 好的。我现在试试

标签: android splash-screen


【解决方案1】:

当大尺寸图像时,您应该对图像进行下采样。根据您的需要更改 inSampleSize 变量值。增加值,会降低图像分辨率,反之亦然

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                     int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
  }

private static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

调用上面的方法,如下代码:

  decodeSampledBitmapFromResource(getResources(), R.drawable.splash, 400, 400);

只是,在你的通用助手类中添加上述方法并调用它

【讨论】:

    【解决方案2】:

    问题是我在创建启动画面时遇到的问题。您在活动的 onCreate 方法中启动处理程序。

    据安卓开发者site:

    当你创建一个新的 Handler 时,它会绑定到正在创建它的线程的线程/消息队列

    因此,您正在主线程上创建一个处理程序,该处理程序也负责 UI。该线程准备您的 UI(在这种情况下似乎需要 2 秒多一点),然后调用处理程序中的 Runnable,该处理程序立即移动到另一个活动,因此您的启动画面不会显示。

    那你能做什么?显示视图后启动计时器!!!

    这就是你可以做到的(可能不是唯一的方法,但已经足够好了):

    1- 在 XML 中为 RelativeLayout 设置一个 id:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/background"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splash">
    </RelativeLayout>
    

    2- 在你的 onCreate 方法中这样做:

    public class Splash extends AppCompatActivity {
        @Override
        protected void onCreate( Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
            View background = findViewById(R.id.background); // or use Butterknife or DataBinding for ease of use
            final Handler handler=new Handler();
            background.post(new Runnable() {
                @Override
                public void run() {
                handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent=new Intent(Splash.this,MainActivity.class);
                    startActivity(intent);
                    Splash.this.finish();
                }
            },2000);
                }
            });
        }
    }
    

    通过这种方式,您将消息发布到视图消息队列,并且在将视图放在屏幕上之后,它会根据需要注册一个处理程序。希望对您有所帮助。

    【讨论】:

      【解决方案3】:

      在这里,我试图解决你的问题

      splash_screen_activity.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"
          android:orientation="vertical"
          android:background="@drawable/splashscreen">
      
      </LinearLayout>
      

      SplashScreenActivity.java

      public class SplashScreenActivity extends AppCompatActivity {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.splash_screen_activity);
      
              Thread background = new Thread() {
                  public void run() {
      
                      try {
                          // Thread will sleep for 5 seconds
                          sleep(5*1000);
      
                          // After 5 seconds redirect to another intent
                          Intent i=new Intent(getBaseContext(),MainActivity.class);
                          startActivity(i);
      
                          //Remove activity
                          finish();
      
                      } catch (Exception e) {
      
                      }
                  }
              };
      
              // start thread
              background.start();
          }
      }
      

      ma​​nifest.xml

      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.pantrykart">
      
          <application
              android:allowBackup="true"
              android:icon="@mipmap/ic_launcher"
              android:label="@string/app_name"
              android:supportsRtl="true"
              android:theme="@style/AppTheme">
              <activity android:name=".SplashScreenActivity">
                  <intent-filter>
                      <action android:name="android.intent.action.MAIN" />
      
                      <category android:name="android.intent.category.LAUNCHER" />
                  </intent-filter>
              </activity>
              <activity android:name=".MainActivity"></activity>
      
          </application>
      
      </manifest>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-17
        • 2018-12-07
        • 2014-01-22
        • 2023-03-30
        • 1970-01-01
        • 1970-01-01
        • 2014-09-20
        相关资源
        最近更新 更多