(筆記)Android 多行垂直滾動

前言:最近在Android開發(fā)學(xué)習(xí)中需要用到多行垂直滾動,做好后現(xiàn)將它整理出來嘀倒。

概述:我這主要是實現(xiàn)兩行滾動采盒,如果字符超過兩行則滾動唧龄,如果少于或等于兩行則不滾動兼砖,點擊按鈕停止?jié)L動。

效果圖:


image

步驟:

1既棺、新建自定義VerticalMarqueeView java文件讽挟。

public class VerticalMarqueeView extends ViewFlipper {
    /**
     * Alpha animation enable
     */
    private boolean isSetAlphaAnim = true;
    /**
     * Animation interval
     */
    private int interval = 5000;
    /**
     * Animation duration
     */
    private int animDuration = 2000;

    public VerticalMarqueeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MarqueeViewUp, defStyleAttr, 0);
        isSetAlphaAnim = ta.getBoolean(R.styleable.MarqueeViewUp_isSetAlphaAnim, isSetAlphaAnim);
        interval = ta.getInteger(R.styleable.MarqueeViewUp_interval, interval);
        animDuration = ta.getInteger(R.styleable.MarqueeViewUp_animDuration, animDuration);
        setFlipInterval(interval);
        // In or out animation: alpha, continuous
        AlphaAnimation animationIn = new AlphaAnimation(0, 1);
        animationIn.setDuration(animDuration);
        AlphaAnimation animationOut = new AlphaAnimation(1, 0);
        animationOut.setDuration(animDuration);
        // TranslateAnimation: show
        TranslateAnimation translateAnimationIn = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.0f);
        // TranslateAnimation: dismiss
        TranslateAnimation translateAnimationOut = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, -1.0f);
        translateAnimationIn.setDuration(animDuration);
        translateAnimationOut.setDuration(animDuration);
        // In
        AnimationSet animationInSet = new AnimationSet(false);
        animationInSet.addAnimation(translateAnimationIn);
        // Out
        AnimationSet animationOutSet = new AnimationSet(false);
        animationOutSet.addAnimation(translateAnimationOut);
        // alpha animation enable
        if (isSetAlphaAnim) {
            animationInSet.addAnimation(animationIn);
            animationOutSet.addAnimation(animationOut);
        }
        setInAnimation(animationInSet);
        setOutAnimation(animationOutSet);
    }


    /**
     * Set flipping views array
     */
    public void setViews(final List<View> views) {
        if (views == null || views.size() == 0) {
            return;
        }
        removeAllViews();
        for (int i = 0; i < views.size(); i++) {
            final int position = i;
            // Single tap listener
            views.get(i).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(position, views.get(position));
                    }
                }
            });
            addView(views.get(i));
        }
        startFlipping();
    }

    /**
     * Single tap click listener
     */
    private OnItemClickListener onItemClickListener;

    /**
     * Set single tap callback
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * Single tap callback interface
     */
    public interface OnItemClickListener {
        /**
         * On item click
         *
         * @param position On click position
         * @param view     On click view
         */
        void onItemClick(int position, View view);
    }

2、在res下的values文件夾新建VerticalMarqueeView中所需要的attrs.xml資源文件丸冕,在里面定義自定義屬性耽梅。

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    // Vertical marquee
    <declare-styleable name="MarqueeViewUp">
        <attr name="isSetAlphaAnim" format="boolean" />
        <attr name="interval" format="integer" />
        <attr name="animDuration" format="integer" />
    </declare-styleable>
</resources>

3、在Actitivity中的運用胖烛。
初始化界面程序

    data = new ArrayList<>();
        //final  String s="21wqsqwsqw";
        String s = "你聽說過一見鐘情嗎眼姐? 那個十二歲的夏天,校門口邊上 他倚在單車邊上佩番,微微回頭尋找我,我撞上他眼睛里,他撞進我心底 沒有任何對話,他送了我一路,那段路好長好長,我走了好久好久众旗。";
        s = s.replaceAll("\t|\n", "");
        voice_tv_words.setText(s);
        List<String> list = getStrList(s, 21);
        for (int i = 0; i < list.size(); i++) {
            data.add(list.get(i));
        }
       //判斷如果超過兩行則執(zhí)行滾動,否則不滾動
        if (Strint_size > 2) {
            voice_tv_words.setVisibility(View.GONE);
            marqueeView1.setVisibility(View.VISIBLE);
            if (s.isEmpty()) {
                return;
            }
            setViewTwoLines();
            marqueeView1.setViews(views1);
        } else {
            voice_tv_words.setText(s);
            voice_tv_words.setVisibility(View.VISIBLE);
            marqueeView1.setVisibility(View.GONE);
        }
          //點擊按鈕停止天滾動
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                marqueeView1.stopFlipping();
                marqueeView1.removeAllViews();
                Toast.makeText(ScrollTextActivity.this, "停止?jié)L動", Toast.LENGTH_SHORT).show();
            }
        });

設(shè)置顯示兩行

private void setViewTwoLines() {
        views1.clear();//去除重影現(xiàn)象
        for (int i = 0; i < data.size(); i = i + 2) {
            final int position = i;
            //設(shè)置滾動的單個布局
            LinearLayout moreView = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.item_view, null);
            //初始化布局的控件
            TextView tv1 = (TextView) moreView.findViewById(R.id.tv1);
            TextView tv2 = (TextView) moreView.findViewById(R.id.tv2);

            //進行對控件賦值
            tv1.setText(data.get(i).toString());
            if (data.size() > i + 1) {
                //奇數(shù)條
                tv2.setText(data.get(i + 1).toString());
            } else {//偶數(shù)條
                //moreView.findViewById(R.id.rl2).setVisibility(View.GONE);
            }
            //添加到循環(huán)滾動數(shù)組里面去
            views1.add(moreView);
            moreView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    marqueeView1.setViews(views1);
                    Toast.makeText(ScrollTextActivity.this, "停止?jié)L動", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

將字符串分成指定長度

public static List<String> getStrList(String inputString, int length) {
        int size = inputString.length() / length;
        if (inputString.length() % length != 0) {
            size += 1;
        }
        return getStrList(inputString, length, size);
    }

    public static List<String> getStrList(String inputString, int length,
                                          int size) {
        List<String> list = new ArrayList<String>();
        for (int index = 0; index < size; index++) {
            String childStr = substring(inputString, index * length,
                    (index + 1) * length);
            list.add(childStr);
        }
        Strint_size = size;
        System.out.println("wyq129:size " + size);
        return list;
    }

    public static String substring(String str, int f, int t) {
        if (f > str.length()) {
            return null;
        }
        if (t > str.length()) {
            return str.substring(f, str.length());
        } else {
            return str.substring(f, t);
        }
    }

item_view.xml布局文件代碼趟畏,顯示兩個textview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:ellipsize="end"
        android:maxLines="1"
        android:textColor="#ffffffff"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:ellipsize="end"
        android:maxLines="1"
        android:textColor="#ffffffff"
        android:textSize="16sp" />
</LinearLayout>

VerticalMarqueeView在布局文件中的引用

 <com.example.aiiage.scrolltextview.VerticalMarqueeView
        android:id="@+id/upview1"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        app:animDuration="1000"
        app:interval="2000"
        app:isSetAlphaAnim="true"
        android:visibility="gone"/>

結(jié)論

以上就全部實現(xiàn)完了贡歧。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子利朵,更是在濱河造成了極大的恐慌律想,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绍弟,死亡現(xiàn)場離奇詭異技即,居然都是意外死亡,警方通過查閱死者的電腦和手機樟遣,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進店門姥份,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人年碘,你說我怎么就攤上這事≌辜Γ” “怎么了屿衅?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長莹弊。 經(jīng)常有香客問我涤久,道長,這世上最難降的妖魔是什么忍弛? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任响迂,我火速辦了婚禮,結(jié)果婚禮上细疚,老公的妹妹穿的比我還像新娘蔗彤。我一直安慰自己,他們只是感情好疯兼,可當(dāng)我...
    茶點故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布然遏。 她就那樣靜靜地躺著,像睡著了一般吧彪。 火紅的嫁衣襯著肌膚如雪待侵。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天姨裸,我揣著相機與錄音秧倾,去河邊找鬼。 笑死傀缩,一個胖子當(dāng)著我的面吹牛那先,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播赡艰,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼胃榕,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起勋又,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤苦掘,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后楔壤,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體鹤啡,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年蹲嚣,在試婚紗的時候發(fā)現(xiàn)自己被綠了递瑰。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡隙畜,死狀恐怖抖部,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情议惰,我是刑警寧澤慎颗,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站言询,受9級特大地震影響俯萎,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜运杭,卻給世界環(huán)境...
    茶點故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一夫啊、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧辆憔,春花似錦撇眯、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至彤钟,卻和暖如春来候,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背逸雹。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工营搅, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人梆砸。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓转质,卻偏偏與公主長得像,于是被迫代替她去往敵國和親帖世。 傳聞我的和親對象是個殘疾皇子休蟹,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明先生_X自主閱讀 15,982評論 3 119
  • 很多事情绑榴,只有真正的經(jīng)歷了,才會知道這其中的付出與艱辛盈魁。 最近幾天確實非常的忙碌翔怎,為了開好公司的省區(qū)會議,每天都是...
    惠影風(fēng)華閱讀 728評論 0 1
  • 今天我看了《一百個孩子一百個夢》這本書的眼淚流光了怎么辦?她是小學(xué)二年級的女生杨耙,她夢到了黃莉莉看一本小人書連環(huán)畫赤套,...
    繆嘉豪閱讀 287評論 0 0
  • 果然學(xué)知識還要找“源頭”的第一手資料! “定位”重要性毋庸置疑珊膜,但說定位不慎會導(dǎo)致大量失業(yè)容握,甚至“付出巨大代價與犧...
    SandyZhao閱讀 201評論 0 0