图像顶部移至底部,底部不断顶部时,单击按钮 [英] Image moves top to bottom and bottom to top continuously when button clicked

查看:126
本文介绍了图像顶部移至底部,底部不断顶部时,单击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形象和一个按钮,当我点击按钮,图像移动从上到下,从下到不断顶部,直到我点击停止按钮但是当我点击启动按钮,它只能移动自下而上或自上而下,它不是连续移动两侧。

我的code是:

 公共类MainActivity延伸活动{RelativeLayout的rl_footer;
ImageView的iv_header;
布尔isBottom = TRUE;
按钮BTN1;@覆盖
保护无效的onCreate(捆绑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按钮=(按钮)findViewById(R.id.button1);
    button.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            // TODO自动生成方法存根
            iv_header.setImageResource(R.drawable.download);
            iv_header.setPadding(0,10,0,0);
            //rl_footer.setBackgroundResource(R.drawable.download);
            如果(isBottom){
                SlideToAbove();
                isBottom = FALSE;            }其他{
                iv_header.setImageResource(R.drawable.download);
                iv_header.setPadding(0,0,0,50);
                //rl_footer.setBackgroundResource(R.drawable.download);
                SlideToDown();
                isBottom = TRUE;
            }        }
    });}公共无效SlideToAbove(){
    动画幻灯片= NULL;
    幻灯片=新TranslateAnimation(Animation.RELATIVE_TO_SELF,0.0,
            Animation.RELATIVE_TO_SELF,0.0,Animation.RELATIVE_TO_SELF,
            0.0,Animation.RELATIVE_TO_SELF,-5.0f);    slide.setDuration(400);
    slide.setFillAfter(真);
    slide.setFillEnabled(真);
    rl_footer.startAnimation(幻灯片);    slide.setAnimationListener(新AnimationListener(){        @覆盖
        公共无效onAnimationStart(动画动画){        }        @覆盖
        公共无效onAnimationRepeat(动画动画){
        }        @覆盖
        公共无效onAnimationEnd(动画动画){            rl_footer.clearAnimation();            RelativeLayout.LayoutParams LP =新RelativeLayout.LayoutParams(
                    rl_footer.getWidth(),rl_footer.getHeight());
            // lp.setMargins(0,0,0,0);
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            rl_footer.setLayoutParams(LP);        }    });}公共无效SlideToDown(){
    动画幻灯片= NULL;
    幻灯片=新TranslateAnimation(Animation.RELATIVE_TO_SELF,0.0,
            Animation.RELATIVE_TO_SELF,0.0,Animation.RELATIVE_TO_SELF,
            0.0,Animation.RELATIVE_TO_SELF,5.2f);    slide.setDuration(400);
    slide.setFillAfter(真);
    slide.setFillEnabled(真);
    rl_footer.startAnimation(幻灯片);    slide.setAnimationListener(新AnimationListener(){        @覆盖
        公共无效onAnimationStart(动画动画){        }        @覆盖
        公共无效onAnimationRepeat(动画动画){
        }        @覆盖
        公共无效onAnimationEnd(动画动画){            rl_footer.clearAnimation();            RelativeLayout.LayoutParams LP =新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);        }    });}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}   }

XML code:

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / rl_main
机器人:layout_width =match_parent
 机器人:layout_height =match_parent
 机器人:背景=@绘制/图形> < RelativeLayout的
机器人:ID =@ + ID / rl_footer
机器人:layout_width =FILL_PARENT
机器人:layout_height =70dp
机器人:layout_alignParentBottom =真正的>< ImageView的
    机器人:ID =@ + ID / iv_up_arrow
    机器人:layout_width =45dp
    机器人:layout_height =45dp
    机器人:layout_centerHorizo​​ntal =真
    机器人:layout_centerVertical =真
    机器人:paddingBottom会=10dp
    机器人:SRC =@绘制/下载/>  < / RelativeLayout的>
  <按钮
 机器人:ID =@ + ID /按钮1
 机器人:layout_width =WRAP_CONTENT
 机器人:layout_height =WRAP_CONTENT
 机器人:layout_alignLeft =@ + ID / rl_footer
 机器人:layout_alignParentTop =真
 机器人:文字=开始/><按钮
 机器人:ID =@ + ID /按钮2
 机器人:layout_width =WRAP_CONTENT
 机器人:layout_height =WRAP_CONTENT
 机器人:layout_alignLeft =@ + ID / rl_footer
 机器人:layout_alignParentTop =真
 机器人:文字=停止/>
 < / RelativeLayout的>


解决方案

尝试下面code其工作罚款的两个动画,它不断地移动图像从下到上,从上到下。并将停止动画作为你点击停止按钮。

我已经做了,你可以用你的code验证一些相关的变化。

 公共类MainActivity延伸活动{
    RelativeLayout的rl_footer;
    ImageView的iv_header;
    布尔isBottom = TRUE;
    按钮BTN1;    @覆盖
    保护无效的onCreate(捆绑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按钮=(按钮)findViewById(R.id.button1);
        按钮buttonstop =(按钮)findViewById(R.id.button2);
        buttonstop.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                rl_footer.clearAnimation();
                rl_footer.removeAllViews();
            }
        });
        button.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                iv_header.setImageResource(R.drawable.delete);
                iv_header.setPadding(0,10,0,0);
                // rl_footer.setBackgroundResource(R.drawable.download);
                //如果(isBottom){
                SlideToAbove();
                // isBottom = FALSE;                //}其他{
                // iv_header.setImageResource(R.drawable.delete);
                // iv_header.setPadding(0,0,0,50);
                // rl_footer.setBackgroundResource(R.drawable.download);
                // SlideToDown();
                // isBottom = TRUE;
                //}            }
        });
    }    公共无效SlideToAbove(){
        动画幻灯片= NULL;
        幻灯片=新TranslateAnimation(Animation.RELATIVE_TO_SELF,0.0,
                Animation.RELATIVE_TO_SELF,0.0,Animation.RELATIVE_TO_SELF,
                0.0,Animation.RELATIVE_TO_SELF,-5.0f);        slide.setDuration(400);
        slide.setFillAfter(真);
        slide.setFillEnabled(真);
        rl_footer.startAnimation(幻灯片);        slide.setAnimationListener(新AnimationListener(){            @覆盖
            公共无效onAnimationStart(动画动画){            }            @覆盖
            公共无效onAnimationRepeat(动画动画){
            }            @覆盖
            公共无效onAnimationEnd(动画动画){// rl_footer.clearAnimation();                RelativeLayout.LayoutParams LP =新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();
            }        });    }    公共无效SlideToDown(){
        动画幻灯片= NULL;
        幻灯片=新TranslateAnimation(Animation.RELATIVE_TO_SELF,0.0,
                Animation.RELATIVE_TO_SELF,0.0,Animation.RELATIVE_TO_SELF,
                0.0,Animation.RELATIVE_TO_SELF,5.2f);        slide.setDuration(400);
        slide.setFillAfter(真);
        slide.setFillEnabled(真);
        rl_footer.startAnimation(幻灯片);        slide.setAnimationListener(新AnimationListener(){            @覆盖
            公共无效onAnimationStart(动画动画){            }            @覆盖
            公共无效onAnimationRepeat(动画动画){
            }            @覆盖
            公共无效onAnimationEnd(动画动画){// rl_footer.clearAnimation();                RelativeLayout.LayoutParams LP =新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版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / rl_main
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    >    < RelativeLayout的
        机器人:ID =@ + ID / rl_footer
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =70dp
        机器人:layout_alignParentBottom =真正的>        < ImageView的
            机器人:ID =@ + ID / iv_up_arrow
            机器人:layout_width =45dp
            机器人:layout_height =45dp
            机器人:layout_centerHorizo​​ntal =真
            机器人:layout_centerVertical =真
            机器人:paddingBottom会=10dp
            机器人:SRC =@绘制/ ic_launcher/>
    < / RelativeLayout的>    <按钮
        机器人:ID =@ + ID /按钮1
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignLeft =@ + ID / rl_footer
        机器人:layout_alignParentTop =真
        机器人:文字=开始/>    <按钮
        机器人:ID =@ + ID /按钮2
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:layout_marginLeft =84dp
        机器人:文字=停止/>< / RelativeLayout的>

I have an image and one button, when I clicked button the image is moved top to bottom and bottom to top continuously until I am click the stop button but when I click start button it move only bottom to top or top to bottom,its not moves continuously both sides.

my code is:

 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.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            iv_header.setImageResource(R.drawable.download);
            iv_header.setPadding(0, 10, 0, 0); 
            //rl_footer.setBackgroundResource(R.drawable.download);
            if (isBottom) {
                SlideToAbove();
                isBottom = false;

            } else {
                iv_header.setImageResource(R.drawable.download);
                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);

        }

    });

}

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);

        }

    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

   }

xml code:

<?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"
 android:background="@drawable/graphics" >

 <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/download" />

  </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_alignLeft="@+id/rl_footer"
 android:layout_alignParentTop="true"
 android:text="stop" />
 </RelativeLayout>

解决方案

Try out below code its working fine for your both animation and it moves image continuously from bottom to top and top to bottom. And will stop animation as you click on stop button.

I have done some relevant changes which you can verify with your code.

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();

            }

        });

    }

}

Layout:

<?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>

这篇关于图像顶部移至底部,底部不断顶部时,单击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆