2012-11-06 49 views
4

我試圖通過單擊自身旋轉的ImageButton的360,但它並沒有我使用的XML文件中像這樣的工作我想通過點擊它將圖像按鈕旋轉到360,但它不能旋轉它?

..

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="72dp" 
     android:layout_height="72dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="83dp" 
     android:layout_marginTop="103dp" 
     android:clickable="true" 
     android:src="@drawable/cute" /> 
</RelativeLayout> 

和Java代碼這樣的...

package com.example.testapplication; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.animation.RotateAnimation; 
import android.widget.ImageButton; 

public class MainActivity extends Activity implements OnClickListener { 


    ImageButton img; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     img = (ImageButton) findViewById(R.id.imageView1); 



    } 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     RotateAnimation ra =new RotateAnimation(0, 360); 
     ra.setFillAfter(true); 
     ra.setDuration(0); 

     img.startAnimation(ra); 

    } 

} 

我的應用程序的目的是使用9張圖片BTN發展和益智遊戲找到....

+0

笑納答案如果解決 – 2013-02-15 09:11:43

回答

7

你的代碼工作得很好。問題是:您將持續時間設置爲0.您無法看到它發生,因爲如果您旋轉360度,其結束位置和開始位置將是相同的。因此設置持續時間1000(一秒)或更長時間以查看實際發生的動畫。

只是嘗試:

ra.setDuration(1000); 

而且你還沒有設置IMG的onclick監聽器。

只是嘗試:

img .setOnClickListener(new OnClickListener() { 

    public void onClick(View arg0) { 
     //animation here 
    } 
}); 
+0

首先,我想說,謝謝你,我已經嘗試設置持續時間1000,但它不會工作... – CrazyMind

+0

也不要忘記添加: img.setOnclickListener (<你的onClickListener這裏>) – Ercan

+0

非常感謝你... – CrazyMind

4

這是我用來在一個小應用程序中旋轉的代碼。

public class MainScreen extends Activity{ 
    ImageView ivDH = null; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.screen); 
     ivDH = (ImageView) findViewById(R.id.dollhouse); 

     ivDH.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View arg0, MotionEvent arg1) { 
       // TODO Auto-generated method stub 

       startAnimation(ivDH); 
       return false; 
      } 
     }); 
    } 
    public void startAnimation(ImageView ivDH) 
    { 
     System.out.println("Inside startAnimation()"); 
     Animation scaleAnim = new ScaleAnimation(0, 2, 0, 2); 
     scaleAnim.setDuration(5000); 
     scaleAnim.setRepeatCount(1); 
     scaleAnim.setInterpolator(new AccelerateInterpolator()); 
     scaleAnim.setRepeatMode(Animation.REVERSE); 

     Animation rotateAnim = new RotateAnimation(0, 360, Animation.ABSOLUTE, Animation.ABSOLUTE, Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF); 
     rotateAnim.setDuration(5000); 
     rotateAnim.setRepeatCount(1); 
     rotateAnim.setInterpolator(new AccelerateInterpolator()); 
     rotateAnim.setRepeatMode(Animation.REVERSE); 

     AnimationSet animationSet = new AnimationSet(true); 
     animationSet.addAnimation(scaleAnim); 
     animationSet.addAnimation(rotateAnim); 

     ivDH.startAnimation(animationSet); 
    } 
} 

你將不得不改變一些參數。希望它有幫助

+0

感謝SAS,但仍然無法旋轉的ImageButton .... – CrazyMind

+0

我在Android應用程序更新,如果你有可以顯示完整的代碼PLS告訴我它將如何修復..... – CrazyMind

+0

有什麼問題?它會拋出任何異常嗎?或者它根本不旋轉?或者它的旋轉,但不是你想旋轉的方式? –

相關問題