public class MainActivity extends Activity {
private ScrollTextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (ScrollTextView) findViewById(R.id.tv);
tv.setText("明月几时有?把酒问青天。不知天上宫阙,今夕是何年。\n"
+"我欲乘风归去,又恐琼楼玉宇,高处不胜寒。\n"
+"起舞弄清影,何似在人间。\n"
+"转朱阁,低绮户,照无眠。不应有恨,何事长向别时圆?\n"
+"人有悲欢离合,月有阴晴圆缺,此事古难全。\n"
+"但愿人长久,千里共婵娟。明月几时有?把酒问青天。不知天上宫阙,今夕是何年。\n"
+"我欲乘风归去,又恐琼楼玉宇,高处不胜寒。\n"
+"起舞弄清影,何似在人间。\n"
+"转朱阁,低绮户,照无眠。不应有恨,何事长向别时圆?\n"
+"人有悲欢离合,月有阴晴圆缺,此事古难全。\n"
+"但愿人长久,千里共婵娟。明月几时有?把酒问青天。不知天上宫阙,今夕是何年。\n"
+"我欲乘风归去,又恐琼楼玉宇,高处不胜寒。\n"
+"起舞弄清影,何似在人间。\n"
+"转朱阁,低绮户,照无眠。不应有恨,何事长向别时圆?\n"
+"人有悲欢离合,月有阴晴圆缺,此事古难全。\n"
+"但愿人长久,千里共婵娟。明月几时有?把酒问青天。不知天上宫阙,今夕是何年。\n"
+"我欲乘风归去,又恐琼楼玉宇,高处不胜寒。\n"
+"起舞弄清影,何似在人间。\n"
+"转朱阁,低绮户,照无眠。不应有恨,何事长向别时圆?\n"
+"人有悲欢离合,月有阴晴圆缺,此事古难全。\n"
+"但愿人长久,千里共婵娟。");
//
}
}
ScrollTextView类:
public class ScrollTextView extends TextView {
public ScrollTextView(Context context) {
this(context, null);
}
public ScrollTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
startScroll();
}
private void startScroll() {
initScroll();
scrollTo(0, 0);
mHandler.removeCallbacks(mRunnable);
mHandler.postDelayed(mRunnable, 7000);//首行滚动延时
}
Handler mHandler = new Handler();
Runnable mRunnable = new Runnable() {
public void run() {
int height = getHeight();
int scrollY = getScrollY();
int lineHeight = getLineHeight();
int lineCount = getLineCount();
double viewCount = Math.floor(height / lineHeight);//可见区域最大显示的行数
//能显示得下就无需滚动
if (lineCount <= viewCount) {
return;
}
//最大需要滚动的长度
int maxScrollY = lineCount * lineHeight;//等于文字总长
if (scrollY >= maxScrollY) { //滚动头时则复位
scrollTo(0, 0);
mHandler.postDelayed(this, 1000); //回滚后延时再滚动
} else {
scrollBy(0, 1);
mHandler.postDelayed(this, 50);
}
}
};
private void initScroll() {
if (mHandler == null) {
mHandler = new Handler();
}
if (mRunnable == null) {
mRunnable = new Runnable() {
public void run() {
int textAreaheight = getHeight() - getPaddingBottom() - getPaddingTop();
int scrollY = getScrollY();
int lineHeight = getLineHeight();
int lineCount = getLineCount();
double viewCount = Math.floor(textAreaheight / lineHeight);//可见区域最大显示的行数
// Log.d("ScrollTextView", "in run, viewCount=" + viewCount + " lineCount=" + lineCount + " height=" + textAreaheight + " lineHeight=" + lineHeight + " scrollY=" + scrollY);
//能显示得下就无需滚动
if (lineCount <= viewCount) {
return;
}
//最大需要滚动的长度
int maxScrollY = lineCount * lineHeight;//等于文字总长
if (scrollY >= maxScrollY) { //滚动头时则复位
scrollTo(0, 0);
mHandler.postDelayed(this, 3000);//回滚后延时再滚动
} else {
scrollBy(0, 1);
mHandler.postDelayed(this, 50);
}
}
};
}
}
}
布局文件 activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.demo.ScrollTextView
android:id="@+id/tv"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@drawable/left_btn"
/>
</LinearLayout>
在drawable文件夹下建立XML:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00000000"/>
<stroke android:width="2dip" android:color="#ff000000" />
<corners android:radius="15px" />
</shape>
<!-- solid设置填充颜色,颜色值以#80开头表示透明
stroke 设置边框宽度,颜色值
corners设置圆角 -->
效果图: