图像视图上的缩放动画

4
我该如何使我的ImageView动态显示和隐藏,就像下面GIF中的FloatingActionButton一样。

FAB animating on RecylerView scroll


可能在这篇帖子中已经得到了答案。 - Emiliano
可能是在Android中将gif图像添加到ImageView的重复问题。 - Emiliano
1
抱歉,这对我没有帮助。我的问题是如何在我的ImageView上放置弹出动画,而不是GIF。 - Poison_X
请更改您的示例,使用一个带有gif的示例。 - Emiliano
你说的动画是指什么?视频吗? - Emiliano
2个回答

6
您可以使用缩放动画来实现这一点。 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放大或缩小的位置。


0

使用CoordinatorLayout作为您的父布局。 为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;
} }

现在将此标签添加到您的布局中的'FloatingActionButton'内

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

OP想要像GIF中FAB的缩放一样,为他的ImageView动态设置可见性 - Sufian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接