安卓(Android) 怎么设置触摸缩放动画(ScaleAnimation)

2019年10月15日21:39:40安卓(Android) 怎么设置触摸缩放动画(ScaleAnimation)已关闭评论 296 898字阅读2分59秒

1.简介

安卓(Android) 怎么设置触摸缩放动画(ScaleAnimation)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import android.view.MotionEvent;
import android.view.View;
import android.view.animation.ScaleAnimation;

public class Main {
    public static void setOnTouchScaleAnimation(View targetView,
            final float scaleX, final float scaleY) {
        targetView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                final int action = event.getAction();
                switch (action) {
                case MotionEvent.ACTION_DOWN: {
                    ScaleAnimation anim = new ScaleAnimation(1.0f, scaleX,
                            1.0f, scaleY, v.getWidth() / 2,
                            v.getHeight() / 2);
                    anim.setDuration(60);
                    anim.setFillEnabled(true);
                    anim.setFillAfter(true);
                    v.startAnimation(anim);
                    break;
                }
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP: {
                    ScaleAnimation anim = new ScaleAnimation(scaleX, 1.0f,
                            scaleY, 1.0f, v.getWidth() / 2,
                            v.getHeight() / 2);
                    anim.setDuration(100);
                    v.startAnimation(anim);
                    break;
                }
                }
                return false;
            }
        });
    }
}

 
继续阅读
编程十万个怎么办