Android应用中TextView垂直滚动


在Android中TextView可以轻松实现横向跑马灯效果,但是对垂直滚动没有直接的支持方法,于是百度上谷歌,谷歌上百度,最终还是没有发现一个拿来即用的demo,呵呵,于是自己研究了下,写了一个可以实现TextView垂直滚动的demo,由于项目需要,在这里我使用的是AbsoluteLayout布局,左右键切换时更改滚动内容,希望此demo能给有同样需求的童鞋们带来帮助!

---写在前面

textscroll.xml配置:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:orientation="vertical" > 
  6.  
  7.     <TextView 
  8.         android:id="@+id/tScroll" 
  9.         android:layout_width="fill_parent" 
  10.         android:layout_height="wrap_content" 
  11.         android:maxLines="5" 
  12.         android:scrollbars="none" 
  13.         android:singleLine="false" 
  14.         android:textColor="#FF0000" > 
  15.     </TextView> 
  16.  
  17. </AbsoluteLayout> 

Java代码:

  1. package sue.test; 
  2.  
  3. import java.util.ArrayList; 
  4. import java.util.List; 
  5.  
  6. import com.amttgroup.element.Container; 
  7. import com.amttgroup.element.RootLayout; 
  8. import com.amttgroup.element.Text; 
  9. import com.amttgroup.utils.G; 
  10.  
  11. import android.app.Activity; 
  12. import android.graphics.Color; 
  13. import android.os.Bundle; 
  14. import android.os.Handler; 
  15. import android.util.Log; 
  16. import android.view.Gravity; 
  17. import android.view.KeyEvent; 
  18. import android.widget.AbsoluteLayout; 
  19. import android.widget.TextView; 
  20.  
  21. public class TextScrollActivity extends Activity { 
  22.     TextView tv; 
  23.     String L = "TextScrollActivity"
  24.     List<String> welcomeWords = new ArrayList<String>(); 
  25.     int curIndex = 0
  26.  
  27.     @Override 
  28.     protected void onCreate(Bundle savedInstanceState) { 
  29.         super.onCreate(savedInstanceState); 
  30.  
  31.         welcomeWords 
  32.                 .add("        帮客之家(LinuxIDC.com)于2006年9月25日注册并开通网站,Linux现在已经成为一种广受关注和支持的一种操作系统,IDC是互联网数据中心,LinuxIDC就是关于Linux的数据中心。帮客之家是专业的Linux系统门户网站,实时发布最新Linux资讯,包括Linux、Ubuntu、Fedora、RedHat、红旗Linux、Linux教程、Linux认证、SUSE Linux、Android、Oracle、Hadoop等技术。"); 
  33.         welcomeWords 
  34.                 .add("  It is an honor for you to stay at the Beijing Hotel. On behalf of the staff at the Beijing Hotel, I sincerely welcome you.Built in 1900, Beijing Hotel is a luxury hotel with a long history. We have elegant guestrooms, exquisite cuisine, convenient facilities and entertainment facilities. It is our pleasure to offer you the best services.Have a nice stay!"); 
  35.  
  36.         setContentView(R.layout.textscroll); 
  37.  
  38.         tv = (TextView) findViewById(R.id.tScroll); 
  39.          
  40.         /** 
  41.          * 动态设置坐标及宽和高,也可以忽略,在配置文件中设置 
  42.          */ 
  43.         AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) tv 
  44.                 .getLayoutParams(); 
  45.         lp.x = 300
  46.         lp.y = 300
  47.         lp.width = 500
  48.         lp.height = 170
  49.          
  50.         tv.setTextSize(16); 
  51.         tv.setTextColor(Color.WHITE); 
  52.         tv.setGravity(Gravity.LEFT); 
  53.  
  54.         tv.setText(welcomeWords.get(curIndex)); 
  55.  
  56.         h.postDelayed(r, 3000); 
  57.     } 
  58.  
  59.     Handler h = new Handler();   
  60.     int i = 0
  61.     Runnable r = new Runnable() { 
  62.  
  63.         @Override 
  64.         public void run() { 
  65.             int height = tv.getHeight(); 
  66.             int scrollY = tv.getScrollY(); 
  67.             int lineHeight = tv.getLineHeight(); 
  68.             int lineCount = tv.getLineCount();//总行数  
  69.             /** 
  70.              * textView不可见内容的高度,可以理解为偏移位移 
  71.              */ 
  72.             int maxY = (tv.getLineCount() * tv.getLineHeight() 
  73.                     + tv.getPaddingTop() + tv.getPaddingBottom()) 
  74.                     - tv.getHeight(); 
  75.              
  76.             Log.e("=maxY=", maxY+""); 
  77.             Log.e("=height=", height+""); 
  78.             Log.e("=lineCount=", tv.getLineCount()+""); 
  79.              
  80.             double viewCount = Math.floor(height / lineHeight);//可见区域最大显示多少行  
  81.             if (lineCount > viewCount) {//总行数大于可见区域显示的行数时则滚动  
  82.  
  83.                 if (scrollY >= maxY) { 
  84.                     tv.scrollBy(0, -maxY); 
  85.                 } else { 
  86.                     tv.scrollBy(0, lineHeight); 
  87.                 } 
  88.                 h.postDelayed(this3000); 
  89.             } 
  90.  
  91.         } 
  92.  
  93.     }; 
  94.  
  95.     public boolean onKeyDown(int keyCode, KeyEvent event) { 
  96.  
  97.         switch (keyCode) { 
  98.         case KeyEvent.KEYCODE_DPAD_UP: 
  99.              
  100.             break
  101.         case KeyEvent.KEYCODE_DPAD_DOWN: 
  102.              
  103.             break
  104.         case KeyEvent.KEYCODE_DPAD_RIGHT: 
  105.              
  106.             handle(); 
  107.             break
  108.         case KeyEvent.KEYCODE_DPAD_LEFT: 
  109.              
  110.             handle(); 
  111.             break
  112.         case KeyEvent.KEYCODE_DPAD_CENTER: 
  113.             handle(); 
  114.             break
  115.         case KeyEvent.KEYCODE_ENTER: 
  116.             handle(); 
  117.             break
  118.         case KeyEvent.KEYCODE_BACK: 
  119.             finish(); 
  120.              
  121.             break
  122.         default
  123.              
  124.         } 
  125.         return super.onKeyDown(keyCode, event); 
  126.     } 
  127.  
  128.     public void handle() { 
  129.  
  130.         h.removeCallbacks(r); 
  131.  
  132.      
  133.  
  134.         curIndex = (curIndex + 1) % 2
  135.  
  136.         tv.setText(welcomeWords.get(curIndex)); 
  137.  
  138.         h.postDelayed(r, 3000); 
  139.  
  140.     } 
  141.  
  142.     @Override 
  143.     public void onDestroy() { 
  144.         super.onDestroy(); 
  145.         h.removeCallbacks(r); 
  146.     } 
  147.  

相关内容