animTabHost.java
package com.tcsoft.opac.view;
import com.tcsoft.opac.activity.effect.AnimationFactory;
import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.widget.TabHost;
public class AnimTabHost extends TabHost {
private boolean isOpenAnimation = false;
private Animation slideRightOut = null;
private Animation slideLeftOut = null;
private Animation slideRightIn = null;
private Animation slideLeftIn = null;
private int duration = 300;
private int mTabCount = 3;
public AnimTabHost(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AnimTabHost(Context context) {
super(context);
}
@Override
public void addTab(TabSpec tabSpec) {
super.addTab(tabSpec);
// mTabCount ++;
}
@Override
public void clearAllTabs() {
super.clearAllTabs();
mTabCount = 0;
}
@Override
public void setCurrentTab(int index) {
int mCurrentTabID = getCurrentTab();
if (index == mCurrentTabID){
return;
}
if (null != getCurrentView()) {
// 第一次设置 Tab 时,该值为 null。
//设置当前的View的效果
if (isOpenAnimation) {
if (mCurrentTabID == (mTabCount - 1) && index == 0) {//从第一个到最后一个
getCurrentView().startAnimation(slideRightOut);
} else if (mCurrentTabID == 0 && index == (mTabCount - 1)) {//从最后一个到第一个
getCurrentView().startAnimation(slideLeftOut);
} else if (index > mCurrentTabID) {
getCurrentView().startAnimation(slideLeftOut);
} else if (index < mCurrentTabID) {
getCurrentView().startAnimation(slideRightOut);
}
}
}
super.setCurrentTab(index);
//设置下一个view的效果
if (isOpenAnimation) {
if (mCurrentTabID == (mTabCount - 1) && index == 0) {
getCurrentView().startAnimation(slideRightIn);
} else if (mCurrentTabID == 0 && index == (mTabCount - 1)) {
getCurrentView().startAnimation(slideLeftIn);
} else if (index > mCurrentTabID) {
getCurrentView().startAnimation(slideLeftIn);
} else if (index < mCurrentTabID) {
getCurrentView().startAnimation(slideRightIn);
}
}
}
public void setAnimation(Animation slideRightIn, Animation slideLeftIn,Animation slideRightOut,Animation slideLeftOut){
this.slideRightIn = slideRightIn;
this.slideLeftIn = slideLeftIn;
this.slideRightOut = slideRightOut;
this.slideLeftOut = slideLeftOut;
isOpenAnimation = true;
}
private void newAnimation(){
if (slideRightIn == null){
slideRightIn = AnimationFactory.getTranslateType(AnimationFactory.TRANSLATE_IN_RIGHT);
slideRightIn.setDuration(duration);
}
if (slideLeftIn == null){
slideLeftIn = AnimationFactory.getTranslateType(AnimationFactory.TRANSLATE_IN_LEFT);
slideLeftIn.setDuration(duration);
}
if (slideRightOut == null){
slideRightOut = AnimationFactory.getTranslateType(AnimationFactory.TRANSLATE_OUT_RIGHT);
slideRightOut.setDuration(duration);
}
if (slideLeftOut == null){
slideLeftOut = AnimationFactory.getTranslateType(AnimationFactory.TRANSLATE_OUT_LEFT);
slideLeftOut.setDuration(duration);
}
}
public boolean isOpenAnimation() {
return isOpenAnimation;
}
public void setOpenAnimation(boolean isOpenAnimation) {
this.isOpenAnimation = isOpenAnimation;
newAnimation();
}
public Animation getSlideRightOut() {
return slideRightOut;
}
public Animation getSlideLeftOut() {
return slideLeftOut;
}
public Animation getSlideRightIn() {
return slideRightIn;
}
public Animation getSlideLeftIn() {
return slideLeftIn;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
}
AnimationFactory.java
package com.tcsoft.opac.activity.effect;
import android.content.Context;
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.AnimationUtils;
import android.view.animation.Interpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
/**
* 一些基本的animation 特效的工厂对象
* @author Robote
*
*/
public class AnimationFactory {
public static final int NULL_ANIM = 0; //无anim效果
public static final int ALPHA_TOSHOW = 1;//透明度从无到有的效果
public static final int ALPHA_TOHIDE = 2;//透明度从有到无的效果
public static final int SCALE_ENLARGE = 11; //从中心放大
public static final int SCALE_NARROW = 12; //从中心缩小
public static final int ROTATE_CENTER = 21; //以画面中心旋转360度
public static final int ROTATE_TOP = 22; //以画面上中心旋转360
public static final int TRANSLATE_OUT_TOP = 31; //向上退出
public static final int TRANSLATE_OUT_BUTTON = 32; //向下退出
public static final int TRANSLATE_OUT_LEFT = 33; //向左退出
public static final int TRANSLATE_OUT_RIGHT = 34; //向右退出
public static final int TRANSLATE_IN_TOP = 35; //向上退出
public static final int TRANSLATE_IN_BUTTON = 36; //向下退出
public static final int TRANSLATE_IN_LEFT = 37; //向左退出
public static final int TRANSLATE_IN_RIGHT = 38; //向右退出
public static final int DURATION_AUTO = -1; //自动设定时间,设置这个选项,将采用勾股函数,算出移动的偏移量
public static final int INTERPOLATOR_LINEAR = 101; //线性
public static final int INTERPOLATOR_ACCELERATEDECELERATE = 102; //在动画开始与结束的地方速率改变比较慢,在中间的时侯加速
public static final int INTERPOLATOR_CYCLE = 103; // 动画循环播放特定的次数,速率改变沿着正弦曲线
public static final int INTERPOLATOR_ACCELERATE = 104; //在动画开始的地方速率改变比较慢,然后开始加速
public static final int INTERPOLATOR_DECELERATE = 105; //在动画开始的地方速率改变比较慢,然后开始减速
/**
* 淡入,80%放大,最后固定为正常效果
*/
public static final int FADE_IN_ZOON= 201;
/**
* 淡出 ,缩小至80%,最后固定为消失
*/
public static final int FADE_OUT_NARROW =202;
private Context context = null;
public AnimationFactory(){}
public AnimationFactory(Context context){
this.context = context;
}
/**
* 获得单一程序内置的Animation,默认动画时间
* @param Type Type 需要的animation 类型
* @return
*/
public static AnimationSet getAnimation(int Type){
return getAnimation(Type,DURATION_AUTO);
}
/**
* 获得单一程序内置的Animation
* @param Type 需要的animation 类型
* @param Duration 动画播放时间
* @return
*/
public static AnimationSet getAnimation(int Type,int Duration){
AnimationSet animationSet = new AnimationSet(true);
switch (Type) {
case FADE_IN_ZOON:
animationSet.addAnimation(new AlphaAnimation(0f, 1.0f));
animationSet.addAnimation(new ScaleAnimation(0.8f,1.0f, 0.8f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f));
break;
case FADE_OUT_NARROW:
animationSet.addAnimation(new AlphaAnimation(1.0f, 0f));
animationSet.addAnimation(new ScaleAnimation(1.0f,0.8f, 1.0f, 0.8f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f));
break;
default:
break;
}
if (Duration < 0){
animationSet.setDuration(500);
}else{
animationSet.setDuration(Duration);
}
animationSet.setFillAfter(true);
return animationSet;
}
/**
* 获得一个animation 集合
* @param alphaType 透明度 Type ALPHA_TOSHOW || ALPHA_TOHIDE
* @param scaleType 伸缩度Type SCALE_ENLARGE || SCALE_NARROW
* @param rotateType 旋转 Type ROTATE_CENTER|| ROTATE_TOP
* @param translateType 平移 Type TRANSLATE_OUT_TOP || TRANSLATE_OUT_BUTTON || TRANSLATE_OUT_LEFT || TRANSLATE_OUT_RIGHT
* @param Duration 动画时间默认500 毫秒完成
* @return AnimationSet 一个集合了多种animation效果的集合
*/
public static AnimationSet getAnimationSet(int alphaType,int scaleType,
int rotateType,int translateType,int Duration){
AnimationSet animationset = new AnimationSet(true);
if (alphaType != 0 || alphaType != NULL_ANIM){
animationset.addAnimation(getAlphaAnimation(alphaType));
}
if (scaleType != 0 || scaleType != NULL_ANIM){
animationset.addAnimation(getScaleAnimation(scaleType));
}
if (rotateType != 0 || rotateType != NULL_ANIM){
animationset.addAnimation(getRotateAnimation(rotateType));
}
if (translateType != 0 || translateType != NULL_ANIM){
animationset.addAnimation(getTranslateType(translateType));
}
if (Duration == DURATION_AUTO | Duration < 0 ){
animationset.setDuration(500);
}else{
animationset.setDuration(Duration);
}
animationset.setFillAfter(true);
return animationset;
}
/**
* 获得一个animation 集合,默认时间为500ms
* @param alphaType 透明度 Type ALPHA_TOSHOW || ALPHA_TOHIDE
* @param scaleType 伸缩度Type SCALE_ENLARGE || SCALE_NARROW
* @param rotateType 旋转 Type ROTATE_CENTER|| ROTATE_TOP
* @param translateType 平移 Type TRANSLATE_OUT_TOP || TRANSLATE_OUT_BUTTON || TRANSLATE_OUT_LEFT || TRANSLATE_OUT_RIGHT
* @return AnimationSet 一个集合了多种animation效果的集合
*/
public static AnimationSet getAnimationSet(int alphaType,int scaleType,
int rotateType,int translateType){
return getAnimationSet(alphaType,scaleType,rotateType,translateType,500);
}
/**
* 透明度变化
* @param alphaType alpha 类型 Type ALPHA_TOSHOW || ALPHA_TOHIDE
* @return Animation
*/
public static Animation getAlphaAnimation(int alphaType){
Animation animation = null;
switch (alphaType){
case ALPHA_TOHIDE:
animation = new AlphaAnimation(1.0f, 0);
break;
case ALPHA_TOSHOW:
animation = new AlphaAnimation(0, 1.0f);
break;
case NULL_ANIM:
animation = null;
break;
default :
animation = new AlphaAnimation(1.0f, 0);
break;
}
return animation;
}
/**
* 伸缩度变化
* @param scaleType 伸缩度Type SCALE_ENLARGE || SCALE_NARROW
* @return Animation
*/
public static Animation getScaleAnimation(int scaleType){
Animation animation = null;
switch (scaleType){
case SCALE_ENLARGE:
animation = new ScaleAnimation(0, 1.0f, 0, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);//(0, 1.0f, 0, 1.0f);
break;
case SCALE_NARROW:
animation = new ScaleAnimation(1.0f,0, 1.0f, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
break;
case NULL_ANIM:
animation = null;
break;
default :
animation = new ScaleAnimation(0, 1.0f, 0, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
break;
}
return animation;
}
/**
* 旋转变化
* @param rotateType 旋转 Type ROTATE_CENTER|| ROTATE_TOP
* @return Animation
*/
public static Animation getRotateAnimation(int rotateType){
Animation animation = null;
switch (rotateType){
case ROTATE_CENTER:
animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);//(0, 360, 0.5f, 0.5f);
break;
case ROTATE_TOP:
animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0);
break;
case NULL_ANIM:
animation = null;
break;
default :
animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
break;
}
return animation;
}
/**
* 平移变化
* @param translateType 平移 Type TRANSLATE_OUT_TOP || TRANSLATE_OUT_BUTTON || TRANSLATE_OUT_LEFT || TRANSLATE_OUT_RIGHT
* @return Animation
*/
public static Animation getTranslateType(int translateType){
Animation animation = null;
switch (translateType){
case TRANSLATE_OUT_RIGHT:
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1.0f ,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
break;
case TRANSLATE_OUT_LEFT:
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1.0f,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
break;
case TRANSLATE_OUT_TOP:
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1.0f);
break;
case TRANSLATE_OUT_BUTTON :
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1.0f);
break;
case TRANSLATE_IN_RIGHT:
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0 ,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
break;
case TRANSLATE_IN_LEFT:
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
break;
case TRANSLATE_IN_TOP:
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0);
break;
case TRANSLATE_IN_BUTTON :
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0);
break;
case NULL_ANIM :
break;
default :
animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
break;
}
return animation;
}
public static Animation getFreeTranslate(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta ,int Duration){//,AnimationListener animationListener){
return getFreeTranslate(fromXDelta, toXDelta, fromYDelta, toYDelta, Duration,null);
}
public static Animation getFreeTranslate(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta ,int Duration,AnimationListener animationListener){
// Animation translateanimation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);
// //
// if (Duration == DURATION_AUTO | Duration < 0){
// //勾股定理,算出偏移量,
// double dura = Math.sqrt((Math.pow((fromXDelta - toXDelta),2)+Math.pow((fromYDelta - toYDelta),2)));
// //以960像素/秒的速度转换
// translateanimation.setDuration((long) (dura/480*500));//偏移像素 /480像素 *秒 >> 滑动的总共时间
// }else{
// translateanimation.setDuration(Duration);
// }
// if (animationListener != null){
// translateanimation.setAnimationListener(animationListener);
// }
// translateanimation.setFillAfter(true);
// return translateanimation;
return getFreeTranslate(fromXDelta, toXDelta, fromYDelta, toYDelta, Duration, animationListener, null);
}
public static Animation getFreeTranslate(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta ,int Duration,AnimationListener animationListener, Interpolator interpolator){
Animation translateanimation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);
//
if (Duration == DURATION_AUTO | Duration < 0){
//勾股定理,算出偏移量,
double dura = Math.sqrt((Math.pow((fromXDelta - toXDelta),2)+Math.pow((fromYDelta - toYDelta),2)));
//以960像素/秒的速度转换
translateanimation.setDuration((long) (dura/480*500));//偏移像素 /480像素 *秒 >> 滑动的总共时间
}else{
translateanimation.setDuration(Duration);
}
if (animationListener != null){
translateanimation.setAnimationListener(animationListener);
}
if (interpolator != null){
translateanimation.setInterpolator(interpolator);
}
translateanimation.setFillAfter(true);
return translateanimation;
}
public Animation getFreeTranslate(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta ,int Duration,AnimationListener animationListener, int interpolatortype){
return getFreeTranslate(fromXDelta, toXDelta, fromYDelta, toYDelta, Duration, animationListener, getInterpolator(interpolatortype));
}
/**
* @param interpolatorType 获得一个插补效果器
* @return Interpolator
*/
public Interpolator getInterpolator(int interpolatorType){
if (context != null){
switch (interpolatorType) {
case INTERPOLATOR_LINEAR:
return AnimationUtils.loadInterpolator(context, android.R.anim.linear_interpolator);
case INTERPOLATOR_ACCELERATE:
return AnimationUtils.loadInterpolator(context, android.R.anim.accelerate_interpolator);
case INTERPOLATOR_CYCLE:
return AnimationUtils.loadInterpolator(context, android.R.anim.cycle_interpolator);
case INTERPOLATOR_DECELERATE:
return AnimationUtils.loadInterpolator(context, android.R.anim.decelerate_interpolator);
case INTERPOLATOR_ACCELERATEDECELERATE:
return AnimationUtils.loadInterpolator(context, android.R.anim.accelerate_decelerate_interpolator);
default:
return null;
}
}
return null;
}
}