How can I draw a tiled background image with "ends" on Android? - android

I want to create a bookshelf that tiles horizontally and vertically. I have three images, ShelfLeft, ShelfMid, and ShelfRight. ShelfLeft starts each row/shelf, followed by X ShelfMids across the screen, capped off by ShelfRight at the end of each row.
There will be a default of 5 rows, and if more are needed, they shall be able to be added dynamically.
What is the best way to go about doing this?
Thanks.

I'd use a TableLayout. Use a cell for each of the "ends", and then another single cell for the tiled background.
To make a background repeat, create res/drawable/my_background.xml:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/back"
android:tileMode="repeat" />
Then reference this as the background in your layout by specifying `android:background="#drawable/my_background".
(Source: http://androidblogger.blogspot.com/2009/01/how-to-have-tiled-background-cont.html)
Also worth noting: You might find the NinePatch class useful when creating your "ends".

Related

Android complex seekbar

I need to create a seekbar that has custom background and custom thumb image. The problem is that background is kind of complex and i can't really create nice 9patch out of it.
Seekbar should have 3 values (0,1,2) and each value is represented by an image. Thumb should be centered around image of current value. Picture shows the seekbar with value "1" selected:
Problems I had were:
to create a 9patch from background and keep edge and central image from moving / scaling.
create a thumb that will always be in right scale compared to images representing different values.
How can I do this?
Edit
In short: I'm having troubles to make sure seekbar fills entire width of the screen (regardless of screen size), but again to make sure "ring" (thumb) will always fit perfectly around each "value" image.
I am not quite sure I understand your problem, but you can create a bitmap xml to avoid resizing on your background pictures:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/mydrawable"
android:tileMode="disabled"
android:gravity="top" >
</bitmap>
If you are having troubles with arranging the items/pictures, you can always play with a relative layout's diferent params.

Create a pattern along the bottom edge of a view

I would like to have a pattern along the edge of a view. For this particular case, I would like it to be a jagged edge (like tearing parchment paper or tinfoil out of a roll). I would prefer a more general solution - perhaps at some point I would like waves along the top of a view, or a binder coil down the side. I would prefer to have some kind of repeating image rather than stretching an image.
Edit:
I would like a the pattern to occur just around the edge of the view (perhaps only on one edge). For example, a tear mark on the bottom or top of a receipt. The tear should not repeat up or down the image, just at the bottom edge.
Create a drawable (pattern_tile.xml) with the root element of a <bitmap>. Like this for instance:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true"
android:src="#drawable/bgnoise_lg"
android:tileMode="repeat" >
</bitmap>
And the drawable image used in the code above is:
Then, in your XML, you set it like you set the usual background using this attribute:
android:background="#drawable/pattern_tile"
Oh. Almost forgot, I put these two files, in the drawable-nodpi folder. I don't want Android to ever scale the image for me.
Obviously, since your question is a little vague, you will have to use your own image instead of the example I have posted, play around with it a bit till you get the result that suits your needs.
NOTE: Such a thing is typically done when you have patterns that should repeat instead of filling up the screen or stretching. So make sure the image you use is such a pattern that can repeat and not look out of place.

Create Tiled Background from Image or <LinearLayout> / <GridView> etc

I am wondering as a new Android developer (10+ years C# OOP) which would be the better way to create a simple repeating background. The background will be consistent no matter the screen size, density, or orientation. I've read the Android Developers Dev Guide about things such as supporting multiple screens, nine patch drawables etc. I have seen tutorials such as this one (http://androidforbeginners.blogspot.com/2010/06/how-to-tile-background-image-in-android.html) telling you how you can use an image and get it to repeat.
Of course using an image then you have to provide multiple images for multiple densities or risk bitmap scaling and pixelation. For a complex background pattern I can see how this might be the way to go. But my pattern is a simple grid pattern so isn't there a better way using just xml?
I looked at GridView and TableLayout and both allowed me to set a background color, specify cell width / height, but I did not see a way to specify the grid line color.
For now I am using the slightly older 2.3.3 api as that is the largest version currently in use.
I don't suspect I'll need much hand holdong just some good solid advise from those who know better than I.
Thank You
JB
You can create a drawable like this:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="AndroidPuzzlesSolver/#drawable/bg_tile"
android:tileMode="repeat"
android:dither="true" />
Place the above in a "background.xml" file at the drawable folder. Then you can use it in your layout like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/background">
You can use a background with all Layouts.
I don't think there is an out-of-the-box way to do an alternate repeat, e.g. show one image at some places and another one in some others. If you want something like this, then you would probably need to implement your own View and override the onDraw method. You could use a FrameLayout to combine this background View with any other elements.

Android - tiled backgrounds occasionally get stretched

I use several tiled drawables that are used as backgrounds for various layouts. For the most part the tiles work correctly but occasionally (once on every 4..5 runs) in some (but not all) of the backgrounds a single tile image gets stretched to fill in the background rather than repeat it (resulting in a very "fuzzy" background).
Tiles are declared in an xml drawable like this:
`bitmap android:src="#drawable/bg_tile_dark" android:tileMode="repeat"
xmlns:android="http://schemas.android.com/apk/res/android" />`
The tile drawable is used as a background image like this:
` RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/my_background_tile" >
`
....
/RelativeLayout>
This issue happens across multiple versions of Android and on different devices so there must be a trick to how to use tiles. Does anyone have experience using tile backgrounds or knows a workaround to force tiles to always repeat?

How to place transparent buttons over an image relatively?

I am looking for a way in Android how to place some transparent buttons over a (background) image so I have good control to position the buttons and they stay were they meant to be also if the screen is much larger.
As you can imagine the image contains also the button art...
The best thing would be if I could position the buttons by using percentage, but sadly this is not possible in Android.
This is my current base of the code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/art_main_background" >
As an alternative, I could also extract the button art and place them over the image, but this would lead to the same problem, how can I control the position if the buttons are not in a 'linear' kind of order, i.e. rather random.
You should definitely make the button art separate pieces, and then place them in as ImageButtons. You can use a FrameLayout to stack them on top of the image (actually, if it's set as a background image, you shouldn't even need the FrameLayout) but I think you'll have more problems than that if I understand you correctly.
Are you trying to make a single image as your controls, and just map buttons to specific positions? Considering the number of variations in screen resolutions and sizes, this is just a bad idea on Android. Consider rethinking your layout to be a bit more flexible. If you do need absolute positioning, you can use a FrameLayout, and just specify left and top margins to position them, but keep in mind what might fit perfectly on one resolution won't necessarily fit into another properly.
If you can post a sample of what you've got in mind, we might be able to give you some ideas.

Categories

Resources