在android中显示/隐藏带动画的视图

时间:2016-07-28 10:03:11

标签: android

我希望在用户触摸屏幕后隐藏并显示动画视图。但我不知道该怎么做。请问有人帮帮我吗?

我在这里得到了很多看法

int finalHeight = listView.getHeight(); 
ValueAnimator mAnimator = slideAnimator(finalHeight, 0);

然后隐藏该布局

ValueAnimator animator = ValueAnimator.ofInt(start, end);

    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            // Update Height
            int value = (Integer) valueAnimator.getAnimatedValue();

            ViewGroup.LayoutParams layoutParams = listView
                    .getLayoutParams();
            layoutParams.height = value;

            listView.setLayoutParams(layoutParams);
        }
    });
  

我想要这样的动画Here is Example

1 个答案:

答案 0 :(得分:1)

您的隐藏动画可能如下所示:

    ObjectAnimator hideAnim = ObjectAnimator.ofFloat(v, "translationY", 0, -v.getHeight());
    hideAnim.setDuration(300);

    hideAnim.addListener(new Animator.AnimatorListener() {
        @Override 
        public void onAnimationStart(Animator animation) {

        } 

        @Override 
        public void onAnimationEnd(Animator animator) {
            view.setVisibility(View.INVISIBLE);
        }

        @Override 
        public void onAnimationRepeat(Animator animation) {

        } 
    });

你的隐藏动画可能如下所示:

    ObjectAnimator unhideAnim = ObjectAnimator.ofFloat(v, "translationY", -v.getHeight, 0);
    unhideAnim.setDuration(300);

点击视图的听众:

v.setOnClickListener(new View.OnClickListener() {
    @Overrride
    public void onClick(View view) {
        if (v.getVisibility() == View.INVISIBLE) {
            v.setVisibility(View.VISIBLE)
            v.startAnimation(unhideAnim);
        } else {
            v.startAnimation(hideAnim);
        }
    }
});

仅当您的视图位于其视图组的顶部时才会起作用。