2016-12-20 104 views
3

我的應用程序中的啓動畫面不顯示。僅顯示白色背景。然後它進入下一頁。我已經看到了其他類似的問題,但它並沒有幫助我。 splash.xml:Android啓動畫面不顯示

<?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> 
+0

發表您的'Manifest.xml' –

+0

ok.will現在就做 – jobin

+0

清單included.Please看看它 – jobin

回答

2

當大尺寸圖像,你應該下采樣圖像。根據需要更改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); 

只是,在共同的輔助類添加上述方法,並調用它

0

這個問題是我在創建啓動畫面時遇到的。您在該活動的onCreate方法中啓動處理程序。

據Android開發site

當你創建一個新的處理程序,它被綁定到線程的線程/消息隊列,創建它

所以要創建主線程上的處理程序也負責UI。這個線程準備你的用戶界面(在這種情況下似乎需要2秒多一點),然後調用你的處理程序中的Runnable,它立即移動到另一個活動,因此你的飛濺不會顯示出來。

那麼你能做什麼?在您的視圖顯示後啓動計時器!

這是你如何能做到這(可能不是唯一的方法,但不夠好):

1-在XML設置一個ID爲RelativeLayout的:

<?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); 
      } 
     }); 
    } 
} 

這樣你發佈消息視圖消息隊列和視圖放在屏幕上後,再根據需要將其註冊的處理程序。希望能幫助到你。

0

在這裏,我試圖解決您的問題

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(); 
    } 
} 

的manifest.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>