Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 851
Milad Faridnia Avatar asked Jan 20 '26 04:01

Milad Faridnia


1 Answers

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" />

like image 145
Milad Faridnia Avatar answered Jan 22 '26 18:01

Milad Faridnia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!