android应用中TextView垂直滚动

2010年3月份入职一来一直到2011年6月份,我都在从事WEB前端的工作,主要就是javascript,后来带了2个新同事做,然后我就提供逻辑实现上的指导,主要精力放在Java后端业务逻辑和前端数据接口。由于公司业务需要,2012年5月份开始接触android,由于项目急切,还对android一头雾水时就得知我需要给android应用提供JSON数据,于是工作又有了侧重点。后来工作中发现项目中欢迎词多的时候需要实现上下滚动,了解到在android中TextView可以轻松实现横向跑马灯效果,但是对垂直滚动没有直接的支持方法,于是百度上谷歌,谷歌上百度,最终还是没有发现一个拿来即用的demo,呵呵,于是自己研究了下,写了一个可以实现TextView垂直滚动的demo,由于项目需要,在这里我使用的是AbsoluteLayout布局,左右键切换时更改滚动内容,希望此demo能给有同样需求的童鞋们带来帮助!

---写在前面


textscroll.xml配置:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tScroll"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:maxLines="5"
        android:scrollbars="none"
        android:singleLine="false"
        android:textColor="#FF0000" >
    </TextView>

</AbsoluteLayout>


Java代码:

package sue.test;

import java.util.ArrayList;
import java.util.List;

import com.amttgroup.element.Container;
import com.amttgroup.element.RootLayout;
import com.amttgroup.element.Text;
import com.amttgroup.utils.G;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.AbsoluteLayout;
import android.widget.TextView;

public class TextScrollActivity extends Activity {
	TextView tv;
	String L = "TextScrollActivity";
	List<String> welcomeWords = new ArrayList<String>();
	int curIndex = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		welcomeWords
				.add("        您的下榻使我们倍感荣幸。我谨代表北京饭店全体员工向您表示诚挚的欢迎。始建于1900年的北京饭店是一座历史悠久的豪华饭店。拥有豪华典雅的客房;口味独特的佳肴;会议会展设施及娱乐健身设施。我们将竭诚为您提供满意而舒适的服务。希望您在北京饭店下榻愉快。\n 您的下榻使我们倍感荣幸。我谨代表北京饭店全体员工向您表示诚挚的欢迎。始建于1900年的北京饭店是一座历史悠久的豪华饭店。拥有豪华典雅的客房;口味独特的佳肴;会议会展设施及娱乐健身设施。我们将竭诚为您提供满意而舒适的服务。希望您在北京饭店下榻愉快。");
		welcomeWords
				.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!");

		setContentView(R.layout.textscroll);

		tv = (TextView) findViewById(R.id.tScroll);
		
		/**
		 * 动态设置坐标及宽和高,也可以忽略,在配置文件中设置
		 */
		AbsoluteLayout.LayoutParams lp = (AbsoluteLayout.LayoutParams) tv
				.getLayoutParams();
		lp.x = 300;
		lp.y = 300;
		lp.width = 500;
		lp.height = 170;
		
		tv.setTextSize(16);
		tv.setTextColor(Color.WHITE);
		tv.setGravity(Gravity.LEFT);

		tv.setText(welcomeWords.get(curIndex));

		h.postDelayed(r, 3000);
	}

	Handler h = new Handler(); 
	int i = 0;
	Runnable r = new Runnable() {

		@Override
		public void run() {
			int height = tv.getHeight();
			int scrollY = tv.getScrollY();
			int lineHeight = tv.getLineHeight();
			int lineCount = tv.getLineCount();//总行数
			/**
			 * textView不可见内容的高度,可以理解为偏移位移
			 */
			int maxY = (tv.getLineCount() * tv.getLineHeight()
					+ tv.getPaddingTop() + tv.getPaddingBottom())
					- tv.getHeight();
			
			Log.e("=maxY=", maxY+"");
			Log.e("=height=", height+"");
			Log.e("=lineCount=", tv.getLineCount()+"");
			
			double viewCount = Math.floor(height / lineHeight);//可见区域最大显示多少行
			if (lineCount > viewCount) {//总行数大于可见区域显示的行数时则滚动

				if (scrollY >= maxY) {
					tv.scrollBy(0, -maxY);
				} else {
					tv.scrollBy(0, lineHeight);
				}
				h.postDelayed(this, 3000);
			}

		}

	};

	public boolean onKeyDown(int keyCode, KeyEvent event) {

		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_UP:
			
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			
			break;
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			
			handle();
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			
			handle();
			break;
		case KeyEvent.KEYCODE_DPAD_CENTER:
			handle();
			break;
		case KeyEvent.KEYCODE_ENTER:
			handle();
			break;
		case KeyEvent.KEYCODE_BACK:
			finish();
			
			break;
		default:
			
		}
		return super.onKeyDown(keyCode, event);
	}

	public void handle() {

		h.removeCallbacks(r);

	

		curIndex = (curIndex + 1) % 2;

		tv.setText(welcomeWords.get(curIndex));

		h.postDelayed(r, 3000);

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		h.removeCallbacks(r);
	}

}






  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值