Android 按钮上的翻译动画有问题,未按预期响应
我是安卓系统的新手。我正在尝试翻译按钮上的动画。我在xml中添加了一个按钮,单击时,通过编程添加的另一个按钮会从当前位置移动到屏幕顶部并得到修复。但问题是,它不是从当前位置开始,而是从下方某个位置开始,而不是固定在顶部。如果可以的话,请有人帮助我。提前谢谢。 以下是我正在使用的代码:-Android 按钮上的翻译动画有问题,未按预期响应,android,translate-animation,Android,Translate Animation,我是安卓系统的新手。我正在尝试翻译按钮上的动画。我在xml中添加了一个按钮,单击时,通过编程添加的另一个按钮会从当前位置移动到屏幕顶部并得到修复。但问题是,它不是从当前位置开始,而是从下方某个位置开始,而不是固定在顶部。如果可以的话,请有人帮助我。提前谢谢。 以下是我正在使用的代码:- ll= (LinearLayout) findViewById(R.id.ll); final int width = this.getResources().getDisplayMetrics(
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>
我建议您在
/res/anim
文件夹中创建一个xml文件,并使用它来设置按钮的动画。带有android:fromYDelta
和android:toYDelta
的
标记作为其属性可能被证明是有用的(这两个属性都与按钮的垂直移动有关),并且在这里更容易,因为您的问题属于翻译动画问题的类别。如果你想考虑同样的问题,你可以找到一些好的解释。 < P>我建议你在你的代码>/RES/ANIM 文件夹中创建一个XML文件,并使用它来激活你的按钮。带有android:fromYDelta
和android:toYDelta
的
标记作为其属性可能被证明是有用的(这两个属性都与按钮的垂直移动有关),并且在这里更容易,因为您的问题属于翻译动画问题的类别。如果你想考虑同样的问题,你可能会找到一些好的解释。