1

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

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

谢谢!

4

1 回答 1

1

就像 alanv 建议的那样,您可以使用 anOnTouchListenerImageView获取触摸坐标并将这些值传递给缩放动画。像这样:

        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用户触摸的点扩大您的规模。

于 2014-10-28T11:10:18.743 回答