ScrollView文字竖直滚动
这几天研究了下文字的竖直滚动,是用ScrollView实现的,按下“启动”按钮,开始滚动,按下停止“按钮”,停止滚动
首先是Activity,
import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ScrollView; import android.widget.TextView; public class ScrollActivity extends Activity { /** Called when the activity is first created. */ ScrollView myscroll; TextView tv; int ori = 0; Button stopButton, startButton, kanButton; boolean isFlag = true; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mylayout); myscroll = (ScrollView) findViewById(R.id.sv); myscroll.setVerticalScrollBarEnabled(false); //禁用垂直滚动 myscroll.setHorizontalScrollBarEnabled(false); //禁用水平滚动 stopButton = (Button)findViewById(R.id.stopButton); stopButton.setOnClickListener(new OnClickListener(){ public void onClick(View v) { isFlag = false; }}); startButton = (Button)findViewById(R.id.startButton); startButton.setOnClickListener(new OnClickListener(){ public void onClick(View v) { isFlag = true; startScroll(); }}); kanButton = (Button)findViewById(R.id.kanButton); kanButton.setOnClickListener(new OnClickListener(){ public void onClick(View v) { int count = Thread.activeCount(); Thread[] ts = new Thread[count]; Thread.enumerate(ts); for(Thread t : ts) { System.out.println(t.getName() + " ========= "); } }}); } Handler h = new Handler() { @Override public void handleMessage(Message msg) { doScrow(); } }; //滚动 public void doScrow(){ int now = myscroll.getScrollY(); if(ori == now){ myscroll.scrollTo(now, 0); ori = -1; } else { myscroll.smoothScrollBy(10, 2); ori = now; } } //启动滚动线程 private void startScroll() { Runnable r = new TimerLoop(); final Thread loop = new Thread(r, "MyThread"); loop.start(); } public class TimerLoop implements Runnable { public void run() { while(isFlag){ try { Thread.currentThread().sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } h.sendEmptyMessage(0); } } } }
下面是xml配置
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ScrollView android:id="@+id/sv" android:layout_width="500px" android:layout_height="50dip" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="钗头凤 陆游红酥手 黄藤酒 满城春色宫墙柳东风恶 欢情薄 一杯愁绪,几年离索错!错!错! 春如旧 人空瘦 泪痕红悒鲛绡透桃花落 闲池阁 山盟虽在 锦书难托莫! 莫! 莫!钗头凤 唐婉世情薄 人情 恶 雨送黄昏花易落晓风干 泪痕残 欲笺心事 独语斜阑难!难!难!人成各 今非昨 病魂常似秋千索 角声寒 夜阑珊 怕人寻问 咽泪装欢瞒! 瞒! 瞒!" /> </ScrollView> <Button android:id="@+id/stopButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="结束" /> <Button android:id="@+id/startButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动" /> <Button android:id="@+id/kanButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查看活动线程" /> </LinearLayout>
相关推荐
JayFighting 2020-06-04
XDgaozhan 2013-07-10
luoj 2011-08-17
AndroidGA 2015-03-22
fuzhangandroid 2012-02-03
PiYuqing 2016-12-26
zzqLivecn 2016-08-15
RickyLee 2015-07-26
superxlcr 2015-03-22
ZhongGuanGuan 2014-10-31
yarkey0 2014-10-22
翟浩浩Android 2014-02-24
OuNuo0 2013-08-05
PrisonJoker 2013-07-10
zmmzmm 2013-01-25
fanjunjian 2012-10-18
housezhu 2012-10-15
yuyu00 2012-05-07