Android. Avoid scaling of background - android

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.

Related

How can I add margin to background bitmap on Android

So I would like to create a checkered effect with my bitmap. So I am using this very small pixel grey color png. I need to add a margin or padding before it's repeated but I'm unsure how to do this. Any help will be welcome as I have just started learning Android development.
What it's doing right now is repeating the entire image, filling the window with one color.
Background.xml
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/bg"
android:tilemode="repeat">
</bitmap>
In my Activity Main XML I'm including the above XML
android:background="#drawable/background"
If I understand You correct and You want to make an effect like this
Using a single grey pixel and adding margin to next before repeating, it will be much easier if You don't use:
But just a:
With a pattern like this, You can repeat an image and You will get a checkered effect.

9-patch with too big resolution doesn't work

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?

Background image quality

I want to apply a background image to a linear layout and I have problems because the image loose from quality when stretched.
The image is composed by the fill color and some dots pattern applied over the background.
The image was made with Adobe Illustrator (png). Also I tried using nine-patch but does not work, the dots are stretched.
Should I make this background from code(override onDraw from the linear layout)?
I'm guessing you want to repeat that pattern, not stretch it. Create a drawable resource in xml form, representing your tiled background:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:tileMode="repeat"
android:src="#drawable/background_image"
/>

Android Application: Background stretching

In my eclipse, when ever I have a scrollView and a background image set up at the same time, my background image stretches vertically. I would like it not to stretch but repeat all the way down the ScrollView.
Please help
Create a XML file within the drawable:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/my_image"
android:tileMode="repeat" />
than you can use this file as your drawable, and it will be repeated
Note that there is a bug related to repeat (android 3.0 and bellow), when used in a listview
see XML drawable Bitmap tileMode bug?

Why does my striped background scale with such a bad quality?

I have an activity that includes a striped background image. Click to enlarge – this is the original image I'm using. There's also a version with a logo for another activity.
It looks fine as is. I set it as a background in the activity through XML, something like this.
I need the hack as an ImageView to be able to properly centerCrop a logo in the background, but this is irrelevant to the issue, as it persists when setting the background for the RelativeLayout and removing the ImageView.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/background_striped" />
Note that since there has to be a logo inside the image, I can't stretch it artificially or have it repeat.
In the layout preview screen, it looks like this:
In the emulator, it looks like this:
As you can see, it looks quite bad, compared to how I'd expect it:
Naturally, I've created versions of the striped background for all display resolutions (from ldpi to xhdpi), but they always seem to be scaled in a pretty bad way.
Is there any way to get the background scaled in a higher quality? Just using the xhdpi version won't result in better quality—it will just make the scale result worse and more washed out.
For scalable drawables it is better to use a 9-patch. You can create one with your small striped background image using the android 9-patch tool:
http://developer.android.com/tools/help/draw9patch.html
Try using repeat. I tested it and it seems to look fine.
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background_striped"
android:tileMode="repeat" />

Categories

Resources