布局文件代码如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:id="@+id/btm_Test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="测试"/>
</RelativeLayout>
Java代码如下
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_Test = (Button)findViewById(R.id.btm_Test);
btn_Test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 200);
animation.setInterpolator(new AccelerateInterpolator());
animation.setDuration(1000);
final Button ll = (Button)v;
animation.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationEnd(Animation animation) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ll.getLayoutParams();
params.topMargin = 200;
ll.setLayoutParams(params);
}
public void onAnimationRepeat(Animation animation) {}
});
ll.startAnimation(animation);
}
});
}
}
animation.setDuration(1000);后面加一个animation.setFillAfter(true);试试??闪烁的原因应该是你的动画结束了,然后回到了原来的位置,然后你又改变了控件的位置,这样一来一回的闪烁吧,setFillAfter(true)表示动画结束之后留在最后的位置,你试试看,我猜的,嘿嘿
我试过了,如果加上setFillAfter(true)这句话,那最终的效果是控件移动到200的位置后,又会往下在跳200,而且按钮实际上在第一个200的位置上,只是看不到。
改成ValueAnimator属性动画吧,TranslateAnimation 我也出过很多问题,不太想用它。