Home>

There are two images iv1 and iv2 on Framelayout. iv1 supports pitch in/out and scales. However, iv2 cannot handle pitch in/out due to animation. Somehow, I want to support pitch-in/out during the animation, what should I do?

I also want to synchronize the positional relationship between iv1 and iv2 if there is room.

Source code
private ScaleGestureDetector detector;
    private float scale = 1f;
    private ImageView imageView1;
    private ImageView imageView2;
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        imageView1 = findViewById (R.id.imageview1);
        imageView2 = findViewById (R.id.imageview2);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat (imageView2, "translationX", 0f, 1000f);
        objectAnimator.setDuration (1000);
        objectAnimator.setRepeatCount (ValueAnimator.INFINITE);
        objectAnimator.start ();
        detector = new ScaleGestureDetector (this, new ScaleListener ());
    }
    public boolean onTouchEvent (MotionEvent event) {
        detector.onTouchEvent (event);
        return super.onTouchEvent (event);
    }
    private class ScaleListener
            extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale (ScaleGestureDetector detector) {
            scale * = detector.getScaleFactor ();
            imageView1.setScaleX (scale);
            imageView1.setScaleY (scale);
            return true;
        }
    }
  • Answer # 1

    I could do what I wanted to do if I was messed up. Please use it for your coding.

    private ScaleGestureDetector detector;
        private float scale = 1f;
        private ImageView imageView1;
        private ImageView imageView2;
        private ValueAnimator Animator;
        @Override
        protected void onCreate (Bundle savedInstanceState) {
            super.onCreate (savedInstanceState);
            setContentView (R.layout.activity_main);
            imageView1 = findViewById (R.id.imageview1);
            imageView2 = findViewById (R.id.imageview2);
            Animator = ObjectAnimator.ofFloat (imageView2, "translationX", 100f, 500f);
            Animator.setDuration (10000);
            Animator.setInterpolator (new LinearInterpolator ());
            Animator.setRepeatCount (ValueAnimator.INFINITE);
            Animator.start ();
            detector = new ScaleGestureDetector (this, new ScaleListener ());
        }
        public boolean onTouchEvent (MotionEvent event) {
            detector.onTouchEvent (event);
            return super.onTouchEvent (event);
        }
        private class ScaleListener
                extends ScaleGestureDetector.SimpleOnScaleGestureListener {
            @Override
            public boolean onScale (ScaleGestureDetector detector) {
                scale * = detector.getScaleFactor ();
                imageView1.setLayoutParams (new FrameLayout.LayoutParams ((int) (scale * 1000), (int) (scale * 1000)));
                imageView2.setScaleY (scale);
                imageView2.setScaleX (scale);
                imageView2.setY ((imageView2.getHeight () * scale-imageView2.getHeight ())/2);
                long time = Animator.getCurrentPlayTime ();
                float w = (imageView2.getWidth () * scale-imageView2.getWidth ())/2;
                Animator = ObjectAnimator.ofFloat (imageView2, "translationX", 100f * scale + w, 500f * scale + w);
                Animator.setInterpolator (new LinearInterpolator ());
                Animator.setDuration (10000);
                Animator.setRepeatCount (ValueAnimator.INFINITE);
                Animator.setCurrentPlayTime (time);
                Animator.start ();
                return true;
            }
        }

Related articles