Use the android control TextSwitcher to scroll up and down Textview,
You can often see many up and down textviews on the app, which can be directly implemented using TextSwitcher.
At first, I wrote a custom view for implementation, and then found that the official(: Invalid "parameters).
Controls include ImageSwitcher and ViewSwitcher. TextSwitcher and ImageSwitcher both inherit from ViewSwitcher, so the methods used are the same, but the objects are different.
Effect
Only a little gif is captured.
Use Layout
Place a TextSwitcher directly.
Called in activity
TextSwitcher textSwitcher = (TextSwitcher) findViewById (R. id. textSwitcher); textSwitcher. setFactory (new ViewSwitcher. viewFactory () {@ Override public View makeView () {return new TextView (context) ;}}); List
Texts = new ArrayList <> (); for (int I = 0; I <10; I ++) {texts. add ("loop ..... "+ I);} textSwitcher. setText (texts. get (0 ));
The simplest way to set attributes is to rewrite ViewFactory. Other ImageSwitcher and ViewSwitcher are the same, but the view setting is different!
When a loop is required, you only need to set a Handler to change the value in the setText of textSwitcher each time.
For example
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); } };
In
textSwitcher.setInAnimation();textSwitcher.setOutAnimation();
Method to Control the input and output animations of textSwitcher
Implement animation switching and Automatic loop
Create a class to uniformly manage textSwitcher animations and loops
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
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
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
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)); }}
The implementation is simple, that is, an in and out animation and Handler are made to control the loop. I will not repeat it here.
Use the following
List
Texts = new ArrayList <> (); for (int I = 0; I <10; I ++) {texts. add ("loop ..... "+ I);} textSwitcher. setFactory (new ViewSwitcher. viewFactory () {@ Override public View makeView () {TextView t = new TextView (mContext); return t ;}}); new TextSwitcherAnimation (textSwitcher, texts ). create ();