【问题标题】:Scale animation on Imageview在 Imageview 上缩放动画
【发布时间】:2017-12-02 15:07:02
【问题描述】:

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

【问题讨论】:

  • 可能已在this 帖子中回答
  • 对不起,这对我没有帮助。我的问题是如何将弹出动画放在我的 imageview 而不是 gif 上。
  • 然后改变你的例子,你放一个带有gif的例子
  • 你的意思是动画......?视频?

标签: android android-animation android-imageview


【解决方案1】:

您可以使用放大/缩小动画来实现这一点。

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 在哪里放大和缩小。

【讨论】:

    【解决方案2】:

    使用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;
    } }
    

    现在在布局的“FlotingActionButton”内添加这个标签

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

    【讨论】:

    • OP 希望为他的ImageView 的可见性设置动画,就像 FAB 在 gif 中如何放大和缩小(当它变得可见和不可见时)一样。跨度>
    猜你喜欢
    • 2012-07-08
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    相关资源
    最近更新 更多