Is it possible to set animation pivot value based on touch event?

797 views Asked by At

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!

1

There are 1 answers

0
joao2fast4u On BEST ANSWER

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.