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>