我有2个按钮(开始和停止)和1个图像当我点击开始按钮图像将从上到下和从下到上移动,这很好,但当我点击停止按钮图像的问题消失但我不会.my要求图像必须在屏幕上暂停,直到我点击开始按钮。我在等你回复
请帮助我,一旦执行我的代码你就能明白了
public class MainActivity extends Activity {
RelativeLayout rl_footer;
ImageView iv_header;
boolean isBottom = true;
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rl_footer = (RelativeLayout) findViewById(R.id.rl_footer);
iv_header = (ImageView) findViewById(R.id.iv_up_arrow);
Button button = (Button) findViewById(R.id.button1);
Button buttonstop = (Button) findViewById(R.id.button2);
buttonstop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
rl_footer.clearAnimation();
rl_footer.removeAllViews();
}
});
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iv_header.setImageResource(R.drawable.delete);
iv_header.setPadding(0, 10, 0, 0);
// rl_footer.setBackgroundResource(R.drawable.download);
// if (isBottom) {
SlideToAbove();
// isBottom = false;
// } else {
// iv_header.setImageResource(R.drawable.delete);
// iv_header.setPadding(0, 0, 0,50);
// rl_footer.setBackgroundResource(R.drawable.download);
// SlideToDown();
// isBottom = true;
// }
}
});
}
public void SlideToAbove() {
Animation slide = null;
slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, -5.0f);
slide.setDuration(400);
slide.setFillAfter(true);
slide.setFillEnabled(true);
rl_footer.startAnimation(slide);
slide.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// rl_footer.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
rl_footer.getWidth(), rl_footer.getHeight());
// lp.setMargins(0, 0, 0, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rl_footer.setLayoutParams(lp);
iv_header.setImageResource(R.drawable.delete);
iv_header.setPadding(0, 0, 0, 50);
SlideToDown();
}
});
}
public void SlideToDown() {
Animation slide = null;
slide = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 5.2f);
slide.setDuration(400);
slide.setFillAfter(true);
slide.setFillEnabled(true);
rl_footer.startAnimation(slide);
slide.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// rl_footer.clearAnimation();
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
rl_footer.getWidth(), rl_footer.getHeight());
lp.setMargins(0, rl_footer.getWidth(), 0, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rl_footer.setLayoutParams(lp);
iv_header.setImageResource(R.drawable.delete);
iv_header.setPadding(0, 10, 0, 0);
SlideToAbove();
}
});
}
}
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:id="@+id/rl_footer"
android:layout_width="fill_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true" >
<ImageView
android:id="@+id/iv_up_arrow"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:paddingBottom="10dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/rl_footer"
android:layout_alignParentTop="true"
android:text="start" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="84dp"
android:text="stop" />
</RelativeLayout>
答案 0 :(得分:0)
clearAnimation()
取消此视图的所有动画。
removeAllViews()
调用此方法从ViewGroup中删除所有子视图。
尝试删除此行:
rl_footer.removeAllViews();
答案 1 :(得分:0)
rl_footer.removeAllViews();
“调用此方法可从ViewGroup中删除所有子视图”。 对此进行评论,图像不应消失。
希望这有帮助。
答案 2 :(得分:0)
正如其他人所说,不要调用removeAllViews。
如果您调用clearAnimation,View将返回其原始位置。 如果您不调用clearAnimation,View将继续设置动画。
您需要做的是计算进入当前动画周期的时间量,然后创建一个持续时间为0的新动画,fillEnabled,fillBefore和fillAfter设置为true,并且平移量等于当前动画抵消视图的数量。