在ImageView上单击从下到上和从上到下的Android布局animation

我在Android中创建了一个视图,我需要从下到上为其设置animation,反之亦然。 当我点击ImageView我需要从底部到顶部为完整的RelativeLayout设置animation,并且它已成功。 但是当我再次点击ImageView并且它没有向下移动时。 此外,当我单击其原始位置时,当我单击ImageViewanimation的原始位置时,但RelativeLayout从原始位置向下移动而不是从上到下。

这是我的代码:

 ImageView iv_header; RelativeLayout rl_footer; boolean isBottom = true; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.rateus_layout); 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 if (isBottom) { FooterAnimation(); isBottom = false; } else { headerAnimation(); isBottom = true; } } }); } public void FooterAnimation() { Animation hide = AnimationUtils.loadAnimation(this, R.anim.move); rl_footer.startAnimation(hide); } public void headerAnimation() { Animation hide = AnimationUtils.loadAnimation(this, R.anim.footer); rl_footer.startAnimation(hide); } 

animation文件从下到上(完全相对布局从底部移动到顶部):

   

从上到下的animation文件(我想再次将相对布局从顶部移动到底部):

   

Xml文件:

                

Solutions Collecting From Web of "在ImageView上单击从下到上和从上到下的Android布局animation"

尝试这个 :

在res文件夹中创建anim文件夹并复制这四个文件:

slide_in_bottom.xml:

 < ?xml version="1.0" encoding="utf-8"?>  

slide_out_bottom.xml:

 < ?xml version="1.0" encoding="utf-8"?>  

slide_in_top.xml:

 < ?xml version="1.0" encoding="utf-8"?>  

slide_out_top.xml:

 < ?xml version="1.0" encoding="utf-8"?>  

当您单击图像视图调用时:

 overridePendingTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom); 

当您点击原始地方电话时:

 overridePendingTransition(R.anim.slide_in_top, R.anim.slide_out_top); 

主要活动 :

 package com.example.animationtest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn1 = (Button) findViewById(R.id.btn1); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, test.class)); } }); } } 

activity_main.xml:

    

test.java:

 package com.example.animationtest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class test extends Activity { Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.test); btn1 = (Button) findViewById(R.id.btn1); overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right); startActivity(new Intent(test.this, MainActivity.class)); } }); } } 

test.xml:

 < ?xml version="1.0" encoding="utf-8"?>    

希望这可以帮助。

我已经解决了我的问题,现在我的animation工作正常:)如果有人需要只需复制我的代码和xml文件,并有一个快乐的编码:)

我的活动主要活动:

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

和我的Xml activity_main:

  < ?xml version="1.0" encoding="utf-8"?>               

只需创建新的Android项目并复制粘贴我的代码,玩得开心! :)还记得在xml我有图像视图和他的背景图像替换你自己的图像谢谢..

在/ res / anim中创建目录并创建bottom_to_original.xml

 < ?xml version="1.0" encoding="utf-8"?>    LinearLayout ll = findViewById(R.id.ll); Animation animation; animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.sample_animation); ll .setAnimation(animation);