AndroID 实现文字滚动效果,自己写了个timer小计时器,textvIEw文字上下翻动效果:
public class autoTextVIEw extends TextSwitcher implements VIEwSwitcher.VIEwFactory { private float mHeight; private Context mContext; //mInUp,mOutUp分别构成向下翻页的进出动画 private Rotate3dAnimation mInUp; private Rotate3dAnimation mOutUp; //mInDown,mOutDown分别构成向下翻页的进出动画 private Rotate3dAnimation mInDown; private Rotate3dAnimation mOutDown; public autoTextVIEw(Context context) { this(context,null); // Todo auto-generated constructor stub } public autoTextVIEw(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,16); a.recycle(); mContext = context; init(); } private voID init() { // Todo auto-generated method stub setFactory(this); mInUp = createAnim(-90,true,true); mOutUp = createAnim(0,90,false,true); mInDown = createAnim(90,false); mOutDown = createAnim(0,-90,false); //TextSwitcher主要用于文件切换,比如 从文字A 切换到 文字 B, //setInAnimation()后,A将执行inAnimation, //setoutAnimation()后,B将执行OutAnimation setInAnimation(mInUp); setoutAnimation(mOutUp); } private Rotate3dAnimation createAnim(float start,float end,boolean turnIn,boolean turnUp){ final Rotate3dAnimation rotation = new Rotate3dAnimation(start,end,turnIn,turnUp); rotation.setDuration(800); rotation.setFillAfter(false); rotation.setInterpolator(new AccelerateInterpolator()); return rotation; } //这里返回的TextVIEw,就是我们看到的VIEw @OverrIDe public VIEw makeVIEw() { // Todo auto-generated method stub TextVIEw t = new TextVIEw(mContext); t.setGravity(Gravity.CENTER); t.setTextSize(16); t.setMaxlines(1); t.setTextcolor(mContext.getResources().getcolor(R.color.textcolor)); return t; } //定义动作,向下滚动翻页 public voID prevIoUs(){ if(getInAnimation() != mInDown){ setInAnimation(mInDown); } if(getoutAnimation() != mOutDown){ setoutAnimation(mOutDown); } } //定义动作,向上滚动翻页 public voID next(){ if(getInAnimation() != mInUp){ setInAnimation(mInUp); } if(getoutAnimation() != mOutUp){ setoutAnimation(mOutUp); } } class Rotate3dAnimation extends Animation { private final float mFromdegrees; private final float mTodegrees; private float mCenterX; private float mCenterY; private final boolean mTurnIn; private final boolean mTurnUp; private Camera mCamera; public Rotate3dAnimation(float fromdegrees,float todegrees,boolean turnUp) { mFromdegrees = fromdegrees; mTodegrees = todegrees; mTurnIn = turnIn; mTurnUp = turnUp; } @OverrIDe public voID initialize(int wIDth,int height,int parentWIDth,int parentHeight) { super.initialize(wIDth,height,parentWIDth,parentHeight); mCamera = new Camera(); mCenterY = getHeight() / 2; mCenterX = getWIDth() / 2; } @OverrIDe protected voID applytransformation(float interpolatedTime,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); } }
demo下载链接:http://xiazai.jb51.net/201611/yuanma/AndroidTextView(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
以上是内存溢出为你收集整理的Android实现文字滚动效果全部内容,希望文章能够帮你解决Android实现文字滚动效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)