如何在Android中的触摸比例动画上应用imageView?

时间:2018-10-31 07:24:28

标签: java android android-animation ontouchlistener objectanimator

我想每当用户触摸按钮时应用一些动画,例如-

  
      
  1. 触摸/悬停时=缩小比例,
  2.   
  3. 将手指移出视图边界=放大到先前的位置,
  4.   
  5. 释放/点击=放大到先前的位置
  6.   

在触摸动画上效果很好,但是当手指松开时,我无法确定将动画放大到以前的位置。另外,我对Motion触摸事件不太熟悉,因此请告诉我哪个Motion Event适合执行此操作。谁能帮帮我吗。这是我的代码:

imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {

                    case MotionEvent.ACTION_DOWN:
                        imageView.animate().scaleX(0.5f).scaleY(0.5f).start();
                        break;

                    case MotionEvent.ACTION_CANCEL:
                        imageView.animate().scaleX(2.0f).scaleY(2.0f).start();
                        break;

                    case MotionEvent.ACTION_UP:
                        imageView.animate().scaleX(2.0f).scaleY(2.0f).start();

                        startActivity(new Intent(ActivityOne.this, ActivityTwo.class));
                        break;
                }
                return true;
            }
        });

我还尝试了对象动画师,诸如此类:

private void scaleAnimation(View view) {
        ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(view, "scaleX", 0.5f);
        ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(view, "scaleY", 0.5f);
        scaleDownX.setDuration(150);
        scaleDownY.setDuration(150);
        //scaleDownX.setRepeatMode(ValueAnimator.REVERSE);
        //scaleDownY.setRepeatMode(ValueAnimator.REVERSE);
        //scaleDownX.setRepeatCount(1);
        //scaleDownY.setRepeatCount(1);

        animatorSet = new AnimatorSet();
        //animatorSet.play(scaleDownX).with(scaleDownY);
        animatorSet.playTogether(scaleDownX, scaleDownY);
        animatorSet.start();
}

1 个答案:

答案 0 :(得分:0)

经过长时间的实验,我自己找到了答案。在这里:

  

触摸后,如果用户移动手指,则需要取消按钮点击

boolean isMove;
// initialize
isMove = false;
  

设置缩小比例和放大动画

private void startScaleAnimation(View view) {
        ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(view, "scaleX", 0.8f);
        ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(view, "scaleY", 0.8f);
        scaleDownX.setDuration(150);
        scaleDownY.setDuration(150);

        scaleDownX.start();
        scaleDownY.start();
    }

    private void cancelScaleAnimation(View view) {
        ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(view, "scaleX", 1.0f);
        ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(view, "scaleY", 1.0f);
        scaleDownX.setDuration(150);
        scaleDownY.setDuration(150);

        scaleDownX.start();
        scaleDownY.start();
    }
  

现在,将此动画与onTouch侦听器一起应用

imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {

                    case MotionEvent.ACTION_DOWN:
                        startScaleAnimation(imageView);
                        break;

                    case MotionEvent.ACTION_MOVE:
                        cancelScaleAnimation(imageView);
                        isMove =true;
                        break;

                    case MotionEvent.ACTION_UP:
                        cancelScaleAnimation(imageView);

                        if (!isMove){
                            isMove = false;
                            Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {

                                    startActivity(new Intent(ActivityOne.this, ActivityTwo.class));

                                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                        overridePendingTransition(R.anim.enter_slide_left, R.anim.exit_slide_left);
                                    }

                                }
                            },150);
                        }else {
                            isMove = false;
                        }

                        break;
                }
                return true;
            }
        });