android 横向滚动文字的实现
来源:互联网 发布:软件创新之路 编辑:程序博客网 时间:2024/05/21 21:38
滚动文字需要自定义控件
注:这只是一种方法,提供借鉴
1、首先是xml文件的对比
原来的TextView xml文件
1
2
3
4
5
6
7
<TextView
android:id=
"@+id/tv_menuname"
android:layout_width=
"350dp"
android:layout_height=
"105dp"
android:text=
" "
android:textColor=
"#fff"
android:textSize=
"85dp"
/>
自定义的xml
1
2
3
4
5
6
7
<com.example.FiveTextView
android:id=
"@+id/tv_menuname"
android:layout_width=
"350dp"
android:layout_height=
"105dp"
android:text=
" "
android:textColor=
"#fff"
android:textSize=
"85dp"
/>
2、FiveTextView.class文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import
android.content.Context;
import
android.graphics.Canvas;
import
android.graphics.Paint;
import
android.util.AttributeSet;
import
android.util.Log;
import
android.view.Display;
import
android.view.WindowManager;
import
android.widget.TextView;
public
class
FiveTextView
extends
TextView {
/** 文字长度 */
private
float
textLength = 0f;
/** 滚动条长度 */
private
float
viewWidth = 0f;
/** 文本x轴 的坐标 */
private
float
tx = 0f;
/** 文本Y轴的坐标 */
private
float
ty = 0f;
/** 文本当前长度 */
private
float
temp_tx1 =
0
.0f;
/** 文本当前变换的长度 */
private
float
temp_tx2 =
0x0f
;
/** 文本滚动开关 */
private
boolean
isStarting =
false
;
/** 画笔对象 */
private
Paint paint =
null
;
/** 显示的文字 */
private
String text =
""
;
/** 文本滚动速度 **/
private
float
sudu;
public
FiveTextView(Context context, AttributeSet attrs) {
super
(context, attrs);
}
/**
* 初始化自动滚动条,每次改变文字内容时,都需要重新初始化一次
*
* @param windowManager
* 获取屏幕
* @param text
* 显示的内容
* @param type
* 文字滚动的类型
*/
public
void
initScrollTextView(WindowManager windowManager, String text,
float
su) {
// 得到画笔,获取父类的textPaint
paint =
this
.getPaint();
// 得到文字
this
.text = text;
this
.sudu = su;
textLength = paint.measureText(text);
// 获得当前文本字符串长度
viewWidth =
this
.getWidth();
// 获取宽度return mRight - mLeft;
if
(viewWidth ==
0
) {
// 获取当前屏幕的属性
Display display = windowManager.getDefaultDisplay();
viewWidth = display.getWidth();
// 获取屏幕宽度 viewWidth 是滚动的开始位置,需要修改的
// 可再此入手
}
tx = textLength;
temp_tx1 = viewWidth + textLength;
temp_tx2 = viewWidth + textLength *
2
;
// 自己定义,文字变化多少
// // 文字的大小+距顶部的距离
ty =
this
.getTextSize() +
this
.getPaddingTop();
}
/**
* 开始滚动
*/
public
void
starScroll() {
// 开始滚动
isStarting =
true
;
this
.invalidate();
// 刷新屏幕
}
/**
* 停止方法,停止滚动
*/
public
void
stopScroll() {
// 停止滚动
isStarting =
false
;
this
.invalidate();
// 刷新屏幕
}
/** 重写onDraw方法 */
@Override
protected
void
onDraw(Canvas canvas) {
if
(isStarting) {
// A-Alpha透明度/R-Read红色/g-Green绿色/b-Blue蓝色
paint.setARGB(
255
,
200
,
200
,
200
);
canvas.drawText(text, temp_tx1 - tx, ty, paint);
tx += sudu;
// 当文字滚动到屏幕的最左边
if
(tx > temp_tx2) {
// 把文字设置到最右边开始
tx = textLength;
}
this
.invalidate();
// 刷新屏幕
}
super
.onDraw(canvas);
}
}
3、接下来就是调用了
首先得创建FiveTextView对象
1
private
FiveTextView tv_scoll;
然后找到对象
1
tv_scoll = (FiveTextView) findViewById(R.id.tv_scoll);
初始化滚动数据
1
2
tv_scoll.initScrollTextView(getWindowManager(), scoll_content,
1
);
//scoll_content为滚动的内容
//1为滚动的速度,越大滚动越快
最后开始滚动
滚动的内容不是tv_scoll.getText()的文字,而是上面初始化时 的scoll_content参数
1
2
tv_scoll.setText(
""
);
tv_scoll.starScroll();
0 0
- android 横向滚动文字的实现
- 文字横向滚动的实现
- 文字横向滚动的实现
- js实现文字横向滚动
- 文字横向滚动的TextView
- Android 横向列表GridView 实现横向滚动
- Android 横向列表GridView 实现横向滚动
- Android 横向列表GridView 实现横向滚动
- Android GridView实现横向滚动
- Android GridView实现横向滚动
- 横向不间断的文字滚动javascript
- 横向不间断的文字滚动效果
- Android文字自动横向滚动的TextView(文字长度不够也可以)
- 文字横向滚动效果
- 手写横向文字滚动
- 文字横向滚动效果
- Android TV横向滚动实现Launcher的RecyclerView第二弹
- 【Android】用RecycleView实现可以横向滚动的ListView效果
- uva 10361
- Android UI布局之TableLayout
- Mac和iOS开发资源汇总
- Mac OS X 软件包管理器 – Homebrew
- /system/app apk裁剪
- android 横向滚动文字的实现
- Codeforces Round #276 (Div. 2) C
- 钢板桩在国外的应用概况
- get与post提交方式的区别
- SqlServerCE数据库类(转自http://blog.csdn.net/letian_fly/article/details/2975962)
- Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能
- socket 错误码errno
- nyoj814又见拦截导弹
- mysql函数收集