2015-07-13 53 views
1

我是Android的新手。我正在嘗試翻譯按鈕上的動畫。我在xml中添加了一個按鈕,點擊後,另一個按程序添加的按鈕從其當前位置移動到屏幕頂部並被修復。但問題是,它不是從目前的位置開始,而是從下面的某個位置開始,而不是固定在頂端。請有人幫助我,如果他們可以。提前致謝。 這裏是我的代碼,我使用: -按鈕上的翻譯動畫有問題,未按預期做出響應

ll= (LinearLayout) findViewById(R.id.ll); 
    final int width = this.getResources().getDisplayMetrics().widthPixels; 
    final int height= this.getResources().getDisplayMetrics().heightPixels; 


    final Button btn=new Button(MainActivity.this); 
    btn.setText("Message"); 
    btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    btn.setX(width/3); 
    btn.setY(height/3); 
    ll.addView(btn); 

    Button btn_click= (Button) findViewById(R.id.buttonbottom); 
    btn_click.setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
    Animation animate= new TranslateAnimation(btn.getX(),btn.getX(), btn.getY(), 0); 
    animate.setDuration(7000); 
    btn.startAnimation(animate); 
    } 
}); 
} 

和它的XML是: -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/ll" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<Button 
    android:id="@+id/buttonbottom" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Click Me" /> 
    </LinearLayout> 

回答

0

我建議你在你的/res/anim文件夾中創建一個XML文件,並用它來動畫的按鈕。具有android:fromYDeltaandroid:toYDelta作爲其屬性的<translate>標記可能被證明是有幫助的(這兩個屬性都與垂直按鈕的移動有關),並且在這裏更容易,因爲您的問題屬於翻譯動畫問題的類別。如果你想考慮一樣的話,你可能會找到一些很好的解釋here