我们经常看到一个软件的首页会有上下循环滚动的TextView,我们可以用来作为标题,或者活动信息展示,那么怎么实现的呢,废话不说,请看代码:
第一步,我们先写一个自定义view来继承TextSwitcher
public class AutoTextView extends TextSwitcher implements ViewFactory {private float mHeight;private Context mContext;//mInUp,mOutUp分别构成向下翻页的进出动private Rotate3dAnimation mInUp;private Rotate3dAnimation mOutUp;final int[] auto3d = {0x7f010000};//mInDown,mOutDown分别构成向下翻页的进出动private Rotate3dAnimation mInDown;private Rotate3dAnimation mOutDown;public AutoTextView(Context context) {this(context, null);}public AutoTextView(Context context, AttributeSet attrs) {super(context, attrs);TypedArray a = context.obtainStyledAttributes(attrs, auto3d);mHeight = a.getDimension(0, 15);a.recycle();mContext = context;init();}private void init() {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将执行OutAnimationsetInAnimation(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(300);rotation.setFillAfter(false);rotation.setInterpolator(new AccelerateInterpolator());return rotation;}//这里返回的TextView,就是我们看到的View@Overridepublic View makeView() {TextView t = new TextView(mContext);t.setGravity(Gravity.CENTER);t.setTextSize(mHeight);t.setSingleLine(true);t.setTextColor(R.color.black);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 final boolean mTurnIn;private final boolean mTurnUp;private float mCenterX;private float mCenterY;private Camera mCamera;public Rotate3dAnimation(float fromDegrees, float toDegrees, boolean turnIn, boolean turnUp) {mFromDegrees = fromDegrees;mToDegrees = toDegrees;mTurnIn = turnIn;mTurnUp = turnUp;}@Overridepublic 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;}@Overrideprotected 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);}}}
第二步,这里写个Handler来循环滚动TextView内容
private Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {switch (msg.what) {case 1001:handler.sendEmptyMessageDelayed(1001, 4000);mStringArray = new ArrayList<>();mStringArray.add(\"滚动字体一\");mStringArray.add(\"滚动字体二\");if (mStringArray.size() != 0) {int i = mLoopCount % mStringArray.size();tv_main_inform.next();tv_main_inform.setText(mStringArray.get(i));mLoopCount++;// handler.sendEmptyMessageDelayed(FLAG, 5000);}break;}};};
这里写的这个handler有点麻烦了,后面我用了一个新写法:
mStringArray = new ArrayList<>();mStringArray.add(\"滚动字体一\");mStringArray.add(\"滚动字体二\");Observable.interval(0, 4, TimeUnit.SECONDS).subscribe {if (mStringArray.size() != 0) {int i = mLoopCount % mStringArray.size();tv_main_inform.next();tv_main_inform.setText(mStringArray.get(i));mLoopCount++;// handler.sendEmptyMessageDelayed(FLAG, 5000);}}
以上就是到这里了,不当之处还望大家指教,不明白的地方可以留言