Android使用图像视图点击上的翻译动画,从底部到顶部和从上到下为我的相对布局设置动画

 mindylee 发布于 2023-02-12 21:00

点击图片视图时,我需要为我的相对布局设置动画.

1.从底部到顶部移动相对布局(在图像视图上单击时).

2.从上到下移动(在图像视图上再次单击时).

首先,当我点击图像视图时,它工作正常,相对布局从下到上向上移动,但是当我再次点击图像视图时,它从上到下是动画,当它到达原始位置时,它隐藏在我的活动上.帮助和提前谢谢.

这是我的活动:

 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.setVisibility(View.INVISIBLE);
            // iv_down.setVisibility(View.VISIBLE);
            iv_header.setPadding(0, 10, 0, 0); // substitute parameters for
                                                // left, top, right, bottom
            rl_footer.setBackgroundResource(R.drawable.up_manu_bar);
            // FooterAnimation();

            if (isBottom) {
                FooterAnimation();
                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);
                headerAnimation();
                isBottom = true;
            }

        }
    });

}

public void FooterAnimation() {
    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();

            if (isBottom) {
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        rl_footer.getWidth(), rl_footer.getHeight());
                lp.setMargins(rl_footer.getWidth(), 0, 0, 0);
                rl_footer.setLayoutParams(lp);
            } else {
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        rl_footer.getWidth(), rl_footer.getHeight());
                lp.setMargins(0, 0, 0, 0);
                rl_footer.setLayoutParams(lp);
            }

        }

    });

}

public void headerAnimation() {

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

            if (isBottom) {
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        rl_footer.getWidth(), rl_footer.getHeight());
                lp.setMargins(rl_footer.getWidth(), 0, 0, 0);
                rl_footer.setLayoutParams(lp);
            } else {
                RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                        rl_footer.getWidth(), rl_footer.getHeight());
                lp.setMargins(0, 0, 0, 0);
                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:

 




    

    

    

    

    

    

    

    

    

    
 


Farhan Shah.. 19

我已经解决了我的问题,现在我的动画工作正常:)如果有人需要只复制我的代码和xml和PLZ不要谢谢:p

我的活动主要活动:

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:

 
 

 

    

    

    

    

    

    

    

    

    

    
 

 

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

1 个回答
  • 我已经解决了我的问题,现在我的动画工作正常:)如果有人需要只复制我的代码和xml和PLZ不要谢谢:p

    我的活动主要活动:

    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"?>
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/rl_main"
      android:layout_
      android:layout_
      android:background="@drawable/autograph_bg" >
    
     <RelativeLayout
        android:id="@+id/rl_footer"
        android:layout_
        android:layout_
        android:layout_alignParentBottom="true"
        android:background="@drawable/down_manu_bar1" >
    
        <ImageView
            android:id="@+id/iv_new_file"
            android:layout_
            android:layout_
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="18dp"
            android:onClick="onNewFileClick"
            android:src="@drawable/file_icon" />
    
        <TextView
            android:id="@+id/tv_new_file"
            android:layout_
            android:layout_
            android:layout_alignLeft="@+id/iv_new_file"
            android:layout_below="@+id/iv_new_file"
            android:text="New"
            android:textColor="#ffffff" />
    
        <ImageView
            android:id="@+id/iv_insert"
            android:layout_
            android:layout_
            android:layout_alignTop="@+id/iv_new_file"
            android:layout_marginLeft="30dp"
            android:layout_toRightOf="@+id/iv_new_file"
            android:src="@drawable/insert_icon" />
    
        <TextView
            android:id="@+id/tv_insert"
            android:layout_
            android:layout_
            android:layout_alignLeft="@+id/iv_insert"
            android:layout_below="@+id/iv_insert"
            android:text="Insert"
            android:textColor="#ffffff" />
    
        <ImageView
            android:id="@+id/iv_up_arrow"
            android:layout_
            android:layout_
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:paddingBottom="10dp"
            android:src="@drawable/up_arrow" />
    
        <ImageView
            android:id="@+id/iv_down_arrow"
            android:layout_
            android:layout_
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="@drawable/down_arrow"
            android:paddingBottom="10dp"
            android:visibility="gone" />
    
        <ImageView
            android:id="@+id/iv_save"
            android:layout_
            android:layout_
            android:layout_alignTop="@+id/iv_insert"
            android:layout_marginLeft="30dp"
            android:layout_toRightOf="@+id/iv_up_arrow"
            android:src="@drawable/save" />
    
        <TextView
            android:id="@+id/tv_save"
            android:layout_
            android:layout_
            android:layout_alignLeft="@+id/iv_save"
            android:layout_alignParentBottom="true"
            android:text="Save"
            android:textColor="#ffffff" />
    
        <ImageView
            android:id="@+id/iv_settings"
            android:layout_
            android:layout_
            android:layout_alignTop="@+id/iv_save"
            android:layout_marginLeft="27dp"
            android:layout_toRightOf="@+id/tv_save"
            android:paddingTop="2dp"
            android:src="@drawable/icon_settings" />
    
        <TextView
            android:id="@+id/tv_settings"
            android:layout_
            android:layout_
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="260dp"
            android:text="Settings"
            android:textColor="#ffffff" />
     </RelativeLayout>
    
     </RelativeLayout>
    

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

    2023-02-12 21:05 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有