2013-03-17 199 views
7

如何設置放大和縮小當點擊imageview?我想我的程序作出反應,當用戶點擊imageview必須在一定程度上變大,並可以移動imageview上該屏幕有時可以隨着屏幕上任何位置的移動而減小尺寸。當再次點擊時,將恢復原來的尺寸我該怎麼做?動畫放大和縮小android的imageview

回答

14

據我所知有兩種方法。

第一種方式:

請在水庫被稱爲「動畫」一個新的文件夾。比內部製作一個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

+0

非常感謝 – 2016-06-17 10:10:56