下拉动画 [英] Drop-down animation

查看:80
本文介绍了下拉动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个下拉动画。当用户点击指定按钮 @ + id / btnToHideView 时,我想要一个视图 @ + id / relativeLayoutControls 下降/向上滑动(可见 / 消失)。

I am trying to create a drop-down animation. When the user taps on a specified button @+id/btnToHideView, I would like a view @+id/relativeLayoutControls to drop down/slide up (VISIBLE / GONE).

这里是布局文件的外观:

Here is how the layout file looks:

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

    <Button
        android:id="@+id/btnToHideView"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/hide_btn"
        />

    <RelativeLayout
        android:id="@+id/relativeLayoutControls"
        android:layout_width="60dp"
        android:layout_height="fill_parent"
        android:layout_marginRight="6dp"
        android:layout_marginEnd="6dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        //I have buttons in this layoout

    </RelativeLayout>

</LinearLayout>

这就是我尝试过的:

我在 res / anim

slide_down.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>

slide_up.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>

然后我尝试通过以下方式处理此问题:

Then I tried handling this by doing:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    controlsHide = (RelativeLayout) findViewById(R.id.relativeLayoutControls);

    final Animation slide_down = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.slide_down);

    final Animation slide_up = AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.slide_up);

    btnToHideView = (Button) findViewById(R.id.btnToHideView);

    btnToHideView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            //I just did slide_up to test if its working
            controlsHide.startAnimation(slide_up);

        }
    });

我关注了这篇文章,但是当我点击按钮时,什么也没有发生。在logcat中,它仅显示 ACTION_DOWN

I followed this post, but when I tap on the button nothing happens. In logcat, it only prints ACTION_DOWN.

推荐答案

请尝试以下操作:

幻灯片:

Animation slideUp = AnimationUtils.loadAnimation(activity, R.anim.slide_up);
view.startAnimation(slideUp);

slide_up.xml

slide_up.xml

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

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

view.animate().translationY(0);

下拉列表:

view.animate().translationY(view.getHeight());

这篇关于下拉动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆