packagecom.example.animtextview;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.Camera;importandroid.graphics.Color;importandroid.graphics.Matrix;importandroid.util.AttributeSet;importandroid.view.Gravity;importandroid.view.View;importandroid.view.animation.AccelerateInterpolator;importandroid.view.animation.Animation;importandroid.view.animation.Transformation;importandroid.widget.TextSwitcher;importandroid.widget.TextView;importandroid.widget.ViewSwitcher;/*** 垂直翻滚
*@authorzhanyue
**/
public class AutoTextView extends TextSwitcher implementsViewSwitcher.ViewFactory {private floatmHeight;privateContext mContext;//mInUp,mOutUp分别构成向下翻页的进出动画
privateRotate3dAnimation mInUp;privateRotate3dAnimation mOutUp;//mInDown,mOutDown分别构成向下翻页的进出动画
privateRotate3dAnimation mInDown;privateRotate3dAnimation mOutDown;publicAutoTextView(Context context) {this(context, null);//TODO Auto-generated constructor stub
}publicAutoTextView(Context context, AttributeSet attrs) {super(context, attrs);//TODO Auto-generated constructor stub
TypedArray a =context.obtainStyledAttributes(attrs, R.styleable.auto3d);//mHeight = a.getDimension(R.styleable.auto3d_textSize, 36);
mHeight=20;
a.recycle();
mContext=context;
init();
}private voidinit() {//TODO Auto-generated method stub
setFactory(this);
mInUp= createAnim(-90, 0 , true, true);
mOutUp= createAnim(0, 90, false, true);
mInDown= createAnim(90, 0 , true , false);
mOutDown= createAnim(0, -90, false, false);//TextSwitcher主要用于文件切换,比如 从文字A 切换到 文字 B,//setInAnimation()后,A将执行inAnimation,//setOutAnimation()后,B将执行OutAnimation
setInAnimation(mInUp);
setOutAnimation(mOutUp);
}private Rotate3dAnimation createAnim(float start, float end, boolean turnIn, booleanturnUp){final Rotate3dAnimation rotation = newRotate3dAnimation(start, end, turnIn, turnUp);//动画持续时间
rotation.setDuration(300);
rotation.setFillAfter(false);
rotation.setInterpolator(newAccelerateInterpolator());returnrotation;
}public voidsetData(){
}//这里返回的TextView,就是我们看到的View
@OverridepublicView makeView() {//TODO Auto-generated method stub
TextView t = newTextView(mContext);
t.setGravity(Gravity.CENTER);
t.setTextSize(mHeight);
t.setMaxLines(2);
t.setPadding(0, 5, 0, 5);//设置文字颜色
t.setTextColor(Color.WHITE);returnt;
}//定义动作,向下滚动翻页
public voidprevious(){if(getInAnimation() !=mInDown){
setInAnimation(mInDown);
}if(getOutAnimation() !=mOutDown){
setOutAnimation(mOutDown);
}
}//定义动作,向上滚动翻页
public voidnext(){if(getInAnimation() !=mInUp){
setInAnimation(mInUp);
}if(getOutAnimation() !=mOutUp){
setOutAnimation(mOutUp);
}
}class Rotate3dAnimation extendsAnimation {private final floatmFromDegrees;private final floatmToDegrees;private floatmCenterX;private floatmCenterY;private final booleanmTurnIn;private final booleanmTurnUp;privateCamera mCamera;public Rotate3dAnimation(float fromDegrees, float toDegrees, boolean turnIn, booleanturnUp) {
mFromDegrees=fromDegrees;
mToDegrees=toDegrees;
mTurnIn=turnIn;
mTurnUp=turnUp;
}
@Overridepublic void initialize(int width, int height, int parentWidth, intparentHeight) {super.initialize(width, height, parentWidth, parentHeight);
mCamera= newCamera();
mCenterY= getHeight() / 2;
mCenterX= getWidth() / 2;
}
@Overrideprotected void applyTransformation(floatinterpolatedTime, Transformation t) {final float fromDegrees =mFromDegrees;float degrees = fromDegrees + ((mToDegrees - fromDegrees) *interpolatedTime);final float centerX =mCenterX ;final float centerY =mCenterY ;final Camera camera =mCamera;final int derection = mTurnUp ? 1: -1;final Matrix matrix =t.getMatrix();
camera.save();if(mTurnIn) {
camera.translate(0.0f, derection *mCenterY * (interpolatedTime - 1.0f), 0.0f);
}else{
camera.translate(0.0f, derection *mCenterY * (interpolatedTime), 0.0f);
}
camera.rotateX(degrees);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
}