Android property animation: how to increase view height?

后端 未结 7 2181
不知归路
不知归路 2020-12-07 20:17

How to increase the view height using Property Animations in Android?

ObjectAnimator a = ObjectAnimator.ofFloat(viewToIncreaseHeight, \"translationY\", -100)         


        
相关标签:
7条回答
  • 2020-12-07 20:28

    For kotlin you can use this method

    private fun increaseViewSize(view: View, increaseValue: Int) {
        val valueAnimator =
            ValueAnimator.ofInt(view.measuredHeight, view.measuredHeight + increaseValue)
        valueAnimator.duration = 500L
        valueAnimator.addUpdateListener {
            val animatedValue = valueAnimator.animatedValue as Int
            val layoutParams = view.layoutParams
            layoutParams.height = animatedValue
            view.layoutParams = layoutParams
        }
        valueAnimator.start()
    }
    

    It will increase view's size as defined in parameter

    based on @Minion answer

    0 讨论(0)
  • 2020-12-07 20:30

    This is not a direct answer to this question. However, it may help someone.

    Sometimes, we want to increase/decrease view's height because some of its child is is being added/removed (or just becoming visible/gone).

    On those cases, you really can use Android default animation. As mentioned in other answers, you can set via:

    <LinearLayout
        ...
        android:animateLayoutChanges="true"
    .../>
    

    Or in Java:

    linearLayout.setLayoutTransition(new LayoutTransition());
    

    If you created a custom view:

    public StickerPickerView(final Context context, final AttributeSet attrs,
            final int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayoutTransition(new LayoutTransition());
    }
    

    For me, it was pretty satisfactory but I just tested on latest APIs. That will automatically add a fade in/out when your view becomes visible. It will also animate the height/width of your view when same changes (like when you change the visibility of one of the child views etc).

    So, I recommend to at least give a try.

    0 讨论(0)
  • 2020-12-07 20:33

    Use this method in kotlin:

    private fun increaseViewSize(view: View) {
        val valueAnimator = ValueAnimator.ofInt(view.measuredHeight, view.measuredHeight+20)
        valueAnimator.duration = 500L
        valueAnimator.addUpdateListener {
            val animatedValue = valueAnimator.animatedValue as Int
            val layoutParams = view.layoutParams
            layoutParams.height = animatedValue
            view.layoutParams = layoutParams
        }
        valueAnimator.start()
    }
    
    0 讨论(0)
  • 2020-12-07 20:38

    It's pretty straight forward as you can see below.

    <LinearLayout android:id="@+id/container"
    android:animateLayoutChanges="true"
    .../>
    

    More info below:

    https://developer.android.com/training/animation/layout

    0 讨论(0)
  • 2020-12-07 20:42

    Here is my code to animate scrolling an item to center of the screen inside a recycleView

    // Scrolling with animation
    val valueAnimator = ValueAnimator.ofInt(getWidth() / 2)
    valueAnimator.duration = 250L
    valueAnimator.addUpdateListener {
        val animatedValue = valueAnimator.animatedValue as Int
        (layoutManager as LinearLayoutManager).scrollToPositionWithOffset(itemToScroll, animatedValue)
    }
    valueAnimator.start()
    // End of scrolling with animation
    

    Note it is a bit similar to UIView.animate in Swift, but with more complication

    0 讨论(0)
  • 2020-12-07 20:50

    You can use ViewPropertyAnimator, which can save you some lines of code :

    yourView.animate()
       .scaleY(-100f)
       .setInterpolator(new AccelerateDecelerateInterpolator())
       .setDuration(1000);
    

    that should be all you need, be sure to check the documentation and all available methods for ViewPropertyAnimator.

    0 讨论(0)
提交回复
热议问题