AnimationController+ReacnnAnimationTabHost

本文介绍了一个自定义的Android动画控制器类,该类提供多种动画效果,如淡入淡出、滑动和平移等,并展示如何将其应用于自定义的TabHost组件中,实现平滑的标签页切换动画。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.reacnn.android.utils;

import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;

public class AnimationController {
	public final int rela1 = Animation.RELATIVE_TO_SELF;
	public final int rela2 = Animation.RELATIVE_TO_PARENT;

	public final int Default = -1;
	public final int Linear = 0;
	public final int Accelerate = 1;
	public final int Decelerate = 2;
	public final int AccelerateDecelerate = 3;
	public final int Bounce = 4;
	public final int Overshoot = 5;
	public final int Anticipate = 6;
	public final int AnticipateOvershoot = 7;
	
	public AnimationController() {
	}

	private class MyAnimationListener implements AnimationListener {
		private View view;

		public MyAnimationListener(View view) {
			this.view = view;
		}

		@Override
		public void onAnimationStart(Animation animation) {
			// this.view.setVisibility(View.VISIBLE);
		}

		@Override
		public void onAnimationEnd(Animation animation) {
			this.view.setVisibility(View.GONE);
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
		}

	}

	private void setEffect(Animation animation, int interpolatorType,
			long durationMillis, long delayMillis) {
		switch (interpolatorType) {
		case 0:
			animation.setInterpolator(new LinearInterpolator());
			break;
		case 1:
			animation.setInterpolator(new AccelerateInterpolator());
			break;
		case 2:
			animation.setInterpolator(new DecelerateInterpolator());
			break;
		case 3:
			animation.setInterpolator(new AccelerateDecelerateInterpolator());
			break;
		case 4:
			animation.setInterpolator(new BounceInterpolator());
			break;
		case 5:
			animation.setInterpolator(new OvershootInterpolator());
			break;
		case 6:
			animation.setInterpolator(new AnticipateInterpolator());
			break;
		case 7:
			animation.setInterpolator(new AnticipateOvershootInterpolator());
			break;
		default:
			break;
		}
		animation.setDuration(durationMillis);
		animation.setStartOffset(delayMillis);
	}

	private void baseIn(View view, Animation animation, long durationMillis,
			long delayMillis) {
		setEffect(animation, Default, durationMillis, delayMillis);
		view.setVisibility(View.VISIBLE);
		view.startAnimation(animation);
	}

	private void baseOut(View view, Animation animation, long durationMillis,
			long delayMillis) {
		setEffect(animation, Default, durationMillis, delayMillis);
		animation.setAnimationListener(new MyAnimationListener(view));
		view.startAnimation(animation);
	}

	public void show(View view) {
		view.setVisibility(View.VISIBLE);
	}

	public void hide(View view) {
		view.setVisibility(View.GONE);
	}

	public void transparent(View view) {
		view.setVisibility(View.INVISIBLE);
	}

	public void fadeIn(View view, long durationMillis, long delayMillis) {
		AlphaAnimation animation = new AlphaAnimation(0, 1);
		baseIn(view, animation, durationMillis, delayMillis);
	}

	public void fadeOut(View view, long durationMillis, long delayMillis) {
		AlphaAnimation animation = new AlphaAnimation(1, 0);
		baseOut(view, animation, durationMillis, delayMillis);
	}

	

 public void slideRightIn(View view, long durationMillis, long delayMillis) {   TranslateAnimation animation = new TranslateAnimation(rela2, 1, rela2,     0, rela2, 0, rela2, 0);   baseIn(view, animation, durationMillis, delayMillis);  }

 public void slideLeftOut(View view, long durationMillis, long delayMillis) {   TranslateAnimation animation = new TranslateAnimation(rela2, 0, rela2,     1, rela2, 0, rela2, 0);   baseIn(view, animation, durationMillis, delayMillis);  }

 public void slideRightOut(View view, long durationMillis, long delayMillis) {   TranslateAnimation animation = new TranslateAnimation(rela2, 0, rela2,     -1, rela2, 0, rela2, 0);   baseOut(view, animation, durationMillis, delayMillis);  }

 public void slideLeftIn(View view, long durationMillis, long delayMillis) {   TranslateAnimation animation = new TranslateAnimation(rela2, -1, rela2,     0, rela2, 0, rela2, 0);   baseOut(view, animation, durationMillis, delayMillis);  }

 

public void scaleIn(View view, long durationMillis, long delayMillis) {

ScaleAnimation animation = new ScaleAnimation(0, 1, 0, 1, rela2, 0.5f, rela2, 0.5f); baseIn(view, animation, durationMillis, delayMillis); } public void scaleOut(View view, long durationMillis, long delayMillis) { ScaleAnimation animation = new ScaleAnimation(1, 0, 1, 0, rela2, 0.5f, rela2, 0.5f); baseOut(view, animation, durationMillis, delayMillis); } public void rotateIn(View view, long durationMillis, long delayMillis) { RotateAnimation animation = new RotateAnimation(-90, 0, rela1, 0, rela1, 1); baseIn(view, animation, durationMillis, delayMillis); } public void rotateOut(View view, long durationMillis, long delayMillis) { RotateAnimation animation = new RotateAnimation(0, 90, rela1, 0, rela1, 1); baseOut(view, animation, durationMillis, delayMillis); } public void scaleRotateIn(View view, long durationMillis, long delayMillis) { ScaleAnimation animation1 = new ScaleAnimation(0, 1, 0, 1, rela1, 0.5f, rela1, 0.5f); RotateAnimation animation2 = new RotateAnimation(0, 360, rela1, 0.5f, rela1, 0.5f); AnimationSet animation = new AnimationSet(false); animation.addAnimation(animation1); animation.addAnimation(animation2); baseIn(view, animation, durationMillis, delayMillis); } public void scaleRotateOut(View view, long durationMillis, long delayMillis) { ScaleAnimation animation1 = new ScaleAnimation(1, 0, 1, 0, rela1, 0.5f, rela1, 0.5f); RotateAnimation animation2 = new RotateAnimation(0, 360, rela1, 0.5f, rela1, 0.5f); AnimationSet animation = new AnimationSet(false); animation.addAnimation(animation1); animation.addAnimation(animation2); baseOut(view, animation, durationMillis, delayMillis); } public void slideFadeIn(View view, long durationMillis, long delayMillis) { TranslateAnimation animation1 = new TranslateAnimation(rela2, 1, rela2, 0, rela2, 0, rela2, 0); AlphaAnimation animation2 = new AlphaAnimation(0, 1); AnimationSet animation = new AnimationSet(false); animation.addAnimation(animation1); animation.addAnimation(animation2); baseIn(view, animation, durationMillis, delayMillis); } public void slideFadeOut(View view, long durationMillis, long delayMillis) { TranslateAnimation animation1 = new TranslateAnimation(rela2, 0, rela2, -1, rela2, 0, rela2, 0); AlphaAnimation animation2 = new AlphaAnimation(1, 0); AnimationSet animation = new AnimationSet(false); animation.addAnimation(animation1); animation.addAnimation(animation2); baseOut(view, animation, durationMillis, delayMillis); } }

 

 

package com.reacnn.android.view;

import com.reacnn.android.utils.AnimationController;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TabHost;

public class ReacnnAnimationTabHost extends TabHost {

	AnimationController animationController = new AnimationController();

	public ReacnnAnimationTabHost(Context context) {
		super(context);
		isOpenAnimation = true;
	}

	private boolean isOpenAnimation;
	private int mTabCount;

	public ReacnnAnimationTabHost(Context context, AttributeSet attrs) {
		super(context, attrs);

		isOpenAnimation = true;
	}

	/**
	 * 设置是否打开动画效果
	 * 
	 * @param isOpenAnimation
	 *            true:打开
	 */
	public void setOpenAnimation(boolean isOpenAnimation) {
		this.isOpenAnimation = isOpenAnimation;
	}

	/**
	 * 
	 * @return 返回当前标签页的总数
	 */

	public int getTabCount() {
		return mTabCount;
	}

	@Override
	public void addTab(TabSpec tabSpec) {
		mTabCount++;
		super.addTab(tabSpec);
	}

	@Override
	public void setCurrentTab(int index) {
		int mCurrentTabID = getCurrentTab();
		if (null != getCurrentView()) {
			if (isOpenAnimation) {
				if (mCurrentTabID == (mTabCount - 1) && index == 0) {
					animationController.fadeOut(getCurrentView(), 2000, 1000);
				} else if (mCurrentTabID == 0 && index == (mTabCount - 1)) {
					animationController.fadeOut(getCurrentView(), 2000, 1000);
				} else if (index > mCurrentTabID) {
					animationController.fadeOut(getCurrentView(), 2000, 1000);
				} else if (index < mCurrentTabID) {
					animationController.fadeOut(getCurrentView(), 2000, 1000);
				}
			}
		}
		super.setCurrentTab(index);
		if (isOpenAnimation) {
			if (mCurrentTabID == (mTabCount - 1) && index == 0) {
				animationController.slideFadeIn(getCurrentView(), 2000, 1000);
			} else if (mCurrentTabID == 0 && index == (mTabCount - 1)) {
				animationController.slideFadeIn(getCurrentView(), 2000, 1000);
			} else if (index > mCurrentTabID) {
				animationController.slideFadeIn(getCurrentView(), 2000, 1000);
			} else if (index < mCurrentTabID) {
				animationController.slideFadeIn(getCurrentView(), 2000, 1000);
			}
		}
	}

}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值