zoukankan      html  css  js  c++  java
  • 出栈入栈动画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());
       	 }
    }
    

      

  • 相关阅读:
    ZedGrapy使用实例
    C#分割字符串(包括使用字符串分割)
    从VS2005项目转换为VS2008项目(C#版)
    关于SQL操作的一些经验
    android socket 编程总结
    Excel绘制人口金字塔图
    使用命令让IE全屏显示指定的页面,适用于触摸屏终端机
    毕业这两年
    使用XML数据结合XSLT导出Excel
    XSLT实现XML作为数据源在web页面显示人口金字塔统计图
  • 原文地址:https://www.cnblogs.com/ycclmy/p/3939097.html
Copyright © 2011-2022 走看看