2013-08-21 203 views
2

我正在做一個android應用程序,它具有圖像縮放動畫控制與開始&停止按鈕(即)我有兩個按鈕與圖像。當我點擊開始時,圖像應該做縮放動畫。當我點擊停止,它應該停止動畫,並且如果我點擊開始,它應該從停止的位置繼續縮放。我該怎麼做? 以下是我的工作示例代碼:Android圖像縮放動畫

ImageView imageView; ScaleAnimation縮放; 動畫animset; float gg;

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    zoom = new ScaleAnimation(0, 1, 0, 1); 
    zoom.setDuration(3000); 

    imageView = (ImageView) findViewById(R.id.imageView1); 
    imageView.startAnimation(zoom); 

    ((Button) findViewById(R.id.start)).setOnClickListener(this); 
    ((Button) findViewById(R.id.stop)).setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.start: 
     zoom.setDuration((long) gg); 
     zoom.start(); 

     break; 
    case R.id.stop: 
     gg = zoom.getDuration(); 
     break; 

    } 

} 

} 謝謝。

回答

4

試試這個

ZOOM IN活動

public class ZoomInActivity extends Activity implements AnimationListener { 

    ImageView imgPoster; 
    Button btnStart; 

    // Animation 
    Animation animZoomIn; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_zoom_in); 

     imgPoster = (ImageView) findViewById(R.id.imgLogo); 
     btnStart = (Button) findViewById(R.id.btnStart); 

     // load the animation 
     animZoomIn = AnimationUtils.loadAnimation(getApplicationContext(), 
       R.anim.zoom_in); 

     // set animation listener 
     animZoomIn.setAnimationListener(this); 

     // button click event 
     btnStart.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // start the animation 
       imgPoster.startAnimation(animZoomIn); 
      } 
     }); 

    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     // Take any action after completing the animation 

     // check for zoom in animation 
     if (animation == animZoomIn) {   
     } 

    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onAnimationStart(Animation animation) { 
     // TODO Auto-generated method stub 

    } 

} 


activity_zoom_in_XML: 


<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#000000" 
    android:gravity="center"> 

    <ImageView android:id="@+id/imgLogo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/man_of_steel" 
     android:layout_centerInParent="true"/> 

    <Button android:id="@+id/btnStart" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Start Animation" 
     android:layout_marginTop="30dp" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="20dp"/> 

</RelativeLayout> 

化妝動畫文件夾中的資源和建立zoom_in.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fillAfter="true" > 

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

</set> 
+1

感謝您的answer.Im ok.But我需要停止和恢復在停止變焦位置。我該怎麼做 – sanjay

+0

我們怎樣才能從相同的圖像縮小? –

+0

謝謝你的答案。但縮小後如何去imageview – rams

1

容易和簡單

  • 創建文件夾RES/anim
  • 認沽動畫描述成RES /動畫文件夾
  • 集動畫圖像使用下面的代碼

    ImageView image=(ImageView)findViewById(R.id.imageId); 
    image.setAnimation(AnimationUtils.loadAnimation(this,R.anim.zoom_in_anim));