Phonegap splash screen for android: not stretched or 9patch

user1198646 picture user1198646 · Feb 9, 2012 · Viewed 13.3k times · Source

I have a fake splash screen that pops up when my phonegap app is launched:

super.setIntegerProperty("splashscreen", R.drawable.splash);
super.loadUrl("file:///android_asset/www/index.html", 500);

I currently have 3 different splash.png files: hdpi, mdpdi and ldpi. My issue is that even in hdpi, you'll get devices that are 480 x 800 and others that are 480 x 854, and android stretches my splash image to fill the screen.

I would much rather have the image keep its aspect ratio. From what I've read, a 9-patch solution will not work with PhoneGap. If it will, please let me know how! I've got the .9.png ready to go. So the other solution I can think of is to prevent android from stretching my image. How would I do that?

Thanks!

Answer

robert picture robert · May 25, 2012

The 9 patch solution does work for splash screens in PhoneGap :-)

  1. Define a new drawable resource by creating a xml file, i.e. res/drawable/splash.xml with the following content:

    <nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/splashimg" 
    android:dither="false"/>;  
    

    Note how it references a real image 'splashimg' which should be a nine patch image (see below).

  2. Create the nine patch images

  3. Add the referenced nine patch images either in res/drawable or in the resolution specific folders e.g. res/drawable/splashimg.9.png

  4. Reference the new drawable in your DroidGap extending class

    public void onCreate(Bundle savedInstanceState) {
    
        super.setIntegerProperty("splashscreen", R.drawable.splash);
    
    }
    

This way you can actually make all kind of background images/effects, by using the power of drawable resources in android. See http://developer.android.com/guide/topics/resources/drawable-resource.html for more.