文字垂直滚动

 

文字垂直滚动

 

[功能]

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

 

 

[原理]

1. 设置 ScrollView的控件高度 为定值

2. 如何滚动显示:ScrollView.smoothScrollBy()

3. 如何循环滚动显示 即 当滚到最下面后 会回到最上面继续滚动: 得到最下面的垂直位移 然后通过 ScrollView.scrollTo() 来返回最上面

4. 如何判断是否到达底部:通过 ScrollView.getScrollY() 得到本次的垂直位移 然后与上次该值做比较 如果相等 则已经到达底部 否则 继续往下滚动

 

 

[代码 步骤]

1. 现以陆游的诗歌为例 定义布局文件 main.xml 如下:

Xml代码 复制代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView     
  8.     android:layout_width="fill_parent"    
  9.     android:layout_height="wrap_content"    
  10.     android:text="Text head! - 钗头凤 之 陆游 唐婉"  
  11.     />  
  12. <ScrollView    
  13.     android:id="@+id/sv"  
  14.     android:layout_width="fill_parent"    
  15.     android:layout_height="50dip" >  
  16. <LinearLayout  
  17.     android:id="@+id/layout"  
  18.     android:orientation="vertical"  
  19.     android:layout_width="wrap_content"    
  20.     android:layout_height="wrap_content" >  
  21. <TextView  
  22.     android:layout_width="fill_parent"  
  23.     android:layout_height="wrap_content"  
  24.     android:text="钗头凤 陆游"/>  
  25. <TextView  
  26.     android:layout_width="fill_parent"  
  27.     android:layout_height="wrap_content"  
  28.     android:text="红酥手 黄藤酒 满城春色宫墙柳"/>  
  29. <TextView  
  30.     android:layout_width="fill_parent"  
  31.     android:layout_height="wrap_content"  
  32.     android:text="东风恶 欢情薄 一杯愁绪,几年离索"/>  
  33. <TextView  
  34.     android:layout_width="fill_parent"  
  35.     android:layout_height="wrap_content"  
  36.     android:text="错!错!错!"/>  
  37. <TextView  
  38.     android:layout_width="fill_parent"  
  39.     android:layout_height="wrap_content"  
  40.     android:text="春如旧 人空瘦 泪痕红悒鲛绡透"/>  
  41. <TextView  
  42.     android:layout_width="fill_parent"  
  43.     android:layout_height="wrap_content"  
  44.     android:text="桃花落 闲池阁 山盟虽在 锦书难托"/>  
  45. <TextView  
  46.     android:layout_width="fill_parent"  
  47.     android:layout_height="wrap_content"  
  48.     android:text="莫! 莫! 莫!"/>  
  49. <TextView  
  50.     android:layout_width="fill_parent"  
  51.     android:layout_height="wrap_content"  
  52.     android:text="---------"/>  
  53. <TextView  
  54.     android:layout_width="fill_parent"  
  55.     android:layout_height="wrap_content"  
  56.     android:text="钗头凤 唐婉"/>  
  57. <TextView  
  58.     android:layout_width="fill_parent"  
  59.     android:layout_height="wrap_content"  
  60.     android:text="世情薄 人情恶 雨送黄昏花易落"/>  
  61. <TextView  
  62.     android:layout_width="fill_parent"  
  63.     android:layout_height="wrap_content"  
  64.     android:text="晓风干 泪痕残 欲笺心事 独语斜阑"/>  
  65. <TextView  
  66.     android:layout_width="fill_parent"  
  67.     android:layout_height="wrap_content"  
  68.     android:text="难!难!难!"/>  
  69. <TextView  
  70.     android:layout_width="fill_parent"  
  71.     android:layout_height="wrap_content"  
  72.     android:text="人成各 今非昨 病魂常似秋千索"/>  
  73. <TextView  
  74.     android:layout_width="fill_parent"  
  75.     android:layout_height="wrap_content"  
  76.     android:text="角声寒 夜阑珊 怕人寻问 咽泪装欢"/>  
  77. <TextView  
  78.     android:layout_width="fill_parent"  
  79.     android:layout_height="wrap_content"  
  80.     android:text="瞒! 瞒! 瞒!"/>  
  81. </LinearLayout>  
  82. </ScrollView>  
  83. <TextView     
  84.     android:layout_width="fill_parent"    
  85.     android:layout_height="wrap_content"    
  86.     android:text="Text tail!"  
  87.     />  
  88. </LinearLayout>  

 

 

2. 得到ScrollView 变量 scroll

 

Java代码 复制代码   收藏代码
  1. scroll = (ScrollView)findViewById(R.id.sv);  
scroll = (ScrollView)findViewById(R.id.sv);

 

 

3. 开辟Thread TimerLoop 用来定时 并通知 Activity 的 ScrollView 滚动一定的位移

Java代码 复制代码   收藏代码
  1. private Handler message = new Handler(){   
  2.         public void handleMessage(Message msg) {   
  3.             doScrow();   
  4.                    
  5.             }   
  6.     };   
  7.            
  8.     public class TimerLoop implements Runnable {   
  9.         @Override  
  10.         public void run() {   
  11.             // TODO Auto-generated method stub   
  12.                
  13.             while(true){   
  14.                 loop(500);   
  15.                    
  16.                 message.sendEmptyMessage(0);   
  17.             }   
  18.         }   
  19.            
  20.     }   
  21.        
  22. //因为sleep()似乎不好用 所以采用这种方法计时   
  23.     public void loop(long i){   
  24.         long j = i;   
  25.         while(j>0){   
  26.                
  27.             j = j-1;   
  28.         }   
  29.   
  30.     }  
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;
		}

    }

 

启动之

Java代码 复制代码   收藏代码
  1. public boolean onKeyDown(int keyCode, KeyEvent msg){   
  2.         Thread loop = new Thread(new TimerLoop());   
  3.         loop.start();   
  4.            
  5.         return super.onKeyDown(keyCode, msg);   
  6.     }  
public boolean onKeyDown(int keyCode, KeyEvent msg){
    	Thread loop = new Thread(new TimerLoop());
    	loop.start();
    	
    	return super.onKeyDown(keyCode, msg);
	}

  

4. 根据值比较结果 来决定是 滚动 还是 返回最上面

Java代码 复制代码   收藏代码
  1. public void doScrow(){   
  2.         int now = scroll.getScrollY();   
  3.            
  4.         if(ori == now){   
  5.             scroll.scrollTo(now, 0);   
  6.             ori = -1;   
  7.                
  8.         }   
  9.         else {   
  10.             scroll.smoothScrollBy(1010);   
  11.                
  12.             ori = now;   
  13.                
  14.         }   
  15.     }  
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!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值