如何动画从右到左动画

时间:2016-01-26 19:19:22

标签: android android-layout listview animation

我正在尝试从右侧滑动线性布局视图并停在列表视图的中间点,但是使用我当前的动画代码,它比预期的宽度更远。此外,当崩溃发生时,它有点工作,但重新显示静态视图然后消失......

这是动画类:

public class MyCustomAnimation extends Animation {

    public final static int COLLAPSE = 1;
    public final static int EXPAND = 0;

    private View mView;
    private int mEndWidth;
    private int mType;
    private LinearLayout.LayoutParams mLayoutParams;

    public MyCustomAnimation(View view, int duration, int type) {

        setDuration(duration);
        mView = view;
        mEndWidth = mView.getWidth();
        mLayoutParams = ((LinearLayout.LayoutParams) view.getLayoutParams());
        mType = type;
        if(mType == EXPAND) {
            mLayoutParams.width = 0;
        } else {
            mLayoutParams.width = LayoutParams.WRAP_CONTENT;
        }
        view.setVisibility(View.VISIBLE);
    }

    public int getWidth(){
        return mView.getWidth();
    }

    public void setWidth(int width){
        mEndWidth = width;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {

        super.applyTransformation(interpolatedTime, t);
        if (interpolatedTime < 1.0f) {
            if(mType == EXPAND) {
                mLayoutParams.width =  (int)(mEndWidth * interpolatedTime);
            } else {
                mLayoutParams.width = (int) (mEndWidth * (1 - interpolatedTime));
            }
            mView.requestLayout();
        } else {
            if(mType == EXPAND) {
                mLayoutParams.width = LayoutParams.WRAP_CONTENT;
                mView.requestLayout();
            }else{
                mView.setVisibility(View.GONE);
            }
        }
    }
}

取自stackoverflow上的答案:Animate view sliding out of another view, pushing views below out of the way

这是我尝试在列表视图中实现它的方式:

holder.homeInfoBtn.setOnClickListener(new View.OnClickListener() {
            int width;
            @Override
            public void onClick(View v) {
               // holder.homeProductInfo.setVisibility(View.VISIBLE);
                //holder.homeInfoBtn.setVisibility(View.GONE);
                MyCustomAnimation a = new MyCustomAnimation(holder.homeProductInfo, 1000, MyCustomAnimation.EXPAND);
                width = a.getWidth();
                holder.homeProductInfo.startAnimation(a);
            }
        });

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/home_list_image">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="@dimen/home_list_image"
        android:id="@+id/homeProductPic"
        android:src="@drawable/ic_person_black_18dp"/>


    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="10dp"
        android:id="@+id/homeInfoBtn"
        android:src="@drawable/ic_info_black_18dp"
        android:layout_alignParentRight="true"
        android:focusable="false"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/home_list_image"
        android:orientation="horizontal">

        <View
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1" />

        <!-- Right side spacer -->
        <LinearLayout
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:background="@color/background_floating_material_dark"
            android:gravity="center_vertical"
            android:padding="5dp"
            android:id="@+id/homeProductInfo"
            android:animateLayoutChanges="true">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/homeName"
                android:text="Name/>

        </LinearLayout>

    </LinearLayout>



</RelativeLayout>

0 个答案:

没有答案