android實(shí)現(xiàn)滾動(dòng)文本效果
本文實(shí)例為大家分享了android實(shí)現(xiàn)滾動(dòng)文本效果的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
實(shí)現(xiàn)方法
直接上代碼
首先是一個(gè)自定義layout,繼承自FrameLayout
public class AnimationTextLayout extends FrameLayout { private static final String TAG = "AnimationTextLayout"; private List<String> tipList; private List<Integer> displayList; private List<TextView> viewList; private List<VirtualPos> virtualPosList; private double deviantAngle = 0; public AnimationTextLayout(@NonNull Context context) { super(context); initView(); } public AnimationTextLayout(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(); } public AnimationTextLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); } private void initView() { viewList = new ArrayList<>(); displayList = new ArrayList<>(); virtualPosList = new ArrayList<>(); } /** * 設(shè)置需要顯示的數(shù)據(jù) * * @param data 需要顯示的數(shù)據(jù) */ public void setData(List<String> data) { this.tipList = data; initTips(); } private void initTips() { while (tipList.size() > viewList.size()) { addTipsView(); } refreshTips(); initVirPos(); post(new Runnable() { @Override public void run() { initPosition(); requestLayout(); } }); } /** * 計(jì)算虛擬位置 */ private void initVirPos() { virtualPosList.clear(); for (int i = 0; i < viewList.size(); i++) { double angle = (Math.PI * ((double) i / viewList.size())*2)+(deviantAngle*Math.PI*2); if (angle>Math.PI*2){ angle-=Math.PI*2; } VirtualPos virtualPos = new VirtualPos(); virtualPos.text = tipList.get(i); virtualPos.z = 100 * Math.sin(angle); virtualPos.y = 100 * Math.cos(angle); virtualPosList.add(virtualPos); } } /** * 將虛擬位置轉(zhuǎn)化為實(shí)際高度和位置 */ private void initPosition() { for (int i = 0; i < viewList.size(); i++) { TextView textView = viewList.get(i); VirtualPos virtualPos=virtualPosList.get(i); int realY = (int) ((100 - virtualPos.y)/200 *getMeasuredHeight()); FrameLayout.LayoutParams layoutParams = (LayoutParams) textView.getLayoutParams(); layoutParams.topMargin = realY; if (virtualPos.z>=0){ float textSize= (float) (( virtualPos.z)/100)*20+5; textView.setTextSize(textSize); textView.setVisibility(VISIBLE); }else { textView.setVisibility(GONE); } Log.d(TAG, "initPosition: y=="+realY); } } private void refreshTips() { for (int i = 0; i < viewList.size(); i++) { TextView tip = viewList.get(i); if (i < tipList.size()) { tip.setVisibility(VISIBLE); tip.setText(tipList.get(i)); continue; } tip.setVisibility(GONE); } } private TextView addTipsView() { TextView textView = new TextView(getContext()); textView.setTextSize(COMPLEX_UNIT_DIP, 12); textView.setTextColor(Color.parseColor("#444444")); textView.setPadding(ConvertUtil.dp2px(5), ConvertUtil.dp2px(3), ConvertUtil.dp2px(5), ConvertUtil.dp2px(3)); LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.gravity=Gravity.CENTER_HORIZONTAL; layoutParams.rightMargin = ConvertUtil.dp2px(6); addView(textView, layoutParams); viewList.add(textView); return textView; } /** * 虛擬位置,最大x,y,z 最大值為100,最小值為-100 */ public static class VirtualPos { public double x; public double y; public double z; public String text; } /** * 滾動(dòng)的偏移值 * @param deviantAngle 最大為1 */ public void setDeviantAngle(float deviantAngle) { this.deviantAngle = deviantAngle; initVirPos(); initPosition(); } public double getDeviantAngle() { return deviantAngle; } }
調(diào)用方布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".AnimationTextActivity"> <com.lanlengran.test.view.AnimationTextLayout android:id="@+id/anim_text_layout" android:layout_width="match_parent" android:background="@color/colorAccent" android:layout_height="400dp"/> </FrameLayout>
調(diào)用方代碼
public class AnimationTextActivity extends Activity { private AnimationTextLayout mAnimTextLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_animation_text); mAnimTextLayout = findViewById(R.id.anim_text_layout); List<String> testData = new ArrayList<>(); for (int i = 0; i < 30; i++) { testData.add("測(cè)試數(shù)據(jù)" + i); } mAnimTextLayout.setData(testData); ObjectAnimator animator = ObjectAnimator.ofFloat(mAnimTextLayout, "deviantAngle", 0f, 1f); animator.setDuration(5000); animator.setRepeatCount(-1); animator.start(); } }
注意事項(xiàng)
此處的動(dòng)畫只是為了演示??梢愿鶕?jù)需要改變自定view的改變滾動(dòng)值的方法,就可以使?jié)L輪滾動(dòng)。例如將滾動(dòng)的角度和手指拖動(dòng)相結(jié)合啥的
/** * 滾動(dòng)的偏移值 * @param deviantAngle 最大為1 */ public void setDeviantAngle(float deviantAngle)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Compose?動(dòng)畫藝術(shù)探索之可見性動(dòng)畫示例詳解
這篇文章主要為大家介紹了Compose?動(dòng)畫藝術(shù)探索之可見性動(dòng)畫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09五分了解Android?Progress?Bar進(jìn)度條加載
這篇文章主要為大家介紹了Android?Progress?Bar進(jìn)度條加載的實(shí)現(xiàn)及屬性示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02AndriodStudio使用listview實(shí)現(xiàn)簡(jiǎn)單圖書管理
這篇文章主要為大家詳細(xì)介紹了AndriodStudio使用listview實(shí)現(xiàn)簡(jiǎn)單圖書管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03android實(shí)現(xiàn)查詢公交車還有幾站的功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)查詢公交車還有幾站的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Android仿QQ、新浪相冊(cè)的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android仿QQ、新浪相冊(cè)的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Linux系統(tǒng)下安裝android sdk的方法步驟
這篇文章主要介紹了Linux系統(tǒng)下安裝android sdk的方法步驟,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友可以們下面來(lái)一起看看吧。2017-03-03Android編程之?dāng)?shù)據(jù)庫(kù)的創(chuàng)建方法詳解
這篇文章主要介紹了Android編程之?dāng)?shù)據(jù)庫(kù)的創(chuàng)建方法,結(jié)合實(shí)例形式分析了Android數(shù)據(jù)庫(kù)創(chuàng)建的步驟、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08Android中怎樣避免創(chuàng)建不必要的對(duì)象
對(duì)象的創(chuàng)建從來(lái)都不是免費(fèi)的. 一個(gè)使用線程分配池的通用垃圾回收器可以讓臨時(shí)對(duì)象的分配變得廉價(jià)一些, 但是分配內(nèi)存總是比不分配要昂貴得多.所以避免創(chuàng)建不必要的對(duì)象是很重要的一方面。2016-08-08