您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在Android在實現一個文字上下滾動效果,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
首先介紹第一種方法:
實現思路:自定義TextView,在TextView中加上從下到上滾動的動畫效果,然后設置循環播放;
創建一個AutoTextVieW使之繼承TextView,然后在onDraw方法中調用getHeight()方法獲取textview當前的高度。
在接下來的動畫翻轉效果中,根據這個高度設置TextView上下滾動的距離。下面是動畫實現的方法:
/** * 向上脫離屏幕的動畫效果 */ 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); } /** * 從屏幕下面向上的動畫效果 */ 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); }
接下來實現ObjectAnimator的監聽事件,在onAnimationEnd 調用setText方法,在動畫沒結束一次更新文字,并且繼續執行動畫效果
@Override public void onAnimationEnd(Animator animator) { super.setText(mText); if (mAnimOver == null) { animationOver(); } mAnimOver.start(); }
然后調用一個可以設置循環滾動的類,這里可以使用ScheduledExecutorService,也可以使用 Timer幾設置計時滾動,在更新UI的時候,調用Handler方法更新;
因為采用Timer執行定時任務時只創建一個線程,所以這里建議采用ScheduledExecutorService;
/** * 獲取數據并設置滾動播放 * @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(); } } }
定時刷新頻率較高,容易產生內存泄漏,這里采用弱引用避免這個情況發生
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) { /** * 設置當前文字 */ String text = textList.get(index); index++; if (index > textList.size() - 1) { index = 0; } textView.setAutoText(text); } } }
到此第一種方法介紹完畢。
第二種方法實現的原理和輪播圖的原理類似,輪播圖一般是左右橫向滾動,這里需要把ViewPager改成上下滑動,關于上下滑動的viewpager,可以在給github上找到;
其次輪播圖中播放的是圖片,把圖片換成文字即可;
然后同樣調用Timer或者ScheduledExecutorService使ViewPager自行滾動;
以下是代碼:
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:修改ViewPager方法實現文字滾動 * * @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(); } /** * 初始化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(); } } } /** * 開始滾動 */ 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; } /** * 設置Adapter * @param adapter */ public void setAdapter(PagerAdapter adapter){ mVerticalViewPager.setAdapter(adapter); mAdapter = adapter; dataChanged(); } private void dataChanged(){ autoPlay(); } }
關于怎么在Android在實現一個文字上下滾動效果就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。