view触摸缩小,抬起恢复
使用方法
mBinding.view.setOnTouchListener(new ViewTouchUitl());
java代码
ViewTouchUitl
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.view.View;
import android.view.animation.ScaleAnimation;
import android.view.animation.Animation;
import android.view.animation.AccelerateDecelerateInterpolator;
public class TouchFeedback {
/**
* 缩放动画的单方面时长,假如是100,是指放大100,当手指离开缩小又是100.
*/
private int duration = 333;
/**
* 放大或者缩小的值
*/
private float aFloatBig = 0.9f;
/**
* 可以自己设定
*
* @param duration
* @param aFloatBig
*/
public void setDurationAndFloatBig(int duration, float aFloatBig) {
this.duration = duration;
this.aFloatBig = aFloatBig;
}
/**
* 私有构造
*/
private TouchFeedback() {
}
/**
* 定义回调的接口
*/
public interface TouchFeedbackListener {
void onFeedback(View view);
}
private static TouchFeedback touchFeedback = null;
public static TouchFeedback getTouchFeedbackInstance() {
if (touchFeedback == null) {
synchronized (TouchFeedback.class) {
if (touchFeedback == null) {
touchFeedback = new TouchFeedback();
}
}
}
return touchFeedback;
}
/**
* 恢复原形的动画
* @param v
* @param b
* @param touchFeedListener
*/
public void scaleSmall(final View v, boolean b) {
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(v, "ScaleX", aFloatBig, 1f).setDuration(duration);
ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(v, "ScaleY", aFloatBig, 1f).setDuration(duration);
objectAnimator2.start();
objectAnimator3.start();
if (b) {
/**动画执行的监听**/
objectAnimator2.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
/**
* 动画结束
* @param animation
*/
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
}
/**
* 变大的动画
* @param v
*/
public void scaleBig(View v) {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(v, "ScaleX", 1f, aFloatBig).setDuration(duration);
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(v, "ScaleY", 1f, aFloatBig).setDuration(duration);
objectAnimator.start();
objectAnimator1.start();
}
public void executeAnimation(View view) {
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.95f, 1.0f, 0.95f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(100);
scaleAnimation.setRepeatCount(1);
scaleAnimation.setRepeatMode(Animation.REVERSE);
scaleAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
view.startAnimation(scaleAnimation);
}
}
ViewTouchUitl
import android.view.MotionEvent;
import android.view.View;
public class ViewTouchUitl implements View.OnTouchListener {
/**
* 缩放动画的单方面时长,假如是100,是指放大100,当手指离开缩小又是100.
*/
private int duration = 100;
/**
* 放大或者缩小的值
*/
private float aFloatBig = 0.9f;
private TouchFeedback mTouchFeedback = TouchFeedback.getTouchFeedbackInstance();
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mTouchFeedback.scaleBig(v);
break;
case MotionEvent.ACTION_MOVE:
break ;
case MotionEvent.ACTION_UP:
if(isOutterUp(event,v)){
event.setAction(MotionEvent.ACTION_CANCEL);
return onTouch(v,event);
}
mTouchFeedback.scaleSmall(v, true);
v.performClick();
// v.setSelected(false);
break;
case MotionEvent.ACTION_CANCEL:
mTouchFeedback.scaleSmall(v, false);
break;
}
return true;
}
private boolean isOutterUp(MotionEvent event, View v) {
float touchX = event.getX();
float touchY = event.getY();
float maxX = v.getWidth();
float maxY = v.getHeight();
return touchX<0 || touchX>maxX || touchY < 0 || touchY > maxY;
}
}
kotlin代码
import android.animation.Animator
import android.animation.ObjectAnimator
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
/**
* @Description:
* @Author: JIULANG
* @Data: 2022/5/12 18:43
*/
object TouchFeedback {
/**
* 缩放动画的单方面时长,假如是100,是指放大100,当手指离开缩小又是100.
*/
private var duration = 333L
/**
* 放大或者缩小的值
*/
private var aFloatBig = 0.9f
/**
* 可以自己设定
*
* @param duration
* @param aFloatBig
*/
fun setDurationAndFloatBig(duration: Long, aFloatBig: Float) {
this.duration = duration
this.aFloatBig = aFloatBig
}
/**
* 定义回调的接口
*/
interface TouchFeedbackListener {
fun onFeedback(view: View?)
}
/**
* 恢复原形的动画
* @param v
* @param b
* @param touchFeedListener
*/
fun scaleSmall(v: View?, b: Boolean) {
val objectAnimator2 = ObjectAnimator.ofFloat(v, "ScaleX", aFloatBig, 1f).setDuration(
duration
)
val objectAnimator3 = ObjectAnimator.ofFloat(v, "ScaleY", aFloatBig, 1f).setDuration(
duration
)
objectAnimator2.start()
objectAnimator3.start()
if (b) {
/**动画执行的监听 */
objectAnimator2.addListener(object : Animator.AnimatorListener {
override fun onAnimationStart(animation: Animator?) {}
/**
* 动画结束
* @param animation
*/
override fun onAnimationEnd(animation: Animator?) {}
override fun onAnimationCancel(animation: Animator?) {}
override fun onAnimationRepeat(animation: Animator?) {}
})
}
}
/**
* 变大的动画
* @param v
*/
fun scaleBig(v: View?) {
val objectAnimator = ObjectAnimator.ofFloat(v, "ScaleX", 1f, aFloatBig).setDuration(
duration.toLong()
)
val objectAnimator1 = ObjectAnimator.ofFloat(v, "ScaleY", 1f, aFloatBig).setDuration(
duration.toLong()
)
objectAnimator.start()
objectAnimator1.start()
}
fun executeAnimation(view: View) {
val scaleAnimation = ScaleAnimation(
1.0f,
0.95f,
1.0f,
0.95f,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f
)
scaleAnimation.duration = 100
scaleAnimation.repeatCount = 1
scaleAnimation.repeatMode = Animation.REVERSE
scaleAnimation.interpolator = AccelerateDecelerateInterpolator()
view.startAnimation(scaleAnimation)
}
}
import android.view.MotionEvent
import android.view.View
/**
* @Description:
* @Author: JIULANG
* @Data: 2022/5/12 18:44
*/
object TouchFeedbackUitl : View.OnTouchListener {
/**
* 缩放动画的单方面时长,假如是100,是指放大100,当手指离开缩小又是100.
*/
private val duration = 100
/**
* 放大或者缩小的值
*/
private val aFloatBig = 0.9f
private val mTouchFeedback= TouchFeedback
override fun onTouch(v: View, event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> mTouchFeedback.scaleBig(v)
MotionEvent.ACTION_MOVE -> {
}
MotionEvent.ACTION_UP -> {
if (isOutterUp(event, v)) {
event.action = MotionEvent.ACTION_CANCEL
return onTouch(v, event)
}
mTouchFeedback.scaleSmall(v, true)
v.performClick()
}
MotionEvent.ACTION_CANCEL -> mTouchFeedback.scaleSmall(v, false)
}
return true
}
private fun isOutterUp(event: MotionEvent, v: View): Boolean {
val touchX = event.x
val touchY = event.y
val maxX: Int = v.width
val maxY: Int = v.height
return touchX < 0 || touchX > maxX || touchY < 0 || touchY > maxY
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。