I have a 9-patch image file which looks like this:
When I use it it appears like this:
What I actually wanted to achieve is the complete dot in the center repeated instead of stretched. I hope that it's possible.
I think it's not possible using 9-Patch to make repeated patterns (only stretching certain area), perhaps you could find more about it in official documentation
...
Correction: If you want the orange dots to repeat, you will not succeed using 9 patch. 9 patch can only stretch the part you told it to stretch and leave untouched, the remaining areas. There is no repeat mode with 9 patch PNG.
You might want to look into Bitmap class. There is a tileMode you might be able to use for your problem here.
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/stripe_bg"
android:tileMode="repeat"
android:antialias="true"
android:dither="false"
android:filter="false"
android:gravity="left"
/>
First make sure you save your 9 patch image as your_image_name.9.png and store it in the res/drawable folder. Then in your xml just set the layout background with-- android:background="#drawable/your_image_name" and that should work. If it's still not working can you post your layout xml?
Related
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.
I'd like to 9patch this image, but I can't seem to
I've tried understanding the things mentioned here https://software.intel.com/en-us/xdk/articles/android-splash-screens-using-nine-patch-png but I can't seem to apply them to my image
Any ideas? And yes, it doesn't help that the icon is rounded ...
According to the linked page you "draw lines that define the stretchable patches.". In your case, the stretchable part seems to be left of the logo until the left half-circle starts.
I added an image with an oversized stretch-line to mark the horizontally stretchable part.
There is no way, to use your image only with 9 path. Best practices for your task - using several images with 9 path. Maybe creating custom view.
1) As you can see, first image, parent with blue color, should include 9 path (it's easy to make without internal image).
2) For best looking in any device you need to create another thing for second image (with facebook char). 9 path for circle - not best idea. So I advice you use holder, with xml canvas background and char "f" in foreground.
3) Now you need to put this all in one FrameLayout, which you will use anywhere. All structure will be as example below:
<FrameLayout> // You may create custom view instead of FrameLayout
// for more flexible and understanding
<BlueImageBackground.9path
gravity=center>
</BlueImageBackground.9path>
<WhiteImageFacebokChar
gravity= center_vertical | left>
</WhiteImageFacebokChar>
</ FrameLayout>
As a result, in any screen, this image will appear identically...
The source image you provided should be edited first, my version:
in sdk/tools folder there is draw9patch tool, so you can use it to edit it. You would see something like this:
Hi it is preferable to create an empty 9 patch image "without the logo" and then setting it as the background of a relative layout and then add an image view to the relative layout and add to the ImageView:
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
Moreover you have to adjust the margin of the image view to achieve the desired result.
Note that this method will not stretch the icon & it requires some fine tuning to the image size and padding to achieve the desired results :-)
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:tileMode="repeat"
android:src="#drawable/header_background">
</bitmap>
I am using the following as a background for the action bar. The code gives me the following image:
How is there gaps on the image ? Can someone please help.
header_background:
Modify the image to remove the shadow outline. It needs to be a flat image, so that when it is tiled, it will be visually seamless.
See this example:
Also, you might be interested in this post regarding tiling along the x-axis.
remove the android titlemode = repeat
Can you not just make this a 9patch that scales like you are looking to do with horizontal tiling?
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" />
I am currently trying to use 9 patch pictures in my app.
The image is quite clear and work pretty well in the graphic tool provided with SDK:
Picture is like this:
and the tool seems to work fine:
Unfortunately, with such a simple layout, the rendering is bad on a device and the 9 patch does not work at all:
<TextView
android:id="#+id/platenumber"
android:background="#drawable/plate_fr"
android:layout_width="320dip"
android:layout_height="wrap_content" />
Any idea on what I am doing wrong?
EDIT:
My picture is named *.9.png
The black lines have to be totally black(RGB:#000000) and the transparent zone around black lines totally transparent.
The extention of your 9-patch image should end with .9.png or it will be taken as a normal image
Check if you have at least one black pixel on all four sides of your images.