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>