在Imageview上缩放动画

时间:2017-06-29 04:12:35

标签: android android-animation android-imageview

如何为我的ImageView设置动画,就像下面gif中FloatingActionButton的动画(显示和隐藏)一样。

FAB animating on RecylerView scroll

2 个答案:

答案 0 :(得分:4)

您可以使用放大/缩小动画来实现此目的。

scale_up.xml

    <set xmlns:android="http://schemas.android.com/apk/res/android">
       <scale
         android:duration="100"
         android:fromXScale="0"
         android:fromYScale="0"
         android:pivotX="50%"
         android:pivotY="50%"
         android:toXScale="1.0"
         android:toYScale="1.0" />
    </set>

scale_down.xml

   <set xmlns:android="http://schemas.android.com/apk/res/android">
       <scale
         android:duration="100"
         android:fromXScale="1.0"
         android:fromYScale="1.0"
         android:pivotX="50%"
         android:pivotY="50%"
         android:toXScale="0"
         android:toYScale="0" />
 </set>

要将动画应用到imageView,您可以执行以下操作:

    /**
    * For scale up animation
    */
    Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.scale_up);
        child.startAnimation(animation);
        child.setVisibility(View.VISIBLE);

缩小

    /**
    * For scale down animation
    */
    Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.scale_down);
        child.startAnimation(animation);
        child.setVisibility(View.INVISIBLE);

这取决于您希望放大和缩小imageView的位置。

答案 1 :(得分:0)

使用FloatingActionButton作为您的父布局。 为public class FabBehaviour extends FloatingActionButton.Behavior { private static final String TAG = FabBehaviour.class.getSimpleName(); int preDX = 0; int preFinal = 0; @Override public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target) { Log.d(TAG, "Stop"); child.show(); super.onStopNestedScroll(coordinatorLayout, child, target); } public FabBehaviour(Context context, AttributeSet attrs) { super(); } @Override public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) { if (ViewCompat.SCROLL_AXIS_NONE == nestedScrollAxes) { child.show(); } return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes); } @Override public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed); preFinal = dyConsumed; if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE && preDX < dyConsumed) { child.hide(); } else if (dyConsumed < -5 && child.getVisibility() != View.VISIBLE) { child.show(); } preDX = dyConsumed; } }

创建滚动行为

将此课程用于滚动行为

app:layout_behavior="<package name>.FabBehaviour"

现在将此标记添加到&#39; FlotingActionButton&#39;在你的布局上

{{1}}