`
gryphone
  • 浏览: 427110 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

文字垂直滚动

阅读更多

文字垂直滚动

 

[功能]

在以前的文章曾经写过 如何水平滚动 现在说一下垂直滚动

 

 

[原理]

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.

 

 

done!

分享到:
评论
8 楼 lenomon 2012-04-07  
这里有个关于垂直滚动的方法汇总:android垂直自动滚动
7 楼 lenomon 2012-03-11  
看看这个,或许适合你要求 android自定义View-垂直滚动的TextView
6 楼 Cremdayz 2011-11-30  
[/b][b][/b][b][/b][b][/b][b][/b][b][/b][b][/i][i][/i][i][/i][i][/u][u][/u][u][u][/u]
引用
引用
引用
引用
引用

    [*]

    [*]

    [*]

    [*]
[/url][url][/url][url]
5 楼 io_in_stream 2011-10-11  
3Q
4 楼 mitechen 2010-09-17  
似乎不能自动滚动啊.只能拖动...
3 楼 gryphone 2010-07-09  
kaixuan_166 写道
首先感谢楼主。
楼主的说明有问题:
1、如何判断是否到达底部:通过 ScrollView.getScrollY()…………
View.getScrollY() Return the scrolled top position of this view。
所以楼主其实是在比较顶部位置。
2、scroll.smoothScrollBy(10, 10)
为什么要在x方向上移动10呢,没必要吧。



一一回复:

1. 我这么做的原因是: 每次下拉后 getScrollY() 返回值 都不同 而当其到达最下方时候 其值保持不变 所以 我借助于此 判断是否已经到达最下方

2. 哪里?
2 楼 kaixuan_166 2010-07-09  
首先感谢楼主。
楼主的说明有问题:
1、如何判断是否到达底部:通过 ScrollView.getScrollY()…………
View.getScrollY() Return the scrolled top position of this view。
所以楼主其实是在比较顶部位置。
2、scroll.smoothScrollBy(10, 10)
为什么要在x方向上移动10呢,没必要吧。
1 楼 gaogaf 2010-06-22  
又被这两首诗打动一次

相关推荐

Global site tag (gtag.js) - Google Analytics