Android 点击View炫酷动画

在这里插入图片描述

package view;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;

import androidx.annotation.Nullable;

public class MyImageView extends ImageView {
    public MyImageView(Context context) {
        super(context);
 //        必须设置setClickable(true), 要不然像ImageView 默认是点击不了的,它就收不到Action_Up 事件
//        如果一个view 的onTouchEvent 的Action_Down 返回false ,那么view的 Action_Up 是不会执行的
        this.setClickable(true);
    }

    public MyImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.setClickable(true);
    }

    public MyImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setClickable(true);
    }

    /**
     * 顶点判断【0:中间】【1:上】【2:右】【3:下】【4:左】
     **/
    private int pivot = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
//                getX() 和 getY() 获取的是距离自身的坐标位置
                startAnimDown(event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_UP:
                System.out.println("onTouchEvent :" + MotionEvent.ACTION_UP);
                endAnimal();
                break;

        }
        boolean b = super.onTouchEvent(event);
        System.out.println("boolean :" + b);
        return b;
    }
    /**
     * 顶点判断【0:中间】【1:上】【2:右】【3:下】【4:左】
     **/
    public OvershootInterpolator interpolator = new OvershootInterpolator(3f);
    public void startAnimDown(float touchX, float touchY) {
//        获取的是自身的相对坐标位置
        System.out.println("touchX :" + touchX + " touchY :" + touchY);
        if (width / 5 * 2 < touchX && touchX < width / 5 * 3 && height / 5 * 2 < touchY && touchY < height / 5 * 3) {
//            代表中心位置
            pivot = 0;
        } else if (touchX < width / 2 && touchY < height / 2) {
            if (touchX > touchY) {
                pivot = 1;// 代表用户点击的上面
            }else {
                pivot = 4; // 代表用户点击的左边
            }
        }else if (touchX < width / 2 && touchY > height / 2) {
            if (touchX < (height - touchY)) {//
                pivot = 4;// 代表用户点击左边
            }else {
                pivot = 3; // 代表用户点击下面
            }
        }else if (touchX > width / 2 && touchY < height / 2){
            if (width - touchX < touchY) {//点击右边
                pivot = 2;// 点击右边
            }else {
                pivot = 1; // 代表用户点击上面
            }
        }else if (touchX > width / 2 && touchY > height / 2){
            if (width - touchX > height - touchY) {//点击下面
                pivot = 3;//点击下面
            }else {
                pivot = 2; // 代表用户点击右边
            }
        }
//        上面这种算法只适合正方形的图片
        String anim = "";
        System.out.println("pivot :" + pivot);
        switch (pivot){
            case 0:
                startCenterSmallAnimal();
                return;
            case 1:
            case 3:
                anim = "rotationX";
                break;
            case 2:
            case 4:
                anim="rotationY";
                break;
        }
//        设置动画的基准点
        this.setPivotX(width/2);
        this.setPivotY(height/2);
//        【0:中间】【1:上】【2:右】【3:下】【4:左】
        if (pivot == 4 ){
            endRotationValue = -7f;
        }else if (pivot == 2){
            endRotationValue = 7f;
        }else if (pivot == 1){
            endRotationValue = 7f;
        }else if (pivot == 3){
            endRotationValue = -7f;
        }
        ObjectAnimator animObject = ObjectAnimator.ofFloat(this, anim, 0, endRotationValue)
                .setDuration(300);
        animObject.setInterpolator(interpolator);
        animObject.start();
    }

    private void startCenterSmallAnimal() {
        int tzStart = (int) this.getTranslationZ();
        this.setTag(tzStart);

        PropertyValuesHolder tz = PropertyValuesHolder.ofFloat("translationZ", tzStart, 0);
        PropertyValuesHolder tX = PropertyValuesHolder.ofFloat("scaleX", this.getScaleX(), 0.95f);
        PropertyValuesHolder tY = PropertyValuesHolder.ofFloat("scaleY", this.getScaleY(), 0.95f);
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(this,tz,tX,tY).setDuration(300);
        objectAnimator.setInterpolator(interpolator);
        objectAnimator.start();
    }
    private void startCenterBigAnimal() {
        int tzValue = (int) this.getTag();
        PropertyValuesHolder tz = PropertyValuesHolder.ofFloat("translationZ", 0, tzValue);
        PropertyValuesHolder tX = PropertyValuesHolder.ofFloat("scaleX", this.getScaleX(), 1f);
        PropertyValuesHolder tY = PropertyValuesHolder.ofFloat("scaleY", this.getScaleY(), 1f);
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(this,tz,tX,tY).setDuration(300);
        objectAnimator.setInterpolator(interpolator);
        objectAnimator.start();
    }

    //    结束动画
    public void endAnimal(){
        String anim = "";
        switch (pivot){
            case 0:
                startCenterBigAnimal();
                return;
//            【0:中间】【1:上】【2:右】【3:下】【4:左】
            case 1:
            case 3:
                anim = "rotationX";
                break;
            case 2:
            case 4:
                anim = "rotationY";
                break;
        }
        System.out.println("pivotUp :" + pivot);
        if (pivot == 2 || pivot == 4){
            startRotationValue = (int) this.getRotationY();
        }else {
            startRotationValue = (int) this.getRotationX();
        }
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this,anim,startRotationValue,0);
        objectAnimator.setDuration(300);
        objectAnimator.setInterpolator(interpolator);
        objectAnimator.start();
    }
    float endRotationValue = 0;
    int startRotationValue = 0;

    //    当宽高发生变化时进行的回调
    int height;
    int width;

    //    System.out: touchX :552.0touchY :492.0
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        System.out.println("width :" + w + " oldWidth :" + oldw);
        System.out.println("height :" + h + " oldHeight :" + oldh);
        width = w;
        height = h;
    }
}

下面是坐标位置
在这里插入图片描述// 【0:中间】【1:上】【2:右】【3:下】【4:左】的算法和参考的算法不一样
参考:https://blog.csdn.net/bamboy_/article/details/54342079

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值