Regarding Android's ability to scroll up and down text, I currently have two ways to implement it:
One is to add a flipped animation effect to the TextView and then set a loop scroll; the other is to rewrite the scrolling direction of the ViewPager, so that it scrolls from bottom to top, and set a loop scroll;
First introduceThe first method:
Implementation idea: Customize the TextView, add an animation effect from bottom to top to the TextView, and then set loop playback;
Create an AutoTextVieW to inherit the TextView, and then call the getHeight() method in the onDraw method to get the current height of the textview.
In the next animation flip effect, set the distance to scroll up and down of the TextView according to this height. Here is the animation implementation method:
/** * Animation effect that goes upwards from the screen */ private void animationStart() { ObjectAnimator translate = (this, "translationY", 0, -height); ObjectAnimator alpha = (this, "alpha", 1f, 0f); mAnimStart = new AnimatorSet(); (translate).with(alpha); (DURATION); (this); } /** * Animation effect up from below the screen */ public void animationOver() { ObjectAnimator translate = (this, "translationY", height, 0); ObjectAnimator alpha = (this, "alpha", 0f, 1f); mAnimOver = new AnimatorSet(); (translate).with(alpha); (DURATION); }
Next, implement the ObjectAnimator listening 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) { (mText); if (mAnimOver == null) { animationOver(); } (); }
Then call a class that can set loop scrolling. Here you can use ScheduledExecutorService, or you can set timing scrolling using Timer. When updating the UI, call the Handler method to update;
Because only one thread is created when using Timer to execute timing tasks, it is recommended to use ScheduledExecutorService;
/** * Get data and set scrolling playback * @param textView * @param list * @param autoPlayTime */ public void getTextData(final IdeaAutoTextview textView, List<String> list, int autoPlayTime) { = textView; = list; if (autoPlayTime != 0) { scheduledExecutorService = (); (new WeakTimerTask(this), autoPlayTime, autoPlayTime, ); } } private TimeTaskHandler mHandler = new TimeTaskHandler(this); private static class WeakTimerTask extends TimerTask { private WeakReference<IdeaAutoTextview> autoTextReference; public WeakTimerTask(IdeaAutoTextview mautoText) { = new WeakReference<>(mautoText); } @Override public void run() { IdeaAutoTextview autoText = (); if (autoText != null) { if (()) { (0); } } else { cancel(); } } }
The timing refresh frequency is high, which is easy to cause memory leakage. Weak references are used here to avoid this situation.
private final class TimeTaskHandler extends Handler { private WeakReference<IdeaAutoTextview> autoTextReference; public TimeTaskHandler(IdeaAutoTextview autoText) { = new WeakReference<>(autoText); } @Override public void handleMessage(Message msg) { IdeaAutoTextview autoText = (); if (autoText!=null) { /** * Set the current text */ String text = (index); index++; if (index > () - 1) { index = 0; } (text); } } }
The first method has been introduced here.
The second methodThe implementation principle is similar to the principle of the carousel diagram. The carousel diagram generally scrolls left and right horizontally. Here you need to change the ViewPager to slide up and down. The viewpager for sliding up and down can be found on Github;
Secondly, the picture is played in the carousel picture, just change the picture to text;
Then call Timer or ScheduledExecutorService to make the ViewPager scroll by itself;
Here is the code:
package ; import ; import ; import ; import .; import .; import ; import ; import ; import ; import ; import ; import ; /** * todo: Modify the ViewPager method to achieve text scrolling * * @author: Create by qjj * @email: gxuqjj@ */ 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(); } /** * Initialize view */ private void initView(){ if(mVerticalViewPager!=null){ removeView(mVerticalViewPager); } mVerticalViewPager = new VerticalViewPager(getContext()); (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) { = new WeakReference<>(autoViewpager); } @Override public void handleMessage(Message msg) { AutoViewpager autoViewpager = (); int cur = ().getCurrentItem()+1; if(cur>= ()){ cur=0; } ().setCurrentItem(cur); } } private TimeTaskHandler mHandler = new TimeTaskHandler(this); private static class WeakTimerTask extends TimerTask { private WeakReference<AutoViewpager> mRollPagerViewWeakReference; public WeakTimerTask(AutoViewpager mAutoViewpager) { = new WeakReference<>(mAutoViewpager); } @Override public void run() { AutoViewpager autoViewpager = (); if (autoViewpager !=null){ if(()){ (0); } }else{ cancel(); } } } /** * Start scrolling */ private void autoPlay(){ if(autoPlayTime<=0||mAdapter == null||()<=1){ return; } scheduledExecutorService = (); (new WeakTimerTask(this), autoPlayTime, autoPlayTime, ); } public void setAutoTime(int autoPlayTime){ = autoPlayTime; autoPlay(); } /** * viewpager * @return */ public ViewPager getViewPager() { return mVerticalViewPager; } /** * Set up Adapter * @param adapter */ public void setAdapter(PagerAdapter adapter){ (adapter); mAdapter = adapter; dataChanged(); } private void dataChanged(){ autoPlay(); } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.