【发布时间】:2015-07-14 21:32:49
【问题描述】:
我是安卓新手。我正在尝试在按钮上翻译动画。我在 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>
【问题讨论】:
标签: android translate-animation