1. 程式人生 > >Android 實現TextView垂直向上滾動(仿向上滾動文字的廣告)

Android 實現TextView垂直向上滾動(仿向上滾動文字的廣告)

配置方法:

project中build.gradle檔案內:

allprojects {
          repositories {
             ...
             maven { url "https://jitpack.io" }
          }
       }

module中build.gradle新增依賴:

dependencies {
             compile 'com.github.paradoxie:AutoVerticalTextview:0.1' 
    }

使用方法:

1、自定義view:

package cn.wdf.textview_roll;
import android.content.Context; import android.graphics.Color; import android.os.Handler; import android.os.Message; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.view.animation.Animation;
import android.view.animation.TranslateAnimation; import android.widget.TextSwitcher; import android.widget.TextView; import android.widget.ViewSwitcher;import java.util.ArrayList; public class VerticalTextview extends TextSwitcher implements ViewSwitcher.ViewFactory { private static final int
FLAG_START_AUTO_SCROLL = 0; private static final int FLAG_STOP_AUTO_SCROLL = 1; private float mTextSize = 16; private int mPadding = 5; private int textColor = Color.GREEN; /** * @param textSize 字號 * @param padding 內邊距 * @param textColor 字型顏色 */ public void setText(float textSize, int padding, int textColor) { mTextSize = textSize; mPadding = padding; this.textColor = textColor; } private OnItemClickListener itemClickListener; private Context mContext; private int currentId = -1; private ArrayList<String> textList; private Handler handler; public VerticalTextview(Context context) { this(context, null); mContext = context; } public VerticalTextview(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; textList = new ArrayList<String>(); } public void setAnimTime(long animDuration) { setFactory(this); Animation in = new TranslateAnimation(0, 0, animDuration, 0); in.setDuration(animDuration); in.setInterpolator(new AccelerateInterpolator()); Animation out = new TranslateAnimation(0, 0, 0, -animDuration); out.setDuration(animDuration); out.setInterpolator(new AccelerateInterpolator()); setInAnimation(in); setOutAnimation(out); } /** * 間隔時間 * * @param time */ public void setTextStillTime(final long time) { handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case FLAG_START_AUTO_SCROLL: if (textList.size() > 0) { currentId++; setText(textList.get(currentId % textList.size())); } handler.sendEmptyMessageDelayed(FLAG_START_AUTO_SCROLL, time); break; case FLAG_STOP_AUTO_SCROLL: handler.removeMessages(FLAG_START_AUTO_SCROLL); break; } } }; } /** * 設定資料來源 * * @param titles */ public void setTextList(ArrayList<String> titles) { textList.clear(); textList.addAll(titles); currentId = -1; } /** * 開始滾動 */ public void startAutoScroll() { handler.sendEmptyMessage(FLAG_START_AUTO_SCROLL); } /** * 停止滾動 */ public void stopAutoScroll() { handler.sendEmptyMessage(FLAG_STOP_AUTO_SCROLL); } @Override public View makeView() { TextView t = new TextView(mContext); t.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); t.setMaxLines(1); t.setPadding(mPadding, 0, 0, 0); t.setTextColor(textColor); t.setTextSize(mTextSize); t.setClickable(true); t.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (itemClickListener != null && textList.size() > 0 && currentId != -1) { itemClickListener.onItemClick(currentId % textList.size()); } } }); return t; } /** * 設定點選事件監聽 * * @param itemClickListener */ public void setOnItemClickListener(OnItemClickListener itemClickListener) { this.itemClickListener = itemClickListener; } /** * 輪播文字點選監聽器 */ public interface OnItemClickListener { /** * 點選回撥 * * @param position 當前點選ID */ void onItemClick(int position); } }

2、xml佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cn.wdf.textview_roll.MainActivity">
<cn.wdf.textview_roll.VerticalTextview
android:id="@+id/mTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffff"
/>
</RelativeLayout>

3、activity中:

在這個類中可以設定字型的大小、顏色和內邊距。設定每行TextView停留時長間隔,設定進入和退出的時間間隔。

package cn.wdf.textview_roll;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends Activity {
    private VerticalTextview TextView;
    private ArrayList<String> titleList= new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}

    private void init() {
        TextView=findViewById(R.id.mTv);
titleList.add("熬過最難熬的那段時間");
titleList.add("你擦乾眼淚終於明白");
titleList.add("有些人");
titleList.add("註定是讓你成長的");
titleList.add("正如");
titleList.add("至尊寶在離開紫霞仙子後");
titleList.add("才真正成長為孫悟空一樣");
TextView.setTextList(titleList);
TextView.setText(22, 50, Color.RED);//設定屬性
TextView.setTextStillTime(3000);//設定停留時長間隔
TextView.setAnimTime(1000);//設定進入和退出的時間間隔
TextView.setOnItemClickListener(new VerticalTextview.OnItemClickListener() {
            @Override
public void onItemClick(int position) {
                Toast.makeText(MainActivity.this,"點選了:"+titleList.get(position),Toast.LENGTH_SHORT).show();
}
        });
}

    @Override
protected void onResume() {
        super.onResume();
TextView.startAutoScroll();
}

    @Override
protected void onPause() {
        super.onPause();
TextView.stopAutoScroll();
}
}

相關推薦

Android 實現TextView垂直向上滾動仿向上滾動文字廣告

配置方法:project中build.gradle檔案內:allprojects { repositories { ... maven { url "https://jitpack.io" }

Android實現二維碼掃描仿微信,輕量Zxing

前言 要做一個功能,二維碼識別。網上找一堆相關的Demo,但是總不是想要的效果,或者都是多年前的版本,權衡考慮之後,決定親自操刀。不糾結直接選中Zxing框架,https://github.com/zxing/zxing 在網站上直接clone下來,執行,然後就發現問題了.

Android實現textview文字滾動顯示跑馬燈效果

自定義Textview並對其進行改造,主要目的是讓textview獲取焦點,這樣文字才能滾動起來 public class MyTextView extends android.support.v7

Android筆記之圖片高斯+Glide實現微信圖片載入策略+仿微信進度條

很久以前就想自己實現一下仿微信圖片載入的那種策略了,先載入一張模糊的圖片,然後再載入清晰大圖,今天研究了一下,不過要是Glide支援進度條顯示就好了,不得不說Glide很強大, 不囉嗦了,直接上程式碼了。 首先看看高斯模糊到底怎麼實現,你問我我也不會(^__

jquery實現同時展示多個tab標籤+左右箭頭實現來回滾動美化版增加刪除按鈕

 閒聊        前段時間小穎分享了:jquery實現同時展示多個tab標籤+左右箭頭實現來回滾動文章,引入專案後,我們的組長說樣子太醜了,小穎覺得還好啊,要不大家評評理,看下醜不醜?無圖無真相,來上圖:   

Android 實現PDF檔案的檢視本地和網路檔案

PDF檔案大家應該不陌生,說的簡單點,其實我認為就是一個全圖片的Word,裡面的每個頁面都可以看做是一張帶文字的圖片,接下來我們看看在Android應用裡如何讀取和檢視PDF檔案內容。 1 在專案的gradle中增加如下程式碼: compile 'com.joanzapa

android實現的簡單的計算器原始碼+打包好的apk檔案

一、啥也不說,先看效果  二、再來看一下目錄結構吧: 三、原始碼: .java檔案 package com.kjgym.mycalculatorv10; import android.support.v7.app.AppCompatActivity; imp

Android 實現輪播圖效果 底部圓點狀態改變

自動輪播和手動輪播之後應該實現圓點的切換 自定義改變圓點狀態的監聽器 新建介面public interface DotChangeListener,新增方法void dotChangeListener(int index);並在ImageBannerFramLayout實

原聲JS實現平滑到制定位置仿錨點效果

點選按鈕,頁面滑動到指定位置,仿a標籤錨點效果 function goTarget(target) { var timer = null; var lastPos = 0; function goMove() { var

Android 實現TextView的部分文字和網路連結及電話號碼點選監聽

前言 最近在寫專案的時候遇到了一個這樣的需求,要像qq一樣,點選評論的者的名字要跳轉評論者的使用者資訊介面,並且點選評論資訊中的web連結要跳轉到WebActivity,同時如果是其他數字的話要像qq一樣點選並顯示底部Dialog提示是播打電話還是複製號碼。 效

android 實現按照城市首字母拼音分類的應用

最近按照公司需要,寫了一個按照城市首字母排序的demo,原理就是獲取城市名稱,然後將城市名稱轉換為相應的拼音,通過對拼音的排序進而得到一個序列,實現了按照首字母分類的功能。上程式碼:獲得城市資訊,此處為假資料,大家可以自行新增自己的伺服器端資料:/* * 繫結城市資訊,此處

POI實現excell批註背景圖片仿html浮窗顯示圖片

              公司客戶需要從系統中匯出一些帶圖片的excel報表,因為圖片的大小不固定,所以如果直接放在excel中會導致嚴重變形。公司的客服說以前有個客戶發給她這樣一個excel表格,在excel中顯示小圖片,滑鼠放到小圖片後顯示大圖片,滑鼠移走後大圖片隱

Android實現截圖方式整理總結

http://www.jb51.net/article/119881.htm本文介紹了Android 實現截圖方式整理,分享給大家。希望對大家有幫助可能的需求:截自己的屏截所有的屏帶導航欄截圖不帶導航欄截圖截圖並編輯選取一部分自動擷取某個空間或者佈局擷取長圖在後臺去截圖1.

Android 實現FlowLayout流式佈局類似熱門標籤

今天跟大家分享一下FlowLayout,最近專案中有遇到熱門標籤這個樣的佈局(文章末尾可下載原始碼),如下圖: 一,建立FlowLayout並繼承ViewGroup FlowLayout 類主要實現onMeasure,onLayout和generateL

關於Android實現TextView跑馬燈效果

在xml屬性中設定 <TextView android:width="wrap_content" android:height="wrap_content" android:singleLine="true" andr

自定義時間選擇控制元件仿ios滾動效果

1.先上自定義的控制元件: /** * 滾輪選擇器 * author LH * data 2016/8/20 17:26 */ public class WheelView extends View { public static final String

android 實現TextView實現跑馬燈形式的字型

         <TextView             android:id="@+id/t1"             android:layout_width="200dp"             android:layout_height="wrap_content"           

Android實現TextView文字連結的4種方式介紹及程式碼

Android實現TextView中文字連結的方式有很多種;總結起來大概有4種:用Spannable或實現它的類,如SpannableString來格式,部分字串等等,感興趣的你可以參考下 Android 的實現TextView中文字連結的方式有很多種。 總結起來大概有4

Android實現TextView走馬燈效果同時自動更新顯示當前時間

在專案開發中,有時候我們需要顯示一個走馬燈效果,簡單的走馬燈效果當然容易實現; 在xml檔案中給TextView設定以下屬性即可; android:focusable=true; android:focuseableInTouchMode=true android:sin

Android實現TextView部分文字監聽單擊事件

由於專案需要,在Android上實現TextView部分文字監聽單擊事件效果如下: 網上找了一些資料,我自己加工一下,生成了一個工具類,使用非常方便,程式碼如下: TextViewUtil.getInstance().setPartOnClickListener(mTv