Android animate my Relative Layout from bottom to Top and Top to Bottom using translate animation on image view click

后端 未结 1 1280
时光说笑
时光说笑 2020-12-09 04:39

I Need to make animate my Relative Layout when click on image view..

1.Moving Relative Layout from Bottom to Top (when clicking him on image view).

2.Moving

1条回答
  •  感动是毒
    2020-12-09 04:58

    I have solved my issue and now my animation works fine :) if anyone needed just copy my code and xml and plz don't thanks :p

    My Activity MainActivity:

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.animation.Animation;
    import android.view.animation.Animation.AnimationListener;
    import android.view.animation.AnimationUtils;
    import android.view.animation.TranslateAnimation;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.RelativeLayout;
    
    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);
        iv_header.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                iv_header.setImageResource(R.drawable.down_arrow);
                iv_header.setPadding(0, 10, 0, 0); 
                rl_footer.setBackgroundResource(R.drawable.up_manu_bar);
                if (isBottom) {
                    SlideToAbove();
                    isBottom = false;
                } else {
                    iv_header.setImageResource(R.drawable.up_arrow);
                    iv_header.setPadding(0, 0, 0, 10);
                    rl_footer.setBackgroundResource(R.drawable.down_manu_bar1);
                    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;
    }
    
     }
    

    and my Xml activity_main:

     
     
    
     
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
     
    
     
    

    just create new android project and copy paste my code and have fun! :) also remember in xml i have image view and his background images replace with yout own images thanks..

    0 讨论(0)
提交回复
热议问题