Thursday, January 29, 2009

How to have a tiled background ( cont.)

Thanks to Romain Guy, I finally could add a repeating background in my Android application !

I'm still using in my layout definition the same way to define the background, with a androidbackground parameter, with the path to your repeating background ( actually that was the point : having a consistent way for background, let them be tiled or not )

xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/MainLayout"
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/backrepeat"
>
...

in my drawable directory, I have a back.png file (  the background I want to tile ), and I create this new backrepeat.xml file :
 <?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/back" 
    android:tileMode="repeat" />


Some points :
* I think my first tries failed because I forgot the 'xmlns:android' parameter.
* I tried without the xml version line, and it still works, but I think I will let it be there... Just in case !
* The tilemode can be repeat, mirror, or clamp.
* Although in Java, I could set a different repeat mode for X and Y, I don't think it is possible in the Xml file. Not that I really care for the moment :)

That's all !
Thanks again Romain ! 
I know it is your job, as a Android engineer, to evangelise and help us, but several times you really helped me, you were quick to answer, with concise and most useful answers...

Saturday, January 24, 2009

How to have a tiled background

Today, I wanted to have a tiled background in my activity.
Until now, my android application only had a stretched image, so I though it was a good time for a change !

Having a background pattern that repeat itself seems like a very common idea, in particular on mobile development where you do not want to spend too much memory on a big image !



But actually it took much more time that I would have think to find this one !
I finally came up with this code :



public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
View MainLayoutView = (View) findViewById(R.id.MainLayout);
BitmapDrawable MyTiledBG = new BitmapDrawable( BitmapFactory.decodeResource(getResources(), R.drawable.background) );
MyTiledBG.setTileModeXY( Shader.TileMode.REPEAT , Shader.TileMode.REPEAT );
MainLayoutView.setBackgroundDrawable( MyTiledBG );
<...>
}
(note that you can use also Shader.TileMode.MIRROR to have a different repeatition pattern )

This code works fine, but I would have prefered a XML way to do this.

I've seen the BitmapDrawable attribute defined in the SDK, but couldn't come with anything working...

Tuesday, January 20, 2009

Sliding drawer, again

The sliding drawer developped by pskink has gone quite a long way from where it came from, and is now a really nice and, I think, usable widget, much like the google home page one !

This kind of widget is fun to use, and fits really well for mobile applications, where there is an obvious lack of place. And, on google phone, users are used with this widget, as there is one on the home page !

Here are some screen of this widget in action :



Enjoy !
[Edit ] :
Ooooppss...
I forget to add the link to the source :
You can find it here !
[/Edit]

Friday, January 16, 2009

Sliding Drawer

Interested in the Home Page sliding drawer ?

pskink has created a new widget like the google one.
Nice job, and easy to use...

Here it is :

Enjoy !

Wednesday, January 14, 2009

To accelerate or to decelerate ?

As many developers discovering a new development environment, I use and abuse of copy and paste.
Starting a new feature process is : 
  • finding in the documentation, or on the internet a similar feature, or something as close as possible, 
  • try to find the parameter/line / bit of code to change to have something like what I had in mind in the first time,
  • Be happy :)
  • ( if I have some time : fully understanding what I've just written... )

And most of the time, I don't have that much have time !

For my tries with animations, I find a sample that moved a view, copied this sample for making my high score list view slide out and in. And that's all.

Only now, I discover that in the sample animation I copied, there was this parameter :
android:interpolator="@android:anim/accelerate_interpolator"

And only now, I discover my mistake. The accelerator interpolator makes the speed of the moving view increase with the time.
So when my high score listview is sliding out of the screen, it has a slow speed at first, when one can see it in its integrality, and a fast speed at the end of the animation, when only a little part of it is visible.

But on the contrary, when it is sliding in in the screen, it has its greatest speed at the end of the animation, when one can see it in its integrality !
This just makes a awful animation !

Now that I discovered that my animation can decelerate for slides in, it is way better !