ScrollTextView - scrolling TextView for Android

找了很就终于在国外一网站上找到一个Scroller的例子,为了日后方便查阅,在博客上记录以下。

Here are the source codes.

The key points are:

  • setHorizontallyScrolling() and use android.widget.Scroller to make the text scroll;
  • Use the attached TextPaint of the TextView to measure the length of the text in pixel;
  • setSingleLine() and setEllipsize(null) to make the text not wrapped and ellipsized;
  • Override computeScroll() to restart the scrolling when finished

Here are the source codes.

 

ExpandedBlockStart.gif 代码
/** ********************************************************************** */
package  com.dirtybear.android;

import  android.content.Context;
import  android.graphics.Rect;
import  android.text.TextPaint;
import  android.util.AttributeSet;
import  android.view.animation.LinearInterpolator;
import  android.widget.Scroller;
import  android.widget.TextView;

public   class  ScrollTextView  extends  TextView {

//  scrolling feature
private  Scroller mSlr;

//  milliseconds for a round of scrolling
private   int  mRndDuration  =   250 ;

//  the X offset when paused
private   int  mXPaused  =   0 ;

//  whether it's being paused
private   boolean  mPaused  =   true ;

/*
* constructor
*/
public  ScrollTextView(Context context) {
this (context,  null );
}

/*
* constructor
*/
public  ScrollTextView(Context context, AttributeSet attrs) {
this (context, attrs, android.R.attr.textViewStyle);
}

/*
* constructor
*/
public  ScrollTextView(Context context, AttributeSet attrs,  int  defStyle) {
super (context, attrs, defStyle);
//  customize the TextView
setSingleLine();
setEllipsize(
null );
setVisibility(INVISIBLE);
}

/**
* begin to scroll the text from the original position
*/
public   void  startScroll() {
//  begin from the very right side
mXPaused  =   - 1   *  getWidth();
//  assume it's paused
mPaused  =   true ;
resumeScroll();
}

/**
* resume the scroll from the pausing point
*/
public   void  resumeScroll() {

if  ( ! mPaused)
return ;

//  Do not know why it would not scroll sometimes
//  if setHorizontallyScrolling is called in constructor.
setHorizontallyScrolling( true );

//  use LinearInterpolator for steady scrolling
mSlr  =   new  Scroller( this .getContext(),  new  LinearInterpolator());
setScroller(mSlr);

int  scrollingLen  =  calculateScrollingLen();
int  distance  =  scrollingLen  -  (getWidth()  +  mXPaused);
int  duration  =  ( new  Double(mRndDuration  *  distance  *   1.00000
/  scrollingLen)).intValue();

setVisibility(VISIBLE);
mSlr.startScroll(mXPaused, 
0 , distance,  0 , duration);
mPaused 
=   false ;
}

/**
* calculate the scrolling length of the text in pixel
*
@return  the scrolling length in pixels
*/
private   int  calculateScrollingLen() {
TextPaint tp 
=  getPaint();
Rect rect 
=   new  Rect();
String strTxt 
=  getText().toString();
tp.getTextBounds(strTxt, 
0 , strTxt.length(), rect);
int  scrollingLen  =  rect.width()  +  getWidth();
rect 
=   null ;
return  scrollingLen;
}

/**
* pause scrolling the text
*/
public   void  pauseScroll() {
if  ( null   ==  mSlr)
return ;

if  (mPaused)
return ;

mPaused 
=   true ;

//  abortAnimation sets the current X to be the final X,
//  and sets isFinished to be true
//  so current position shall be saved
mXPaused  =  mSlr.getCurrX();

mSlr.abortAnimation();
}

@Override
/*
* override the computeScroll to restart scrolling when finished so as that
* the text is scrolled forever
*/
public   void  computeScroll() {
super .computeScroll();

if  ( null   ==  mSlr)  return ;

if  (mSlr.isFinished()  &&  ( ! mPaused)) {
this .startScroll();
}
}

public   int  getRndDuration() {
return  mRndDuration;
}

public   void  setRndDuration( int  duration) {
this .mRndDuration  =  duration;
}

public   boolean  isPaused() {
return  mPaused;
}
}
/** ********************************************************************** */


 
 

 

Issues to be fixed:

  1. Do not know why it would not scroll sometimes if setHorizontallyScrolling is called in constructor;
  2. The scrolling is not smooth enough. Maybe it's because of the single thread model of Android UI;

Tested on:


Windows XP


Android SDK 1.0 rc2


Android Emulator 1.0


Android Debug Bridge version 1.0.20

转载于:https://www.cnblogs.com/nstop/archive/2010/04/13/1710848.html

### 回答1: -webkit-overflow-scrolling: touch; 是一个CSS属性,用于在移动设备上实现平滑的滚动效果。它可以应用于具有滚动内容的元素,例如 div 或 iframe。当用户在移动设备上滚动该元素时,它将以流畅的方式滚动,而不是像默认滚动一样卡顿。 ### 回答2: -webkit-overflow-scrolling:touch;是一个CSS属性,用于移动设备的浏览器中控制页面滚动的行为。当一个页面在移动设备上被访问时,其内容有可能会超过设备屏幕的大小,此时就需要滚动来查看页面内容。但是,在移动设备上的滚动效果相较于在桌面电脑上的体验要差很多,因为它们通常会有卡顿、慢反应或跳跃等不良效果。这样的效果会影响用户的体验,因此必须要采用一些方法来改善这种情况。 -webkit-overflow-scrolling:touch;可以帮助我们实现流畅的滚动效果,消除卡顿、慢反应和跳跃等问题。这个属性是仅适用于 Webkit 内核的,因此在移动设备上使用较为广泛。它会在页面滚动时使用硬件加速来优化滚动性能,从而实现更加平滑、自然的滚动。 这个属性使用起来非常简单,只需要将它添加到滚动区域的 CSS 样式中即可。例如: #scrollable { height: 150px; -webkit-overflow-scrolling: touch; overflow-y: auto; } 在这个例子中,页面中的一个元素被定义为滚动区域。当使用触摸设备滚动时,-webkit-overflow-scrolling:touch;会生效,从而确保滚动平滑自然。 总之,-webkit-overflow-scrolling:touch;是一个非常有用的属性,它可以提高移动设备上页面滚动的性能和用户体验。无论是开发移动网站还是移动应用,我们都应该考虑使用这个属性来改善用户体验。 ### 回答3: -webkit-overflow-scrolling: touch;是一个CSS属性,用于在屏幕上启用流畅的滚动功能。它主要应用于移动设备上的网页浏览器。通过启用此属性,用户可以在触摸屏上轻松地使用手指来滚动页面,而不会出现卡顿或延迟的情况。 该属性只能在Webkit浏览器中使用,以确保兼容性。它适用于大多数常见的移动设备和平板电脑,包括iPhone, iPad, Android手机和平板电脑等。 此属性的主要目的是优化网页在移动设备上的用户体验。由于移动设备的屏幕尺寸通常较小,因此需要在较小的屏幕上显示更多的内容。通过使用此属性,用户可以轻松地滚动网页并捕捉所需要的内容,而无需缩放视图或调整字号。 此外,-webkit-overflow-scrolling: touch;还可用于增强应用程序的用户体验。例如,在移动应用程序中,可以使用此属性使用户可以轻松地滚动列表或其他元素。 总之,-webkit-overflow-scrolling: touch;是一种对网页和应用程序的特殊效果,可以在移动设备上改善用户体验。它通过启用流畅的滚动功能,让用户可以轻松地浏览内容,缩小了移动设备和桌面网页之间的差距。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值