Android Realizes Text Scroll Up and Down

  • 2021-10-25 07:55:55
  • OfStack

About Android to achieve text scrolling up and down this function, I currently have two ways to achieve it:

One is to add flip animation effect in TextView, and then set circular scrolling; One is to rewrite the scrolling direction of ViewPager, make it scroll from bottom to top, and set circular scrolling;

First, introduce the first method:

Implementation idea: Customize TextView, add the animation effect from bottom to top in TextView, and then set the loop play;

Create an AutoTextVieW that inherits TextView, and then call the getHeight () method in the onDraw method to get the current height of textview.
In the next animation flip effect, set the distance that TextView scrolls up and down according to this height. The following is the animation implementation method:


/**
 *  Animation effect of upward off-screen 
 */
private void animationStart() {
  ObjectAnimator translate = ObjectAnimator.ofFloat(this, "translationY", 0, -height);
  ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "alpha", 1f, 0f);
  mAnimStart = new AnimatorSet();
  mAnimStart.play(translate).with(alpha);
  mAnimStart.setDuration(DURATION);
  mAnimStart.addListener(this);

}

/**
 *  Animation effect from bottom to top of screen 
 */
public void animationOver() {
  ObjectAnimator translate = ObjectAnimator.ofFloat(this, "translationY", height, 0);
  ObjectAnimator alpha = ObjectAnimator.ofFloat(this, "alpha", 0f, 1f);
  mAnimOver = new AnimatorSet();
  mAnimOver.play(translate).with(alpha);
  mAnimOver.setDuration(DURATION);

}

Next, implement the ObjectAnimator monitoring event, call the setText method in onAnimationEnd, update the text once before the animation is finished, and continue to execute the animation effect


@Override
public void onAnimationEnd(Animator animator) {
  super.setText(mText);
  if (mAnimOver == null) {
    animationOver();

  }

  mAnimOver.start();
}

Then call a class that can set circular scrolling. Here, you can use ScheduledExecutorService or Timer to set timing scrolling. When updating UI, call Handler method to update;

Because only one thread is created when executing timed tasks with Timer, ScheduledExecutorService; is recommended here;


/**
 *  Get data and set scroll playback 
 * @param textView
 * @param list
 * @param autoPlayTime
 */
public void getTextData(final IdeaAutoTextview textView, List<String> list, int autoPlayTime) {
  this.textView = textView;
  this.textList = list;

  if (autoPlayTime != 0) {

    scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    scheduledExecutorService.scheduleWithFixedDelay(new WeakTimerTask(this), autoPlayTime, autoPlayTime, TimeUnit.SECONDS);
  }
}


private TimeTaskHandler mHandler = new TimeTaskHandler(this);

private static class WeakTimerTask extends TimerTask {
  private WeakReference<IdeaAutoTextview> autoTextReference;

  public WeakTimerTask(IdeaAutoTextview mautoText) {
    this.autoTextReference = new WeakReference<>(mautoText);
  }

  @Override
  public void run() {
    IdeaAutoTextview autoText = autoTextReference.get();
    if (autoText != null) {
      if (autoText.isShown()) {
        autoText.mHandler.sendEmptyMessage(0);
      }
    } else {
      cancel();
    }
  }
}

The timing refresh frequency is high, which is easy to produce memory leakage. Here, weak reference is used to avoid this situation


private final class TimeTaskHandler extends Handler {
  private WeakReference<IdeaAutoTextview> autoTextReference;

  public TimeTaskHandler(IdeaAutoTextview autoText) {
    this.autoTextReference = new WeakReference<>(autoText);
  }

  @Override
  public void handleMessage(Message msg) {
    IdeaAutoTextview autoText = autoTextReference.get();

    if (autoText!=null)
    {
      /**
       *  Set the current text 
       */
      String text = textList.get(index);
      index++;
      if (index > textList.size() - 1) {
        index = 0;
      }
      textView.setAutoText(text);
    }


  }
}

At this point, the first method is introduced.

The principle of the second method is similar to the principle of carousel. Carousel 1 is generally left and right horizontal rolling. Here, ViewPager needs to be changed to sliding up and down. viewpager sliding up and down can be found on github;
Secondly, the picture is played in the carousel picture, and the picture can be replaced with text;
Then call Timer or ScheduledExecutorService to make ViewPager scroll by itself;
Here's the code:


package com.idea.idea.viewutils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import java.lang.ref.WeakReference;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * todo : Modifications ViewPager Method to realize text scrolling 
 *
 * @author: Create by qjj
 * @email: gxuqjj@163.com
 */
public class AutoViewpager extends RelativeLayout{

  private VerticalViewPager mVerticalViewPager;
  private PagerAdapter mAdapter;
  private int autoPlayTime;
  private ScheduledExecutorService scheduledExecutorService;

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

  public AutoViewpager(Context context, AttributeSet attrs) {
    this(context, attrs, 0);

  }

  public AutoViewpager(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initView();
  }


  /**
   *  Initialization view
   */
  private void initView(){
    if(mVerticalViewPager!=null){
      removeView(mVerticalViewPager);
    }
    mVerticalViewPager = new VerticalViewPager(getContext());
    mVerticalViewPager.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    addView(mVerticalViewPager);

  }

  private final static class TimeTaskHandler extends Handler {
    private WeakReference<AutoViewpager> mRollPagerViewWeakReference;

    public TimeTaskHandler(AutoViewpager autoViewpager) {
      this.mRollPagerViewWeakReference = new WeakReference<>(autoViewpager);
    }

    @Override
    public void handleMessage(Message msg) {
      AutoViewpager autoViewpager = mRollPagerViewWeakReference.get();
      int cur = autoViewpager.getViewPager().getCurrentItem()+1;
      if(cur>= autoViewpager.mAdapter.getCount()){
        cur=0;
      }
      autoViewpager.getViewPager().setCurrentItem(cur);

    }
  }
  private TimeTaskHandler mHandler = new TimeTaskHandler(this);

  private static class WeakTimerTask extends TimerTask {
    private WeakReference<AutoViewpager> mRollPagerViewWeakReference;

    public WeakTimerTask(AutoViewpager mAutoViewpager) {
      this.mRollPagerViewWeakReference = new WeakReference<>(mAutoViewpager);
    }

    @Override
    public void run() {
      AutoViewpager autoViewpager = mRollPagerViewWeakReference.get();
      if (autoViewpager !=null){
        if(autoViewpager.isShown()){
          autoViewpager.mHandler.sendEmptyMessage(0);
        }
      }else{
        cancel();
      }
    }
  }

  /**
   *  Start scrolling 
   */
  private void autoPlay(){
    if(autoPlayTime<=0||mAdapter == null||mAdapter.getCount()<=1){
      return;
    }

    scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    scheduledExecutorService.scheduleWithFixedDelay(new WeakTimerTask(this), autoPlayTime, autoPlayTime, TimeUnit.SECONDS);
  }

  public void setAutoTime(int autoPlayTime){
    this.autoPlayTime = autoPlayTime;
    autoPlay();
  }


  /**
   * viewpager
   * @return
   */
  public ViewPager getViewPager() {
    return mVerticalViewPager;
  }

  /**
   *  Settings Adapter
   * @param adapter
   */
  public void setAdapter(PagerAdapter adapter){
    mVerticalViewPager.setAdapter(adapter);
    mAdapter = adapter;
    dataChanged();
  }
  private void dataChanged(){
    autoPlay();
  }

}

Related articles: