前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >安卓滚动字幕以及TextSwitcher、ImageSwitcher使用

安卓滚动字幕以及TextSwitcher、ImageSwitcher使用

作者头像
先知先觉
发布2019-01-21 11:44:13
2.1K0
发布2019-01-21 11:44:13
举报

项目源码下载:https://github.com/libin7278/TextSwitcher

TextSwitcher使用方法

四部曲

1:布局并初始化

代码语言:javascript
复制
 <TextSwitcher
 android:id="@+id/ts"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>

2:设置工厂模式

代码语言:javascript
复制
 ts.setFactory(this);
 implements ViewSwitcher.ViewFactory
 实现makeView()方法

 @Override
 public View makeView() {
 TextView textView = new TextView(this);
 textView.setTextSize(36);
 return textView;
 }

3.使用

每调一setText()方法会实现自动切换,比如倒计时之类的效果不错

这里写图片描述
这里写图片描述

4.酷炫切换效果

代码语言:javascript
复制
setInAnimation()
setOutAnimation()
这里写图片描述
这里写图片描述

ImageSwitcher使用方法

四部曲

1:布局并初始化

代码语言:javascript
复制
 <ImageSwitcher
        android:id="@+id/ImageSwitcher"
        android:layout_width="300dp"
        android:layout_height="300dp"/>

2:设置工厂模式

代码语言:javascript
复制
public class Switch3Activity extends Activity implements ViewSwitcher.ViewFactory {

ImageSwitcher.setFactory(this);

}

@Override
    public View makeView() {
        ImageView imageView = new ImageView(this);
        return imageView;
    }

3.使用setImageResource

每调一次setImageResource就会更改图片

代码语言:javascript
复制
ImageSwitcher.setImageResource(images[number%images.length]);
这里写图片描述
这里写图片描述

4.酷炫切换效果

代码语言:javascript
复制
setInAnimation()
setOutAnimation()

竖直字幕滚动效果

这里写图片描述
这里写图片描述
代码语言:javascript
复制
/**
 * 自动垂直滚动的TextView
 */
public class AutoVerticalScrollTextView extends TextSwitcher implements ViewSwitcher.ViewFactory {

    private Context mContext;

    //mInUp,mOutUp分别构成向下翻页的进出动画
    private Rotate3dAnimation mInUp;
    private Rotate3dAnimation mOutUp;

    public AutoVerticalScrollTextView(Context context) {
        this(context, null);
    }

    public AutoVerticalScrollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mContext = context;
        init();

    }

    private void init() {

        setFactory(this);
        setInAnimation(showTextAnimation());//当View显示时动画资源ID
        setOutAnimation(dismessTextAnimation());//当View隐藏是动画资源ID。

    }

    private Rotate3dAnimation createAnim( boolean turnIn, boolean turnUp){

        Rotate3dAnimation rotation = new Rotate3dAnimation(turnIn, turnUp);
        rotation.setDuration(1200);//执行动画的时间
        rotation.setFillAfter(false);//是否保持动画完毕之后的状态
        rotation.setInterpolator(new AccelerateInterpolator());//设置加速模式

        return rotation;
    }


    //这里返回的TextView,就是我们看到的View,可以设置自己想要的效果
    public View makeView() {

        TextView textView = new TextView(mContext);
        textView.setGravity(Gravity.LEFT);
        textView.setTextSize(20);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(Color.WHITE);
        textView.setLineSpacing(20,1);

        return textView;

    }

    //定义动作,向上滚动翻页
    public void next(){
        //显示动画
        if(getInAnimation() != mInUp){
            setInAnimation(showTextAnimation());
        }
        //隐藏动画
        if(getOutAnimation() != mOutUp){
            setOutAnimation(dismessTextAnimation());
        }
    }

    class Rotate3dAnimation extends Animation {
        private float mCenterX;
        private float mCenterY;
        private final boolean mTurnIn;
        private final boolean mTurnUp;
        private Camera mCamera;

        public Rotate3dAnimation(boolean turnIn, boolean turnUp) {
            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() ;
            mCenterX = getWidth() ;
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {

            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.getMatrix(matrix);
            camera.restore();

            matrix.preTranslate(-centerX, -centerY);
            matrix.postTranslate(centerX, centerY);
        }
    }

    public AnimationSet showTextAnimation(){
        mInUp = createAnim(true, true);
        AnimationSet animationSet = new AnimationSet(true);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
        animationSet.addAnimation(alphaAnimation);
        animationSet.addAnimation(mInUp);
        animationSet.setDuration(1000);

        return animationSet;
    }

    public AnimationSet dismessTextAnimation(){
        mOutUp = createAnim(false, true);
        AnimationSet animationSet = new AnimationSet(true);
        AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
        animationSet.addAnimation(alphaAnimation);
        animationSet.addAnimation(mOutUp);
        animationSet.setDuration(1000);

        return animationSet;
    }
}

如想要了解更多项目详细代码可以访问github: https://github.com/libin7278/TextSwitcher

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年12月05日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • TextSwitcher使用方法
    • 四部曲
      • 1:布局并初始化
      • 2:设置工厂模式
      • 3.使用
      • 4.酷炫切换效果
  • ImageSwitcher使用方法
    • 四部曲
      • 1:布局并初始化
      • 2:设置工厂模式
      • 3.使用setImageResource
      • 4.酷炫切换效果
  • 竖直字幕滚动效果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档