views:

32

answers:

2

is there any way to apply an animation to a property of a view? currently, the only animation i am aware of is applying an animation to an entire view. i'm wondering if i can apply an animation to a property (i.e. layout_width for example)

A: 

Even if you want to apply the animation only to a property of the view you still have to set it as the view's animation.

<scale 
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXScale="1.0" 
    android:toXScale="0" />

This animation for example changes the width of your view.

You can set the Animation by

yourView.setAnimation(AnimationUtils.loadAnimation(this, R.anim.animation_name));
Mannaz
yes, but the problem is that this scales the entire view. so if the view has a background with a 1 dip border (for example) then the border gets thicker as the animation plays. i want to apply the animation to the property and then have the xml drawable re-draw so that the border stays at 1 dip.
Ben
You can use a 9-Patch as a drawable for the background and define the border as a non-stretching area, so your border stays at a fixed with.
Mannaz
I like that idea... but... now the question becomes, is there a way to create a 9patch via xml drawable... :)
Ben
A: 

You can edit the animation to affect only certain parts of the view. For example, I wanted a ViewFlipper to look like it was flipping over vertically, so I made the out animation shrink the y of the view to 0 in the middle and the in animation grow the y from the middle. Here's what they look like.

shrink_to_middle.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <scale
        android:interpolator="@android:anim/linear_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.0"
        android:fromYScale="1.0"
        android:toYScale="0.0"
        android:fillAfter="false"
        android:duration="200" />
    <translate
        android:fromYDelta="0"
        android:toYDelta="50%"
        android:duration="200"/>
</set>

grow_from_middle.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"&gt;
    <scale
        android:interpolator="@android:anim/linear_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:fillAfter="false"
        android:startOffset="200"
        android:duration="200" />
    <translate
        android:fromYDelta="50%"
        android:toYDelta="0"
        android:startOffset="200"
        android:duration="200"/>
</set>
CaseyB