list view with change width of row when scroll lik

2019-06-14 10:02发布

问题:

I am trying to achieved animated list view.As show in below image when list view item fully visible that time change width of fully visible item other wise its width remain default width as given.Here blue color item width is small when its not fully visible but when green color item come in middle of screen then its width goes to full mode with animation.How can i achieved this type of animation in list view ?

change size of up coming item with animation for list item.

回答1:

Try with the below code... and make the changes to suite you.

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.ListView;

public class MyListView extends ListView {
    private final Transformation mTransformation;

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (!isInEditMode()) {
            setStaticTransformationsEnabled(true);
            mTransformation = new Transformation();
            mTransformation.setTransformationType(Transformation.TYPE_MATRIX);
        } else {
            mTransformation = null;
        }      
    }

    @Override
    protected boolean getChildStaticTransformation(View child, Transformation t) {
        mTransformation.getMatrix().reset();
        final int childTop = Math.max(0,child.getTop());
        final int parentHeight = getHeight();
//      Log.d("details1","childTop : "+childTop+" , parentHeight : "+parentHeight );
        final float scale = (float)(parentHeight-(childTop/2))/getHeight();
//      Log.d("details2", "parentHeight-(childTop/2) : "+ (parentHeight-(childTop/2)) );
//      Log.d("details3", "getHeight() : "+getHeight());
//      Log.d("scale",scale+"");
        final float px = child.getLeft() + (child.getWidth()) / 2;
        final float py = (child.getTop() + (child.getHeight()) / 2);
//      Log.d("details4", "child.getLeft() : "+child.getLeft()+ ",  child.getWidth(): "+child.getWidth()+" , child.getTop(): "+child.getTop()+" , child.getHeight()"+child.getHeight());
//      Log.d("px py", px +" , "+py);
//      Log.e("end", "end");
        mTransformation.getMatrix().preScale(scale, scale);

        t.compose(mTransformation);
        return true;
    }

}