Android 设备上文件的应用启动画面

Android app splash screen from file on device

我想在应用程序从设备上的 PNG 文件启动时显示启动画面 x 秒。

我已经在主题中尝试 android:windowBackground 但是不能从文件中获取并且只能预定义 Drawable

该文件可能随时更改,因此在下次应用启动时会有所不同。

您可以使用ImageView。 设置缩放图像以适合屏幕。 使 window 全屏,没有标题栏。通过这个你可以每次都设置不同的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>

通过..

在启动画面activity 中访问此 ImageView
ImageView img = (ImageView) findviewbyId(R.id.imageView);
Bitmap bitmapImage = BitmapFactory.decodeFile(imagePathFromSDCard);
    Drawable drawableImage = new BitmapDrawable(bitmapImage);

    img.setBackgroundDrawable(drawableImage);

下面是设置启动画面计时器的代码activity。另外,不要忘记在清单中包含 activity。

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.

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>