Android实现文字滚动效果
Android实现文字滚动效果,自己写了个timer小计时器,textview文字上下翻动效果:
publicclassAutoTextViewextendsTextSwitcherimplements
ViewSwitcher.ViewFactory{
privatefloatmHeight;
privateContextmContext;
//mInUp,mOutUp分别构成向下翻页的进出动画
privateRotate3dAnimationmInUp;
privateRotate3dAnimationmOutUp;
//mInDown,mOutDown分别构成向下翻页的进出动画
privateRotate3dAnimationmInDown;
privateRotate3dAnimationmOutDown;
publicAutoTextView(Contextcontext){
this(context,null);
//TODOAuto-generatedconstructorstub
}
publicAutoTextView(Contextcontext,AttributeSetattrs){
super(context,attrs);
//TODOAuto-generatedconstructorstub
TypedArraya=context.obtainStyledAttributes(attrs,R.styleable.auto3d);
mHeight=a.getDimension(R.styleable.auto3d_textSize,16);
a.recycle();
mContext=context;
init();
}
privatevoidinit(){
//TODOAuto-generatedmethodstub
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);
}
privateRotate3dAnimationcreateAnim(floatstart,floatend,booleanturnIn,booleanturnUp){
finalRotate3dAnimationrotation=newRotate3dAnimation(start,end,turnIn,turnUp);
rotation.setDuration(800);
rotation.setFillAfter(false);
rotation.setInterpolator(newAccelerateInterpolator());
returnrotation;
}
//这里返回的TextView,就是我们看到的View
@Override
publicViewmakeView(){
//TODOAuto-generatedmethodstub
TextViewt=newTextView(mContext);
t.setGravity(Gravity.CENTER);
t.setTextSize(16);
t.setMaxLines(1);
t.setTextColor(mContext.getResources().getColor(R.color.textColor));
returnt;
}
//定义动作,向下滚动翻页
publicvoidprevious(){
if(getInAnimation()!=mInDown){
setInAnimation(mInDown);
}
if(getOutAnimation()!=mOutDown){
setOutAnimation(mOutDown);
}
}
//定义动作,向上滚动翻页
publicvoidnext(){
if(getInAnimation()!=mInUp){
setInAnimation(mInUp);
}
if(getOutAnimation()!=mOutUp){
setOutAnimation(mOutUp);
}
}
classRotate3dAnimationextendsAnimation{
privatefinalfloatmFromDegrees;
privatefinalfloatmToDegrees;
privatefloatmCenterX;
privatefloatmCenterY;
privatefinalbooleanmTurnIn;
privatefinalbooleanmTurnUp;
privateCameramCamera;
publicRotate3dAnimation(floatfromDegrees,floattoDegrees,booleanturnIn,booleanturnUp){
mFromDegrees=fromDegrees;
mToDegrees=toDegrees;
mTurnIn=turnIn;
mTurnUp=turnUp;
}
@Override
publicvoidinitialize(intwidth,intheight,intparentWidth,intparentHeight){
super.initialize(width,height,parentWidth,parentHeight);
mCamera=newCamera();
mCenterY=getHeight()/2;
mCenterX=getWidth()/2;
}
@Override
protectedvoidapplyTransformation(floatinterpolatedTime,Transformationt){
finalfloatfromDegrees=mFromDegrees;
floatdegrees=fromDegrees+((mToDegrees-fromDegrees)*interpolatedTime);
finalfloatcenterX=mCenterX;
finalfloatcenterY=mCenterY;
finalCameracamera=mCamera;
finalintderection=mTurnUp?1:-1;
finalMatrixmatrix=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);
}
}
demo下载链接:http://xiazai.jb51.net/201611/yuanma/AndroidTextView(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。