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?
Related
Lets say I have a big image, something like 2000x3000px, and want to cut out an 1080x1920px image and put it as the background of my app. What I figured out is that every device with a viewport of 412x732 will display this image correct (no different factor for stretching in height and width). The aspect ratio will be the same and therefore an object of the 1080x1920 image wont look stretched in height while maintaining the same width on an 1440x2560px (viewport 412x732) device. However when displaying this image on an 1080x2280px device (viewport 412x869) the image will look stretched (like you can see in the right image). Now I wonder how I can implement background images for different aspect ratios so that such stretching wont occur on any device. Instead I want that more parts of the original big image are displayed on screens with larger (or less image on smaller) aspect ratio devices.
Imagine the black border of the image is the original 2000x3000px, the blue border would be the image on an 1080x1920 device and the green border would be an 1080x2280 device. I hope you get the idea what I mean.
After some research I found the option to use an ImageView with android:scaleType="centerCrop" rather than putting the image as background. However I don't know if this will solve my problem. What do you guys think would be the best way to solve this?
With the android:scaleType="centerCrop" you should additionally put ImageView on every your screen. This would be fine if you have only 1 screen, otherwise you will need to duplicate same trick.
If you need the same background of windows to be set as theme property so you could wrap your image with drawable resource. Something like:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#color/bg_image"/>
<item>
<bitmap
android:gravity="center|bottom|clip_vertical"
android:src="#drawable/your_image" />
</item>
</layer-list>
Playing with gravity you'll be able to receive preferred result with scaling/positioning.
To set image as a theme property you need to add it in styles AppTheme:
<item name="android:windowBackground">#drawable/you_image_as_a_layer_list</item>
I can see Delphi XE7 comes with splash/startup image support for Android.
However, when I choose to center the image (which looks best) Delphi shows black color around it. I would prefer white since it fits better with the image and color theme of the app. There does no appear to be an opion for background color where you set splash/startup images.
How can I change this color?
If you want fill the black background with the same background color from your image you must use 9patch images. To make 9patch pngs use NinePatch (included in android sdk)
Once you hace the images, add to your project and set up like this:
splash tile mode: disabled
splash gravity: center
Then go to project -> deployment:
Uncheck splash_image_def.xml (to not deploy)
rename your splash_image.png to splash_image_def.9.png
If you are just wanting to change the background color of the splash screen, you can modify two files. colors.xml and splash_image_def.xml. You will find these in the Android/Debug/ (or Android64/Debug/) folder of your project. These get recreated each time you deploy your application so you will need to make backup copies of them.
In Project|Deployment, untick these items and create new entries for the copies you have made, making sure that you deploy them to the same location. You will end up with two entries for Debug and Release.
Edit colors.xml and add the new color that you want like so
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<color name="notification_accent_color">#000000</color>
<color name="logoblue">#0094ff</color>
</resources>
Here I've added the logoblue color. Next edit splash_image_def.xml and change #android:color/black to #color/logoblue. It should look somewhat like the following
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
<item android:drawable="#color/logoblue" />
<item>
<bitmap
android:src="#drawable/splash_image"
android:antialias="true"
android:dither="true"
android:filter="true"
android:gravity="center"
android:tileMode="disabled"/>
</item>
</layer-list>
There might be a simpler way of doing this, but this is what I've come up with through a bit of trial and error.
There is no background color property of splash images because this is supposed to be a part of the image. There are 4 different possible sizes for Splash Images on Android:
426 x 320
470 x 320
640 x 480
960 x 720
So, depending on your supported devices, you should have up to 4 images matching these sizes, with the background color being whatever you wish. Set it up to fill the entire screen, and make sure you're not using transparency. For Splash Gravity, select the fill option.
to change default black splashscreen, just open
splash_image_def.xml at debug folder,
and change "black" to white if you want change it to white, like this
<item android:drawable="#android:color/white" />
Note : Build it with no change at source code, layout or anything at RAD Studios. Just edit the file xml with notepad, save it, then rebuild.
My designer has given me the following layout (a little bit changed to protect my business :)
Now, I am facing a lot of doubts as how I can incorporate every element to my android layout.
As you can see, the background has a light noise to it. I tried creating a 9patch from it, but when loading in Android, I completely lost the quality of the imaged. It stretched out weirdly. Should I have one background for the different resolutions (ie. mdpi, hdpi... ) or can this be achieved with 9 patch
What to do with the button? I tried saving the button (which in photoshop it is a group) into a new image and saved it as a PNG. When loading in the layout with a ImageButton and trying on my phone, the button is just too big. How can I guarantee the size of the button on different type of screens? Will I need different sizes of this button for different resolutions (mdpi... ) and if so, how will I know what is the size of this button for a hdpi resolution, or for a mdpi resolution? Or maybe I should force the width and height of the button to a value like 60dp, but that doesn't sound right to me. I understand I can create a shape and apply it to the background of the button but, what about the Facebook button? I imagine that, in that case, I will need a ImageButton with the PNG as background.
I know this is late as hell but hopefully it will help others.
1) Ask your designer for a repeatable image that you can tile for the background. I don't see any reason why it couldn't be tiled. To tile an image make an xml file of type drawable for each of the generalized dpi folders. Put this code in the xml file:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:src="#drawable/piece_of_the_background_that_can_be_repeated"
android:tileMode="repeat" />
Set the background drawable to this xml file.
2) Set a 9patch image as the button background and use this custom textView for the text. Use the innerShadow attribute to get the shadow effect your designer shows.
I want to create some kind of row image that looks like that
but I want it to scale with the screen size and density. So reading this http://developer.android.com/guide/developing/tools/draw9patch.html and this http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch I startet creating a Nine-patch image.
But when I import it and use it in my android project it looks like that
As you can see the little black line that was drawn with the draw 9 patch tool is still visible. Why?
Here is the 9.png image
Are you using the draw9patch tool that comes with the sdk? it is quite handy.
Anyway:
It is vital that every pixel except the black ones are completely
transparent.
The black pixels should be at the absolute top/bottom/left/right of
the image.
The image should be named filename.9.png.
I have found the best and the simplest answer to make 9-patch image.
This is best link to create 9-patch image for all the resolutions - XHDPI, HDPI, MDPI, LDPI in just one click.
Let me know if you have any queries, and do upvote it, if it was helpful to you.
If you need an image which consists of a border with rounded corners, I don't have to create a 9-patch.
All you need is an XML like this stored in the res/drawable directory
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="3.0dp" />
<stroke
android:width="2dp"
android:color="#color/green" />
</shape>
Read more about drawable resources
In a start activity of our android application we're using a linearlayout with an background image for whole space.
The image size is 320x480 - the same as device resolution, we're using for testing.
The problem is, the image will be scaled und looks not so nice.
I tried to use imageview instead, but I've got black borders.
Some ideas, how to avoid scaling or how to get the proper size for background image?!
Thank you in advance.
Mur.
Create a background.xml file in res/drawable and use the following:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background_image"
android:gravity="center"/>
And use this background.xml as your LinearLayout's background.