如何在单击imageview时设置放大和缩小?我希望我的程序在用户点击imageview时做出反应必须在某种程度上变大,并且可以在该屏幕上移动imageview,有时它会在触摸时移动缩小尺寸在屏幕上的任何地方.再次点击是恢复原始大小我该怎么办?
Iks*_*eto 14
据我所知,有两种方法.
第一种方式:
在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>
Run Code Online (Sandbox Code Playgroud)
另一个用于缩小,但具有反转值.
<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>
Run Code Online (Sandbox Code Playgroud)
您可以根据需要更改值.我认为它们是不言自明的.
现在在你的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);
Run Code Online (Sandbox Code Playgroud)
现在你只需要跟踪哪个是当前状态.并为每个州执行以下代码行:
imageView.startAnimation(zoomin);
Run Code Online (Sandbox Code Playgroud)
和
imageView.startAnimation(zoomout);
Run Code Online (Sandbox Code Playgroud)
例如:
imageView.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
if(!pressed) {
v.startAnimation(zoomin);
pressed = !pressed;
} else {
v.startAnimation(zoomout);
pressed = !pressed;
}
}
});
Run Code Online (Sandbox Code Playgroud)
另一种方法在这里描述:http://developer.android.com/training/animation/zoom.html.