3

I have a button that says "Sign In". When the user hits it it brings up an alert dialogue that takes user log in info. Once the user inputs their info and hits OK the button stretches to the width required to contain "Welcome, [User Name]". What I'm trying to do is to animate the change in width of the button from "Sign In" to "Welcome, [User Name]".

I've made the button background with a nine patch, which works perfectly if I specify different widths in the xml file. When I try to animate a change in width from the default width to a longer width it just stretches like a normal .png and not a 9.png.

Here's the code I'm using to stretch from one static width to another:

<Button
    android:id="@+id/signIn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:background="@drawable/button_nine_patch"
    android:textSize="20dip"
    android:layout_marginBottom="-2dip" />

And

 ScaleAnimation stretch = new ScaleAnimation(1.0f ,10.0f, 1.0f, 1.0f);

 this.signIn = (Button) this.findViewById(R.id.signIn);
 this.signIn.setOnClickListener(this);

 stretch.setDuration(2000);
 signIn.startAnimation(stretch);

Since I don't know how many letters a person's name will be I need to be able to set the final width of the button from code.

I've googled like crazy and gone over all the android documentation but I just can't find an answer to this problem.

Any help would be MUCH appreciated.

  • I am having the same issue. I am trying to swap the 9-patch out before and after the animation. This may not work in your case, but have you tried that? – prodaea Feb 14 '14 at 17:21

1 Answers1

1

When Android undergoes the animation process on a view, it animates a Bitmap representation of the current view rather than the actual view. Animating the view would require dealing with the components surrounding the view, and would be significantly more costly. While this usually is beneficial, in your situation it turns a 9patch into just any old bitmap, and therefore does not give you the desired result.

I do not know of any way to only transform a specific region of a view, and have a question dedicated to that over here: Stretch/Scale a Specific Region of View/Bitmap

Community
  • 1
  • 1
Daniel Smith
  • 8,561
  • 3
  • 35
  • 58