2016-01-25 33 views
-2

我想在啓動應用時從設備上的PNG文件啓動啓動屏幕x秒。設備上文件的Android應用啓動屏幕

我已經試過的android:windowBackground然而,在主題不能從一個文件中獲取,只有預定義的繪製對象

該文件可能隨時改變,因此在明年推出的應用程序會有所不同。

+0

設置的ImageView或恰好等於屏幕大小在活動佈局視圖並在活動中爲其設置背景圖片。 – Sharj

回答

1

下面是設置開機畫面活動定時器的代碼。另外,不要忘記將活動包含在清單中。

splash_screen.java

public class Splash extends Activity {   
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.splash_screen); 

      Thread timerThread = new Thread(){ 
       public void run(){ 
        try{ 
         sleep(5000); //Change the timing of the Screen 
        }catch(InterruptedException e){ 
         e.printStackTrace(); 
        }finally{ 
         Intent intent = new Intent(SplashScreen.this,MainActivity.class); 
         startActivity(intent); 
        } 
       } 
      }; 
      timerThread.start(); 
     } 
    } 

讓你的佈局,全屏,刪除操作欄。下面是splash_screen.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:background="@drawable/splashscreen" 
android:orientation="vertical"> 

</LinearLayout> 

更改啓動畫面的圖像的代碼,當你需要,你的情況,每次應用程序啓動。 HTH。

+0

儘管沒有明確解釋如何將新文件讀入可繪製對象,但這對於啓動屏幕來說是很好的代碼。 – user2206836

+0

我不明白你的問題。將'android:background =「@ drawable/splashscreen」'的源更改爲您的可繪製資源,並且它也會在啓動畫面中更改。 –

0

您可以使用ImageView。 設置縮放圖像以適應屏幕。 使窗口全屏,沒有標題欄。通過這個,你可以設置不同的drawable每次。

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

<ImageView 
      android:id="@+id/imageView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/image2" 
      /> 
</LinearLayout> 

訪問這個ImageView的在閃屏活動由..

ImageView img = (ImageView) findviewbyId(R.id.imageView); 
Bitmap bitmapImage = BitmapFactory.decodeFile(imagePathFromSDCard); 
    Drawable drawableImage = new BitmapDrawable(bitmapImage); 

    img.setBackgroundDrawable(drawableImage); 
+0

Stackoverflow上的答案通常包含一些更多的細節,如代碼示例,...也許你可以提供它。這將使這個答案肯定更有用。 – Trilarion

0

SplashScreen.java

public class SplashScreen extends Activity { 
    // Splash screen timer 
    private static int SPLASH_TIME_OUT = 3000; 
    public static File app_dir; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splash_screen); 
     ContextWrapper c = new ContextWrapper(this); 
     app_dir = c.getFilesDir(); 
     try { 
      String pathName = app_dir + "/background.png"; 
      File f = new File(pathName); 
      Drawable d = Drawable.createFromPath(pathName); 
      RelativeLayout v = (RelativeLayout) findViewById(R.id.splash_image); 
      Bitmap myBitmap = BitmapFactory.decodeFile(f.getAbsolutePath()); 
      v.setVisibility(View.VISIBLE); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 


     new Handler().postDelayed(new Runnable() { 

      /* 
      * Showing splash screen with a timer. This will be useful when you 
      * want to show case your app logo/company 
      */ 

      @Override 
      public void run() { 
       Intent i = new Intent(SplashScreen.this, Activity_MainActivity.class); 
       startActivity(i); 
       finish(); 
      } 
     }, SPLASH_TIME_OUT); 
    } 

} 

,然後在我的清單

<activity 
    android:name="com.package.name.SplashScreen" 
    android:label="@string/app_name" 
    android:screenOrientation="landscape" 
    android:theme="@android:style/Theme.Black.NoTitleBar" > 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

splash_screen.xml

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

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:id="@+id/splash_image2" /> 

</RelativeLayout> 
相關問題