2

我在屏幕上有两个视图

一个位于屏幕顶部,位于其正下方

我需要绿色视图滑出顶部 - 结果使蓝色视图占据整个屏幕

在此处输入图像描述

这就是我想要做的:

- 问题是,当动画完成时,蓝色视图只是“跳”起来 - 我希望它随着消失的绿色视图而缓和,我该怎么做?

slide_in_animation.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
            android:duration="1000"
            android:fromYDelta="-100%"
            android:toYDelta="0%" />
</set>

slide_out_animation.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
            android:duration="1000"
            android:fromYDelta="0%"
            android:toYDelta="-100%" />
</set>

MainActivity.java

slideInAnimation = AnimationUtils.loadAnimation(mActivity, R.anim.slide_in_animation);
slideOutAnimation = AnimationUtils.loadAnimation(mActivity,R.anim.slide_out_animation);


    slideInAnimation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            mGreenView.setVisibility(View.VISIBLE);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }
    });

    slideOutAnimation.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {


        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mGreenView.setVisibility(View.GONE);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // To change body of implemented methods use File | Settings
            // | File Templates.
        }
    });
4

3 回答 3

4

这对我有用

没有通知动画通知通知可见动画通知没有通知

首先你必须导入这个库: http: //nineoldandroids.com/

导入是通过将现有的 android 项目导入您的工作区来完成的,然后右键单击您的 Poject -> Properties -> Android。在这里您将看到一个库部分,单击添加按钮并添加 Nineoldandroids 库。

首先,这是用于此工作的布局 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"
        android:divider="@android:color/transparent"
        android:fastScrollEnabled="false"
        android:listSelector="@android:color/transparent"
        android:scrollbars="vertical"
        android:smoothScrollbar="true" />

    <View
        android:id="@+id/greenView"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="#ff00ff00"
        android:alpha="0"/>
</FrameLayout>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/animate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clickHandler"
        android:text="animate" />

    <Button
        android:id="@+id/close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clickHandler"
        android:text="close" />
</LinearLayout>

注意: ListView 和绿色 View 都可以是任何类型的布局,任何类型的内容。

接下来是概念验证活动。

public class TestActivity extends Activity {

private View greenView;
private ListView listView;
private int greenHeight;

private boolean isShowingBox;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    // The animated view 
    greenView = (View)findViewById(R.id.greenView);
    listView = (ListView)findViewById(R.id.listView1);


    // Instanciating an array list (you don't need to do this, you already have yours)
    ArrayList<String> your_array_list = new ArrayList<String>();
    your_array_list.add("1");
    your_array_list.add("2");
    your_array_list.add("3");
    your_array_list.add("4");
    your_array_list.add("5");
    your_array_list.add("6");
    your_array_list.add("7");
    your_array_list.add("8");
    your_array_list.add("9");
    your_array_list.add("10");
    your_array_list.add("11");
    your_array_list.add("12");
    your_array_list.add("13");
    your_array_list.add("14");
    your_array_list.add("15");
    // This is the array adapter, it takes the context of the activity as a first // parameter, the type of list view as a second parameter and your array as a third parameter
    ArrayAdapter<String> arrayAdapter =      
    new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, your_array_list);
    listView.setAdapter(arrayAdapter);

    final LinearLayout layout = (LinearLayout)findViewById(R.id.parentLayout);

       final ViewTreeObserver vto = layout.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                if (Build.VERSION.SDK_INT < 16) {
                    layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } else {
                    layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }

                greenHeight = greenView.getHeight();

            }
        });
}

public void clickHandler(View v) {
    if (isShowingBox) {
        isShowingBox = false;
        slideOut(1500, 0);
    } else {
        isShowingBox = true;
        slideIn(1500, 0);
    }
}

private void slideIn(int duration, int delay) { 
    AnimatorSet set = new AnimatorSet();
    set.playTogether(
        // animate from off-screen in to screen 
        ObjectAnimator.ofFloat(greenView, "translationY",  -greenHeight, 0),
        ObjectAnimator.ofFloat(listView, "translationY",  0, greenHeight),
        ObjectAnimator.ofFloat(greenView, "alpha", 0, 0.25f, 1)
        // add other animations if you wish
    );
    set.setStartDelay(delay);
    set.setDuration(duration).start();
}

private void slideOut(int duration, int delay) {
    AnimatorSet set = new AnimatorSet();
    set.playTogether(
        // animate from on-screen and out
        ObjectAnimator.ofFloat(greenView, "translationY", 0, -greenHeight),
        ObjectAnimator.ofFloat(listView, "translationY", greenHeight, 0),
        ObjectAnimator.ofFloat(greenView, "alpha", 1, 1, 1)
        // add other animations if you wish
    );
    set.setStartDelay(delay);
    set.setDuration(duration).start();
}

}

重要提示:请记住从您的类中的 Nineoldandroids 导入 AnimatorSet 和 ObjectAnimator,而不是从 Android SDK 导入!!!

于 2013-09-30T15:42:35.520 回答
1

一种实现良好且流畅的退出的潜在解决方案是将 LinearLayout 的 weight 属性与 ValueAnimator 结合使用。假设您使用 LinearLayout 作为绿色和蓝色块的父视图,您的代码将如下所示。

布局.xml

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">


    <!--Let's assume this is the view you wish you disappear-->
    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"/>

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"/>
</LinearLayout>

现在有了这个,在您的代码中,您可以使用 ValueAnimator,如下所示:

public class MainActivity extends Activity implements AnimatorUpdateListener
{
    View view1;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        view1 = (View)findViewById(R.id.view1);
    }

    public void someAction()
    {
        //This is the important part, because it is FROM the first value TO 
        //the second.  Notice that it must be a float type
        ValueAnimator anim = ValueAnimator.ofFloat(1f, 0f);
        anim.setDuration(200);
        anim.addUpdateListener(this);
        anim.start();
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation)
    {
        view1.setLayoutParams(new LinearLayout.LayoutParams(0, 
                                    LayoutParams.MATCH_PARENT,
                        (Float) animation.getAnimatedValue()));
    }
}

ValueAnimator 将自动计算增量并执行它们以获得您想要的平滑过渡,并具有保持视图运行的额外好处。

由于缩小视图,您可能还需要处理一些奇怪的 UI 事件(即,TextViews 在过渡时可能会表现得很有趣),但我并没有遇到太多麻烦来修补它们并保持整洁。

祝你好运!希望这可以帮助。

于 2013-09-30T15:59:43.233 回答
0

使用以下代码。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">

    <scale
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="0.0"
        android:interpolator="@android:anim/linear_interpolator"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>
于 2013-10-01T10:51:49.697 回答