Android View 动画

标签 android android-layout android-animation android-view

我已经在互联网上搜索了一段时间,但一直无法为我的动画问题找到可行的解决方案。

我有一个 ListView ,当您单击其中一项时,更多信息会从底部动画显示,为您提供几行附加信息。这只是我在用于这些列表项的 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);

不这样的结果是,当我单击 ListView 项时,应该是动画的 View 最后占据的整个空间是白色的,而 View 从上到下进行动画处理。位置是正确的,但我希望它能够动画并将其下方的所有内容推开,而不是立即将所有内容推开然后动画填充该空间。

知道如何让它在动画期间而不是立即将所有东西推开吗?甚至有可能达到这种效果吗?非常感谢任何帮助。

此外,我知道如何让它简单地在其他 View 之上设置动画,但我需要它来真正将所有内容推开。

最佳答案

这样做的技巧是创建您自己的动画子类。

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;
    }
}

然后您可以将其应用到您应该扩展的 View 中:

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

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

关于Android View 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10626192/

相关文章:

具有多种外设的Android配件模式

java - 如何在Java中向onClick函数传递参数?(Android应用程序开发)

android - 在 Android 中倾斜 TextView

android - 使用 AppCompat 获取 NullPointerException

Android 在不应检测到点击时检测到点击

android - FragmentTransaction.replace()淡入过渡显示 “ghost” fragment

java - onPause中的unregisterReceiver(receiver)导致接收者不注册

java - 如何使用 IF 条件比较 Flutter(dart)中的两个 DateTime 值?

android - layout_gravity ="center_vertical"和 layout_centerVertical ="true"有什么区别

android - 有没有办法改变 View android的海拔颜色?