6

单击imageview时如何设置放大和缩小?我希望我的程序在用户单击imageview时做出反应,必须在一定程度上变大并且可以在该屏幕上移动imageview,有时它会在触摸时缩小尺寸屏幕上的任何地方。再次单击时恢复原始大小我该怎么办?

4

2 回答 2

15

据我所知有两种方法。

第一种方式:

在 res 中创建一个名为“anim”的新文件夹。比在里面创建一个 xml 文件,例如 zoomin.xml。然后将以下代码放入其中。

<scale  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromXScale="1" 
  android:toXScale="5" 
  android:fromYScale="1" 
  android:toYScale="5" 
  android:pivotX="50%" 
  android:pivotY="50%" 
  android:duration="1000" 
  android:fillAfter="true">
</scale>

再做一个缩小,但值相反。

<scale  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromXScale="5" 
  android:toXScale="1" 
  android:fromYScale="5" 
  android:toYScale="1" 
  android:pivotX="50%" 
  android:pivotY="50%" 
  android:duration="1000" 
  android:fillAfter="true">
</scale>

您可以根据需要更改这些值。我认为它们是不言自明的。

现在在你的java代码中。

ImageView imageView = (imageView)findViewById(R.id.yourImageViewId);

Animation zoomin = AnimationUtils.loadAnimation(this, R.anim.zoomin);
Animation zoomout = AnimationUtils.loadAnimation(this, R.anim.zoomout);
imageView.setAnimation(zoomin);
imageView.setAnimation(zoomout);

现在您只需要跟踪哪个是当前状态。并且对于每个状态执行这行代码:

imageView.startAnimation(zoomin);

imageView.startAnimation(zoomout);

例如:

imageView.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                if(!pressed) {
                    v.startAnimation(zoomin);
                    pressed = !pressed;
                } else {
                    v.startAnimation(zoomout);
                    pressed = !pressed;
                }
            }
        });

另一种方式在这里描述:http: //developer.android.com/training/animation/zoom.html

于 2013-07-13T09:15:26.623 回答
0

您可以按照本指南轻松完成此操作

http://developer.android.com/training/animation/zoom.html

很快你应该使用第三个图像视图,当用户触摸你想要的任何图像视图时它是不可见的,你可以通过在不可见的图像视图中使用动画来显示它。

于 2015-03-11T13:59:43.093 回答