码迷,mamicode.com
首页 > 其他好文 > 详细

出栈入栈动画demo

时间:2014-08-27 12:36:37      阅读:805      评论:0      收藏:0      [点我收藏+]

标签:android   blog   os   java   io   ar   art   div   cti   

项目做了一个切换界面动画的功能,用到了出栈入栈的,写了一个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());
   	 }
}

  

出栈入栈动画demo

标签:android   blog   os   java   io   ar   art   div   cti   

原文地址:http://www.cnblogs.com/ycclmy/p/3939097.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!