文字垂直滾動
[功能]
在以前的文章曾經寫過 如何水平滾動 現在說一下垂直滾動
[原理]
1. 設定 ScrollView的控制項高度 為定值
2. 如何滾動顯示:ScrollView.smoothScrollBy()
3. 如何迴圈滾動顯示 即 當滾到最下面後 會回到最上面繼續滾動: 得到最下面的垂直位移 然後通過 ScrollView.scrollTo() 來返回最上面
4. 如何判斷是否到達底部:通過 ScrollView.getScrollY() 得到本次的垂直位移 然後與上次該值做比較 如果相等 則已經到達底部 否則 繼續往下滾動
[代碼 步驟]
1. 現以陸遊的詩歌為例 定義布局檔案 main.xml 如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Text head! - 釵頭鳳 之 陸遊 唐婉" /> <ScrollView android:id="@+id/sv" android:layout_width="fill_parent" android:layout_height="50dip" > <LinearLayout android:id="@+id/layout" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="釵頭鳳 陸遊"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="紅酥手 黃藤酒 滿城春色宮牆柳"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="東風惡 歡情薄 一杯愁緒,幾年離索"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="錯!錯!錯!"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="春如舊 人空瘦 淚痕紅悒鮫綃透"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="桃花落 閑池閣 山盟雖在 錦書難托"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="莫! 莫! 莫!"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="---------"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="釵頭鳳 唐婉"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="世情薄 人情惡 雨送黃昏花易落"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="曉風乾 淚痕殘 欲箋心事 獨語斜闌"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="難!難!難!"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="人成各 今非昨 病魂常似鞦韆索"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="角聲寒 夜闌珊 怕人尋問 咽淚裝歡"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="瞞! 瞞! 瞞!"/> </LinearLayout> </ScrollView> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Text tail!" /> </LinearLayout>
2. 得到ScrollView 變數 scroll
scroll = (ScrollView)findViewById(R.id.sv);
3. 開闢Thread TimerLoop 用來定時 並通知 Activity 的 ScrollView 滾動一定的位移
private Handler message = new Handler(){ public void handleMessage(Message msg) { doScrow(); } }; public class TimerLoop implements Runnable { @Override public void run() { // TODO Auto-generated method stub while(true){ loop(500); message.sendEmptyMessage(0); } } } //因為sleep()似乎不好用 所以採用這種方法計時 public void loop(long i){ long j = i; while(j>0){ j = j-1; } }
啟動之
public boolean onKeyDown(int keyCode, KeyEvent msg){ Thread loop = new Thread(new TimerLoop()); loop.start(); return super.onKeyDown(keyCode, msg); }
4. 根據值比較結果 來決定是 滾動 還是 返回最上面
public void doScrow(){ int now = scroll.getScrollY(); if(ori == now){ scroll.scrollTo(now, 0); ori = -1; } else { scroll.smoothScrollBy(10, 10); ori = now; } }
emulator 運行 (注意2次的字元內容的差異)
1.
2.
打完收工!