Milad Milad - 6 months ago 41
Java Question

Scale Animation for listItem

I have a

ListView
. I know how to highlight or change the background of a selected item in a ListView.
But I want to scale the selected item like this:

(selected item is a little bigger than other list items in this picture)

enter image description here

Can anyone help me to do that?

Answer

I found the solution, I set OnFocusChangeListener for List items and set animation for them:

OnFocusChangeListener itemFocusChangeListener = new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
                int focus = 0;
        if (hasFocus) {
            focus = R.anim.enlarge;
        } else {
            focus = R.anim.decrease;
        }
        Animation mAnimation = AnimationUtils.loadAnimation(
                getActivity().getApplication(), focus);
        mAnimation.setBackgroundColor(Color.TRANSPARENT);
        mAnimation.setFillAfter(hasFocus);
        v.startAnimation(mAnimation);
        mAnimation.start();
        v.bringToFront();
    }
};

enlarg.xml:

<?xml version="1.0" encoding= "UTF-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
<scale
    android:duration="100"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:pivotX="50.0%"
    android:pivotY="50.0%"
    android:repeatCount="0"
    android:toXScale="1.15"
    android:toYScale="1.15" />

decrease.xml:

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="false"
android:fillBefore="true"
android:shareInterpolator="false" >

<scale
    android:duration="100"
    android:fromXScale="1.1"
    android:fromYScale="1.1"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:pivotX="50.0%"
    android:pivotY="50.0%"
    android:repeatCount="0"
    android:toXScale="1.0"
    android:toYScale="1.0" />

Comments