安卓控件TextSwitcher的使用(实现Textview的上下滚动)

经常可以在app上面看到许多的上下滚动textview,是可以直接使用TextSwitcher直接实现的。
开始还写了个自定义view来实现,然后发现官方原来有(:з」∠)

控件还有ImageSwitcher和ViewSwitcher。TextSwitcher和ImageSwitcher都是继承于ViewSwitcher的,所以使用方法都一样只不过对象不一样罢了。


效果

滚动效果

只是稍微截取了gif


使用

布局

<TextSwitcher
     android:id="@+id/textSwitcher"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">
 </TextSwitcher>

直接放置一个TextSwitcher即可

activity中调用

TextSwitcher textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
    @Override
    public View makeView() {
        return new TextView(context);
    }
});
List<String> texts = new ArrayList<>();
for (int i = 0; i < 10; i++) {
    texts.add("循环....."+i);
}
textSwitcher.setText(texts.get(0));

最简单的设置属性,其中只要实现ViewFactory的重写即可,其他的ImageSwitcher和ViewSwitcher都是一样的,只不过设置的view不同!

当需要循环的时候只需要设置一个Handler来每次改变textSwitcher的setText中的值。
比如

    private Handler handler = new Handler();
    private Runnable task = new Runnable() {
        @Override
        public void run() {
            marker = ++marker % texts.size();
            textSwitcher.setText(texts.get(marker));
            handler.postDelayed(task, 4000);
        }
    };

在然后是通过调用

textSwitcher.setInAnimation();
textSwitcher.setOutAnimation();

方法来控制textSwitcher的进出动画

实现动画切换并且自动循环

创建一个类来统一管理textSwitcher的动画和循环

import android.os.Handler;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.TextSwitcher;

import java.util.List;

/**
 * TextSwitcherAnimation
 * Author: gjn.
 * Time: 2018/2/22.
 */

public class TextSwitcherAnimation {
    private static final int DURATION = 1000;

    private TextSwitcher textSwitcher;
    private List<String> texts;
    private int marker;
    private AnimationSet InAnimationSet;
    private AnimationSet OutAnimationSet;

    private int delayTime = 2000;
    private Handler handler = new Handler();
    private Runnable task = new Runnable() {
        @Override
        public void run() {
            nextView();
            handler.postDelayed(task, delayTime * 2);
        }
    };

    public TextSwitcherAnimation(TextSwitcher textSwitcher, List<String> texts) {
        this.textSwitcher = textSwitcher;
        this.texts = texts;
    }

    public void start() {
        stop();
        handler.postDelayed(task, delayTime);
    }

    public void stop(){
        handler.removeCallbacks(task);
    }

    public int getMarker() {
        return marker;
    }

    public TextSwitcherAnimation setTexts(List<String> texts) {
        this.texts = texts;
        return this;
    }

    public void setDelayTime(int delayTime) {
        this.delayTime = delayTime;
    }

    public void create() {
        marker = 0;
        if (texts == null){
            Log.w("TextSwitcherAnimation", "texts is null");
            return;
        }
        if (textSwitcher == null) {
            Log.w("TextSwitcherAnimation", "textSwitcher is null");
            return;
        }
        textSwitcher.setText(texts.get(0));
        createAnimation();
        textSwitcher.setInAnimation(InAnimationSet);
        textSwitcher.setOutAnimation(OutAnimationSet);
        start();
    }

    private void createAnimation() {
        AlphaAnimation alphaAnimation;
        TranslateAnimation translateAnimation;

        int h = textSwitcher.getHeight();
        if (h <= 0) {
            textSwitcher.measure(0,0);
            h = textSwitcher.getMeasuredHeight();
        }

        InAnimationSet = new AnimationSet(true);
        OutAnimationSet = new AnimationSet(true);

        alphaAnimation = new AlphaAnimation(0,1);
        translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, h, Animation.ABSOLUTE, 0);
        InAnimationSet.addAnimation(alphaAnimation);
        InAnimationSet.addAnimation(translateAnimation);
        InAnimationSet.setDuration(DURATION);

        alphaAnimation = new AlphaAnimation(1,0);
        translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -h);
        OutAnimationSet.addAnimation(alphaAnimation);
        OutAnimationSet.addAnimation(translateAnimation);
        OutAnimationSet.setDuration(DURATION);
    }

    private void nextView() {
        marker = ++marker % texts.size();
        textSwitcher.setText(texts.get(marker));
    }
}

实现很简单,就是做了一个进出动画和Handler来控制循环。这边就不复述了。

使用如下

        List<String> texts = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            texts.add("循环....."+i);
        }
        textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                TextView t = new TextView(mContext);
                return t;
            }
        });
        new TextSwitcherAnimation(textSwitcher,texts).create();

猜你喜欢

转载自blog.csdn.net/g777520/article/details/79348688