Android complex seekbar - android

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.

Related

keep aspect ratio of background image

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>

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.

Disabling anti-aliasing/dithering/other image enhancements on background drawables / 9-patch pngs

I am having trouble removing anti-aliasing on a 9-patch png and a normal drawable that is tiled to make a background. I am using the 9-patch as a background for my EditText controls and the tiled drawable is used as a background for an ImageView to create the 'divider'.
Here is some enlarged versions of my 9 patch png (it's hard to show white graphics so I filled in the transparent areas in blue):
And my tiled background image along with the xml that defines the tiled pattern:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/border_diagonal_lines"
android:tileMode="repeat"
/>
Here is the whole thing put together:
The screenshot on the left is what I currently get and the screenshot on the right is what I want it to look like. What could I do to achieve the desired effect?
I also would like the imageview with the tiled background to always be 3 pixels tall regardless of screen resolution but using 'pixels' as units is generally a bad idea. Is there a way round this?
It isn't anti-aliasing your graphics, it's probably scaling them (which, granted, may also perform anti-aliasing, but it's not the cause). Are you placing them in the correct density-specific folder? (e.g. drawable-hdpi, drawable-xhdpi, etc.) If you are doing that, also make sure you are not using the android:anyDensity="true" tag in your manifest.
I also would like the imageview with the tiled background to always be
3 pixels tall regardless of screen resolution but using 'pixels' as
units is generally a bad idea. Is there a way round this?
It's discouraged in general cases, when the system should be handling scaling for multiple densities (using DP instead), but if you specifically need something 3 pixels tall, then there's nothing "bad" about specifying a pixel height.

The designer has given me the PSD. Now, how do I create my android layout?

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.

How can I draw a tiled background image with "ends" on 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".

Categories

Resources