2016-12-20 30 views
3

私のアプリのスプラッシュ画面が表示されません。白い背景のみが表示されます。それから、次のページに行きます。私はstackoverflowで他の同様の質問を見ましたが、それは私を助けませんでした。 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

は今 – 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を担当するメインスレッドのハンドラ。このスレッドはあなたのUIを準備します(この場合は2秒以上かかると思われます)。ハンドラ内のRunnableを呼び出し、すぐに別のアクティビティに移動します。スプラッシュは表示されません。

だから何をすることができますか?あなたのビューが表示された後にタイマーを開始してください!

これは(おそらくない唯一の方法は十分に良い)あなたがそれを行うことができる方法である:あなたのonCreateで

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

1- XMLでRelativeLayoutのためのIDを設定しますビューのメッセージキューにメッセージを投稿し、ビューが画面上に置かれた後、あなたが望むようにそれはハンドラを登録し

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> 
関連する問題