2013-02-05 31 views
0

我有我的活動按鈕,使用TranslateAnimation改變其位置正確的結果。

我使用anim.setFillAfter(真);爲了保留最終的翻譯結果,按鈕會改變其位置,但觸摸區域保持不變。
現在,當我觸摸按鈕它不接觸,而在按鈕的前一個位置的工作要求其聽衆..
翻譯鈕釦不給

這裏是我的代碼:
MainActivity.java

public class MainActivity extends Activity { 

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

     Animation anim=new TranslateAnimation(0, 100, 0, 0); 
     anim.setDuration(500); 
     anim.setFillAfter(true); 
     Button mybtn = (Button)findViewById(R.id.btn); 
     mybtn.startAnimation(anim); 
    } 

    public void myFunc(View v){ 
     Toast.makeText(this, "btn clicked", Toast.LENGTH_SHORT).show(); 
    } 
} 

activity_main.xml中

<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" 
      tools:context=".MainActivity" > 

      <Button 
       android:id="@+id/btn" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:text="@string/hello_world" 
       android:onClick="myFunc"/> 

     </RelativeLayout> 
+0

你能嘗試刪除動畫和嘗試? – lokoko

+0

我想要按鈕動畫到其他位置,並在那裏執行其點擊屬性。 –

回答

0

我已重新生成您的情況,它好像只有50%的屁股上顯示所選顏色。是對的嗎 ?試試這個:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button sampleButton = (Button)findViewById(R.id.btn); 
    sampleButton.animate().translationX(somevalue).setDuration(someduration); 
} 
+0

mybtn.getWidth()在上面的代碼中返回0,如果它返回按鈕的寬度,那麼我也認爲點擊左邊的按鈕將會工作。 –

+0

獲取按鈕的寬度做: mybtn.getViewTreeObserver()addOnGlobalLayoutListener(新OnGlobalLayoutListener(){ 公共無效onGlobalLayout(){ 阿尼姆=新TranslateAnimation(0,mybtn.getWidth(),0,0) ; } } – lokoko

+0

http://stackoverflow.com/questions/5886624/animation-setfillafter-before-do-they-work-what-are-they-for,對於這個問題的答案說setFillAfter只適用於動畫?屬性,而不能查看屬性,我們怎樣才能改變一樣的動畫屬性視圖屬性是我的問題... –

1

我找到了答案,我的查詢在本頁面 vogellahere Android已經解釋過,按鈕需要的屬性動畫不只是觀看動畫:

這裏是代碼我用:

Button mybtn = (Button)findViewById(R.id.btn); 
mybtn.animate().translationX(400).setDuration(500); 

thanx您的幫助...... 我要離開這個職位,如果別人需要幫助。