• 出栈入栈动画demo


    项目做了一个切换界面动画的功能,用到了出栈入栈的,写了一个demo

    package com.myron.stackview;
    
    import java.util.Stack;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.animation.Animation;
    import android.view.animation.LinearInterpolator;
    import android.view.animation.TranslateAnimation;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    
    public class StackViewActivity extends Activity {
        private Stack<View> mViewStack = new Stack<View>();
        private View mCurrentView;
        private LinearLayout parent2;
     // 动画
    	 private TranslateAnimation mAnimLeftOut;
    	 private TranslateAnimation mAnimRightIn;
    	 private TranslateAnimation mAnimRightOut;
    	 private TranslateAnimation mAnimLeftIn;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            
            initAnim();
            //第一个界面
            LinearLayout parent = new LinearLayout(this);
            parent.setOrientation(LinearLayout.VERTICAL);
            ImageView imageView = new ImageView(this);
            imageView.setBackgroundResource(R.drawable.icon);
            parent.addView(imageView, -2, -2);
            Button btnPush = new Button(this);
            btnPush.setId(1);
            btnPush.setText("push");
            parent.addView(btnPush, -2, -2);
            pushView2Stack(parent);
            btnPush.setOnClickListener(listener);
            
            //第二个界面
            parent2 = new LinearLayout(this);
            parent2.setOrientation(LinearLayout.VERTICAL);
            ImageView imageView2 = new ImageView(this);
            imageView2.setBackgroundResource(R.drawable.icon);
            parent2.addView(imageView2, -2, -2);
            Button btnPop = new Button(this);
            btnPop.setId(2);
            btnPop.setText("pop");
            parent2.addView(btnPop, -2, -2);
            btnPop.setOnClickListener(listener);
        }
        
        private OnClickListener listener = new OnClickListener() {
    		
    		@Override
    		public void onClick(View v) {
    			int id = v.getId();
    			switch (id) {
    			case 1:
    				pushView2Stack(parent2);
    				break;
    			case 2:
    				popViewFromStack();
    				break;
    
    			default:
    				break;
    			}
    		}
    	};
        
    	/**
    	 * 进栈
    	 * @param newView
    	 */
        public void pushView2Stack(View newView) {
    		if (mViewStack.size() > 0) {
    			View peek = mViewStack.peek();
    			peek.clearFocus();
    			peek.startAnimation(mAnimLeftOut);
    		}
    		mViewStack.push(newView);
    		mCurrentView = newView;
    		setContentView(newView);
    		newView.requestFocus();
    		if (mViewStack.size() > 1) {
    			// 启动动画
    			newView.startAnimation(mAnimRightIn);
    		}
    	}
        
        public View popViewFromStack() {
    		if (mViewStack.size() > 1) {
    			// 弹出旧ui
    			View pop = mViewStack.pop();
    			pop.clearFocus();
    			// 动画
    			pop.startAnimation(mAnimRightOut);
    
    			// 新ui
    			mCurrentView = mViewStack.peek();
    			setContentView(mCurrentView);
    			mCurrentView.requestFocus();
    			mCurrentView.startAnimation(mAnimLeftIn);
    			return mCurrentView;
    		} else {
    			finish();
    			return null;
    		}
    	}
        
        private void initAnim() {
       	 // 左出
       	 mAnimLeftOut = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
       	 Animation.RELATIVE_TO_SELF, -1, Animation.RELATIVE_TO_SELF, 0,
       	 Animation.RELATIVE_TO_SELF, 0);
       	 mAnimLeftOut.setDuration(400);
       	 mAnimLeftOut.setFillAfter(true);
       	 mAnimLeftOut.setInterpolator(new LinearInterpolator());
       	
       	 // 右进
       	 mAnimRightIn = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1,
       	 Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
       	 Animation.RELATIVE_TO_SELF, 0);
       	 mAnimRightIn.setDuration(400);
       	 mAnimRightIn.setFillAfter(true);
       	 mAnimRightIn.setInterpolator(new LinearInterpolator());
       	
       	 // 右出
       	 mAnimRightOut = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0,
       	 Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0,
       	 Animation.RELATIVE_TO_SELF, 0);
       	 mAnimRightOut.setDuration(400);
       	 mAnimRightOut.setFillAfter(true);
       	 mAnimRightOut.setInterpolator(new LinearInterpolator());
       	
       	 // 左进
       	 mAnimLeftIn = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1,
       	 Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
       	 Animation.RELATIVE_TO_SELF, 0);
       	 mAnimLeftIn.setDuration(400);
       	 mAnimLeftIn.setFillAfter(true);
       	 mAnimLeftIn.setInterpolator(new LinearInterpolator());
       	 }
    }
    

      

  • 相关阅读:
    Elasticsearch学习之基本核心概念
    Kudu,支持快速分析的新型Hadoop存储系统
    npm命令
    git相关命令
    Linux下Nodejs安装(完整详细)转
    数据库删除discuz 部分数据操作
    用tcping检查网站开放的端口
    Mybatis各种模糊查询
    抱歉,当前存在网络问题或服务器繁忙错误代码:20003问题解决方法
    win7下python2.7安装 pip,setuptools的正确方法
  • 原文地址:https://www.cnblogs.com/ycclmy/p/3939097.html
Copyright © 2020-2023  润新知