是否可以根据触摸事件设置动画枢轴值?

时间:2014-10-27 22:00:54

标签: android animation imageview zoom scaleanimation

我有一个ImageView,我有一个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);

效果很好,但是我真的希望用户能够决定放大图像的哪个部分。有没有办法将触摸坐标转换为透视值?

谢谢!

1 个答案:

答案 0 :(得分:1)

就像alanv建议的那样,您可以在OnTouchListener上使用ImageView来触摸坐标,并将这些值传递给比例动画。像这样:

        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);
}

这会从用户触摸的位置放大ImageView