【问题标题】:Drop-down animation下拉动画
【发布时间】:2017-07-21 07:54:23
【问题描述】:

我正在尝试创建一个下拉动画。当用户点击指定按钮@+id/btnToHideView 时,我希望视图@+id/relativeLayoutControls 下拉/向上滑动(VISIBLE / GONE)。

这是布局文件的外观:

<?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创建了2个动画文件

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>

然后我尝试这样做:

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

        }
    });

我关注了this post,但是当我点击按钮时没有任何反应。在 logcat 中,它只打印ACTION_DOWN

【问题讨论】:

    标签: android animation drop-down-menu


    【解决方案1】:

    请试试这个:

    上滑:

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

    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());
    

    【讨论】:

    • view 替换为您的相关布局或按钮或您想要动画的任何视图。并将activity 替换为您的上下文。
    • 原来我的逻辑是错误的,但你帮我注意一下,谢谢。
    • 很高兴听到这个消息
    【解决方案2】:

    这是我的源代码实现(refer here),你可以使用它们

    // Initially hide/show the content view.
    redLayout = mView.findViewById(R.id.history_operation);
    //Load animation
    slide_down = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_down);
    slide_up = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up);
    
    • 为上下滑动创建动画 xml。如果您愿意,可以随意设置动画的持续时间。这里我设置了 500 毫:
    <translate
        android:duration="500"
        android:fromYDelta="-100%"
        android:toYDelta="0" />
    

    <translate
        android:duration="500"
        android:fromYDelta="0"
        android:toYDelta="-100%" />
    
    • 在源码中实现动画:

        slide_up.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
      
            }
      
            @Override
            public void onAnimationEnd(Animation animation) {
                //When the animation was finished, set gone to the view
                redLayout.setVisibility(View.GONE);
            }
      
            @Override
            public void onAnimationRepeat(Animation animation) {
      
            }
        });
        slide_down.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                //When the animation start, set visible to the view
                redLayout.setVisibility(View.VISIBLE);
            }
      
            @Override
            public void onAnimationEnd(Animation animation) {
            }
      
            @Override
            public void onAnimationRepeat(Animation animation) {
      
            }
        });
      

    最后调用toggle函数来启动定义好的动画

    private void toggle1() {
        // Start animation
        if(isFadeOut){
            redLayout.startAnimation(slide_down);
        }else {
            redLayout.startAnimation(slide_up);
        }
        isFadeOut = !isFadeOut;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-26
      • 2018-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-12
      • 2023-03-20
      • 2015-08-28
      • 1970-01-01
      相关资源
      最近更新 更多