1

I have an ImageView, and I have a ScaleAnimation.

           ScaleAnimation scaleAnimation =
           new ScaleAnimation(1.0f, 5f, 1.0f, 5f,
           ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
           ScaleAnimation.RELATIVE_TO_SELF, 0.30f);

           scaleAnimation.setDuration(9000);

           ImageView lol = (ImageView) findViewById(R.id.imageView1);

           lol.setImageResource(R.drawable.img1);
           lol.setAnimation(scaleAnimation);

Works great and everything, but I would really like the user to be able to decide what part of the image gets zoomed in on. Is there any way to convert touch coordinates to a pivot value?

Thanks!

  • 2
    Why not just use Animation.ABSOLUTE pivot values? You should be able to just pipe the MotionEvent.getX/Y() values directly from an OnTouchListener into the animation. – alanv Oct 28 '14 at 00:34

1 Answers1

1

Like alanv is suggesting, you can use an OnTouchListener on your ImageView to get the touch cordinates and pass those values to the scale animation. Like this:

        lol.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(final View v, MotionEvent event) {
            ScaleAnimation scaleAnim = scaleAnimation;
            Log.i(TAG, "x:"+event.getX() + ", y:"+ event.getY());
            startScaleAnimation(v, scaleAnim, event.getX()/v.getWidth(), event.getY()/v.getHeight());
            v.performClick();
            return true;
        }
    });

    //a method to execute your animation
    static void startScaleAnimation(View v, ScaleAnimation scaleAnim, float pivotX, float pivotY){
    scaleAnim =
            new ScaleAnimation(1.0f, 5f, 1.0f, 5f,
                    ScaleAnimation.RELATIVE_TO_SELF, pivotX,
                    ScaleAnimation.RELATIVE_TO_SELF, pivotY);
    scaleAnim.setDuration(4000);

    v.startAnimation(scaleAnim);
}

This will scale up your ImageView from the point the user touched.

joao2fast4u
  • 6,868
  • 5
  • 28
  • 42