文字垂直滚动

[功能]

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

[原理]

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.

打完收工!

Android实现文字垂直滚动相关推荐

  1. jq文字垂直滚动/滚屏效果

    下载地址:https://github.com/ybx13579/text-verticality-scroll 直接复制文本即可查看,或点击 ↑ 地址下载查看 效果预览 <!DOCTYPE h ...

  2. android 自定义textview 垂直滚动,Android中TextView如何实现水平和垂直滚动

    一.只想让TextView显示一行,但是文字超过TextView的长度怎么办? 在开头显示省略号 android:singleLine="true" android:ellipsi ...

  3. Android——仿京东垂直滚动广告栏

    效果图: 实现仿京东垂直滚动广告栏,它是一个动态的无限自动轮播图,让我们看看它是怎么实现的: 先看一看布局文件: activity_main.xml <?xml version="1. ...

  4. javascript 水平文字垂直滚动

    <html> <body> <div id="templayer" style="position:absolute;z-index:1;v ...

  5. android TextView 的垂直滚动

    最近突然对原来做的一个项目有想法,当时是一个显示文本的界面会循环滚动,因为时间比较仓促,就以实现需求为目的写了一个滚动的TextView,结果还是效果挺好的.现在想把它分享给大家,这次写demo是从零 ...

  6. Android 中ScrollView垂直滚动视图之隐藏滚动条的三种方法

    博主前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住也分享一下给大家,

  7. android上垂直跑马灯,android textview 垂直滚动and水平跑马灯

    垂直滚动的话就挺简单的.当然了不是自动的垂直滚动.如果是自动的垂直滚动的话还需要自定义.所以如果有需求需要textview 可以垂直滚动的话,那你来这里就对了. 直接贴 代码.就是这么任性. andr ...

  8. ViewFlipper实现文字轮播(仿淘宝头条垂直滚动广告)

    ViewFlipper实现文字轮播(仿淘宝头条垂直滚动广告) 广告条目可以单独写成布局文件,然后在布局文件或者代码中添加到总布局中 从源码可以看出,其实ViewFlipper间接的继承了FrameLa ...

  9. android文字自动滚动,Android TextView文字横向自动滚动(跑马灯)

    TextView实现文字滚动需要以下几个要点: 1.文字长度长于可显示范围:android:singleLine="true" 2.设置可滚到,或显示样式:android:elli ...

最新文章

  1. 欧洲、加拿大、澳大利亚新增2.1万个点可购买比特币现金
  2. layer.alert 延迟自动关闭_自动门中的检测与延时预防事故,分享控制案例
  3. 计算机网络 | 网络层 :IP协议详解
  4. python中的列表,添加元素,获取元素,删除元素,列表分片,常用操作符
  5. 修复bug的12个关键步骤
  6. 解决ubuntu下pdf中文不显示或乱码问题
  7. doxygen 教程 linux,doxygen linux
  8. Atitit 运营之道 互联网产品运营之道 attilax、著 1. 概念 2 1.1. 核心点 内容 媒体 用户 活动 数据分析 2 2. 第二章内容运营   2 2.1. 2.1 创建用户模
  9. linux内核程序运行在哪里,linux内核 – 设备驱动程序代码在哪里执行?内核空间还是用户空间?...
  10. WPF系列教程(十六):文本控件——TextBox、RichTextBox、PasswordBox
  11. C#控件中使用DirectX
  12. RabbitMQ(二)
  13. idea快速创建serilizableuid
  14. 2022年甘肃省安全员B证判断题及在线模拟考试
  15. Android中TextView加横线的属性
  16. torch 正确的测试模型推理时间 torch.cuda.synchronize()
  17. 二级c语言常用,二级C语言常用库函数.doc
  18. mysql多个嵌套查询
  19. ADC0832的AD模数转换原理及编程
  20. 创建TimesTen 数据库

热门文章

  1. 前端复健(微信小程序)
  2. 工业机器人安装调试与维护课程试卷_工业机器人安装、调试与维护教学课件作者阙正湘项目五工业机器人机械结构件的维修...
  3. 简述覆盖表(Covering Arrays)
  4. Multisim仿真—整流滤波电路(二)
  5. 关于C++调用::CoCreateInstance创建对象提示没有注册类的问题的解决
  6. 路径遍历(目录遍历)
  7. python3.0如何画表格_怎么用python画表格?
  8. 验证(Verification)与确认(Validation)的区别
  9. 食品行业渠道商商城系统渠道利益透明,加速企业资金回笼
  10. 艾美捷A型CpG DNA, 人/小鼠应用和化学性质说明