1

I got this strange problem with a 9-patch in Android, which i want to use as a splash-screen. I use a build-in Android Studio plugin to create a 9-patch, but it doesnt't work properly with images with too high resolution.

This is a simplified version of what I do in generator (X is the symbol of black pixels):

    X       X
  ************X
X ************X
  ************X
  ****LOGO****X
  ************X
X ************X
  ************
   XXXXXXXXXX

When i try to use it as a activity background it behaves like a normal bitmap, not like a 9-patch - it is displayed in original resolution and doesn't fit the screen. This is the code of XML file:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@color/colorPrimary"/>
    <item>
        <nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
            android:src="@drawable/patch_logo"
            android:tileMode="disabled"
            android:gravity="center"/>
    </item>
</layer-list>

I decided to test it as a background of a layout, in that case it was the FrameLayout where I load fragments with initial settings. This image shows two previews - on the left is used a normal png, 800x800. On the right side is a 9-patch file of the same image.

previews

As you can see, left image is just stretched to fit the layout. Right image, although in preview it looks like when i was using xml drawable as a background of activity, on a real device it is displayed exactly like using normal png - it is just stretched.

When I use 500x500 image 9-patch it is a little bit too big on preview, but on my device it looks good, just the resolution is too low.

All 9-patch files are stored in drawable folder, it is not specified for any density.

This is the logo I want to use logo

I was trying to find the solution all day. What am I doing wrong?

Pawel
  • 163
  • 1
  • 1
  • 5
  • This doesn't seem a case where you want a **9 patch** (you don't want stretched areas). – Phantômaxx Oct 06 '16 at 10:44
  • I want stretched areas only on the top, bottom, left and right of the image to make the logo propotions look good on all screens. That is what i was trying to do: [link](http://fsiete.blogspot.com/2013/10/android-9-patch-splash-screens-that-do.html) – Pawel Oct 06 '16 at 10:58
  • Well, you don't really want that. Instead, check [this post](http://stackoverflow.com/questions/4668001/android-stretch-image-in-imageview-to-fit-screen) – Phantômaxx Oct 06 '16 at 11:22
  • But i need an image which is set as a background for activity. It takes quite a lot of time before layout is loaded, especially when application is launched for the first time. Without it I just got a blue screen which doesn't look good. Actually I am using ImageView now, and it works fine but as I said, it doesn't load layout immediately so at the beginning user see just a screen with blue background, so I wanted to add a splash screen – Pawel Oct 06 '16 at 11:57
  • Yes, we're supposed to be talking about a Splash Screen. Splash Screens aren't limited to show 9 patches. – Phantômaxx Oct 06 '16 at 12:23
  • Ok, so what is the other way do do it? All I know is that I have to set the image as the background of activity in styles to load it immdiately after launch. How can i make it look good on every screen without using 9 patch? – Pawel Oct 06 '16 at 15:16
  • You don't need to put the image in styles. It's enough that your layout covers the whole screen and that the ImageView inside it covers the parent. And choose the appropriate [scaleType](https://developer.android.com/reference/android/widget/ImageView.ScaleType.html) for it. – Phantômaxx Oct 07 '16 at 07:01
  • As I said, it takes some time to load a layout, lets say 10s on my device, so imageview isn't displayed too. But when I set background in styles it loads image immediately, thats why I want to do it this way – Pawel Oct 07 '16 at 13:10
  • `lets say 10s on my device` IMPOSSIBLE. The app will show an ANR (Application Not Responding) dialog after 5s. Let's say 1 or 2s. – Phantômaxx Oct 07 '16 at 13:27
  • I've just tested it - it takes 13,5s to load layout. Application is responding, this is the log which is repeated in logcat during this process: _Adjust thread count for runtime dex2oat_. It is only after first launch of applciation. – Pawel Oct 07 '16 at 14:47
  • http://stackoverflow.com/a/29387706/2649012 – Phantômaxx Oct 07 '16 at 15:52
  • 1
    Ok, i found the reason of waiting so long, it's because of the instant run option in Android Studio. Now using ImageView as a Splash Screen is good enough. – Pawel Oct 07 '16 at 21:03

0 Answers0