其他分享
首页 > 其他分享> > Android View动画

Android View动画

作者:互联网

我已经搜寻互联网已有一段时间了,但一直无法找到解决动画问题的可行解决方案.

我有一个列表视图,当您单击其中一项时,会在底部显示更多信息,为您提供几行其他信息.这只是我在XML文件中用于这些列表项的线性布局,这里:

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




<LinearLayout
    android:id="@+id/friendActivityList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/friendInfo"
    android:background="@color/grey"
    android:orientation="vertical"
    android:visibility="gone" >

    <RelativeLayout
        android:id="@+id/RelativeLayout04"
        android:layout_width="match_parent"
        android:layout_height="@dimen/freind_activity_list_height" >

        <ImageView
            android:id="@+id/ImageView04"
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_margin="5dp"
            android:src="@drawable/logo_d" />

        <TextView
            android:id="@+id/TextView04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/ImageView04"
            android:text="TextView"
            android:textColor="@color/black"
            android:textSize="17dp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/RelativeLayout03"
        android:layout_width="match_parent"
        android:layout_height="@dimen/freind_activity_list_height" >

        <ImageView
            android:id="@+id/ImageView03"
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_margin="5dp"
            android:src="@drawable/logo_d" />

        <TextView
            android:id="@+id/TextView03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/ImageView03"
            android:text="TextView"
            android:textColor="@color/black"
            android:textSize="17dp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/RelativeLayout02"
        android:layout_width="match_parent"
        android:layout_height="@dimen/freind_activity_list_height" >

        <ImageView
            android:id="@+id/ImageView02"
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_margin="5dp"
            android:src="@drawable/logo_d" />

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/ImageView02"
            android:text="TextView"
            android:textColor="@color/black"
            android:textSize="17dp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="@dimen/freind_activity_list_height" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_margin="5dp"
            android:src="@drawable/logo_d" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@id/imageView1"
            android:text="TextView"
            android:textColor="@color/black"
            android:textSize="17dp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/RelativeLayout01"
        android:layout_width="match_parent"
        android:layout_height="@dimen/freind_activity_list_height">

        <ImageView
            android:id="@+id/ImageView01"
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_margin="5dp"
            android:src="@drawable/logo_d" />

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/ImageView01"
            android:text="TextView"
            android:textColor="@color/black"
            android:textSize="17dp" />
            </RelativeLayout>
        </LinearLayout>






        <RelativeLayout
            android:id="@+id/friendInfo"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:background="@drawable/bg_list_item_n" >

    <ImageView android:id="@+id/imgCompany"
        android:layout_marginLeft="5dp" 
        android:layout_centerVertical="true"
        android:layout_width="60dp"
        android:src="@drawable/ic_launcher"
        android:scaleType="centerInside" 
        android:layout_alignParentLeft="true"
        android:layout_height="50dp">
    </ImageView>



    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:layout_toRightOf="@id/imgCompany"
        android:background="@android:color/transparent"
        android:gravity="left|center"
        android:orientation="vertical"
        android:paddingLeft="5dp" >

            <TextView android:id="@+id/lblCompanyName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:textSize="13dp"
                android:text="Company Name">
            </TextView>

            <TextView android:id="@+id/lblReawrdDesc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:textSize="13dp"
                android:text="Reawrd Description">
            </TextView>

            <TextView android:id="@+id/lblScores"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:textSize="13dp"
                android:singleLine="true"
                android:text="My Score: 13434 | Top Score: 344425">
            </TextView>

        </LinearLayout>

    </RelativeLayout>

    </RelativeLayout>

其中大多数只是占位符信息,这样我就可以使动画正常工作.这是即时消息用于动画的代码:

    listviewFriends.setOnItemClickListener(new OnItemClickListener() {//this is the listView that im animating inside of

        public void onItemClick(AdapterView<?> parent, View view,final int pos, long id) {
            Log.v("ListItemClicked", "this position was clicked: "+pos);
            if(friendInfoList != null){
                friendInfoList.clearAnimation();//this is the new view that gets animated and is supposed to push everything below it out of the way
                friendInfoList.setVisibility(View.GONE);
            }
            friendInfoList = (LinearLayout) view.findViewById(R.id.friendActivityList);
            friendInfoList.setVisibility(View.VISIBLE);
            friendInfoList.startAnimation(infoAnim);

        }
    });

    infoAnim = new TranslateAnimation(0,0, -150, 0);//this is the animation im using
    infoAnim.setDuration(1000);
    infoAnim.setFillAfter(true);

这样做的结果是,当我单击列表视图项时,假定要进行动画处理的视图最后占用的整个空间为白色,而视图从上到下进行动画处理.该位置是正确的,但我希望它进行动画处理并将其下方的所有内容推开,相反,它会立即将所有内容推开,然后设置动画以填充该空间.

知道如何在动画过程中而不是立即将所有内容推开吗?甚至有可能达到这种效果?任何帮助是极大的赞赏.

另外,我知道我如何才能使其简单地在其他视图之上创建动画,但是我需要它实际上将所有内容推开.

解决方法:

诀窍是创建自己的Animation子类.

public class ExpandAnimation extends Animation
{
    private int _targetHeight;
    private View _view;
    private boolean _down;

    public ExpandAnimation(View view, int targetHeight)
    {
        _view = view;
        _targetHeight = targetHeight;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t)
    {
        int newHeight;
        if(_down)
        {
            newHeight = (int) (_targetHeight * interpolatedTime);
        }
        else
        {
            newHeight = (int) (_targetHeight * (1 - interpolatedTime));
        }

        _view.getLayoutParams().height = newHeight;
        _view.requestLayout();
    }

    public ExpandAnimation expand()
    {
        _down = true;
        return this;
    }

    public ExpandAnimation collapse()
    {
        _down = false;
        return this;
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight)
    {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    @Override
    public boolean willChangeBounds()
    {
        return true;
    }
}

然后,您可以将其应用于应扩展的视图:

public void togglePreview()
{
    if(_expanded) _preview.startAnimation(_animation.collapse());
    else _preview.startAnimation(_animation.expand());

    _expanded = !_expanded;
    getParent().requestLayout();
}

标签:android-layout,android-animation,android-view,android
来源: https://codeday.me/bug/20191101/1982735.html