代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
textview(跑马灯效果)文字长短不限循环播放
作者:
JoanneTou
/ 发布于
2012/8/13
/
548
textview显示跑马灯效果,使用的是继承的方法onDraw不停地绘制 优点: 1.文字长短不限哦 2.不用非得获取焦点哦
<div><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" ></div> <div> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" ></div> <div> <feng.f7_27.activity.CustomTextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="#aaffee"/> </LinearLayout> </LinearLayout></div> <div></div> <div></div> <div>CustomTextView.java</div> <div>package feng.f7_27.activity;</div> import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.os.Parcel; import android.os.Parcelable; import android.util.AttributeSet; import android.view.Display; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; import android.widget.TextView;</div> <div>public class CustomTextView extends TextView implements OnClickListener{ public final static String TAG = CustomTextView.class.getSimpleName();</div> <div>private float textLength = 0f;//??????? private float viewWidth = 0f; private float step = 0f;//????????? private float y = 0f;//?????????? private float temp_view_plus_text_length = 0.0f;//??????????????? private float temp_view_plus_two_text_length = 0.0f;//??????????????? public boolean isStarting = false;//???????? private Paint paint = null;//?????? private String text = "";//???????</div> <div> public CustomTextView(Context context) { super(context); initView(); } <div>public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } <div>public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); } <div>/** *//** * ???????? */ private void initView() { setOnClickListener(this); } <div>/** *//** * ??????????ÿ?θ???????????????Ч????????????³??????? */ public void init(WindowManager windowManager) { paint = getPaint(); text = getText().toString(); textLength = paint.measureText(text); viewWidth = getWidth(); if(viewWidth == 0) { if(windowManager != null) { Display display = windowManager.getDefaultDisplay(); viewWidth = display.getWidth(); } } step = textLength; temp_view_plus_text_length = viewWidth + textLength; temp_view_plus_two_text_length = viewWidth + textLength * 2; y = getTextSize() + getPaddingTop()+15; } @Override public Parcelable onSaveInstanceState() { Parcelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState);</div> <div>ss.step = step; ss.isStarting = isStarting;</div> <div>return ss;</div> <div>} @Override public void onRestoreInstanceState(Parcelable state) { if (!(state instanceof SavedState)) { super.onRestoreInstanceState(state); return; } SavedState ss = (SavedState)state; super.onRestoreInstanceState(ss.getSuperState());</div> <div>step = ss.step; isStarting = ss.isStarting;</div> <div>} <div>public static class SavedState extends BaseSavedState { public boolean isStarting = false; public float step = 0.0f; SavedState(Parcelable superState) { super(superState); } <div>@Override public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); out.writeBooleanArray(new boolean[]{isStarting}); out.writeFloat(step); } <div> public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {</div> <div>public SavedState[] newArray(int size) { return new SavedState[size]; } <div>@Override public SavedState createFromParcel(Parcel in) { return new SavedState(in); } };</div> <div>private SavedState(Parcel in) { super(in); boolean[] b = null; in.readBooleanArray(b); if(b != null && b.length > 0) isStarting = b[0]; step = in.readFloat(); } } <div>/** *//** * ??????? */ public void startScroll() { isStarting = true; invalidate(); } <div>/** *//** * ?????? */ public void stopScroll() { isStarting = false; invalidate(); } <div> @Override public void onDraw(Canvas canvas) { canvas.drawText(text, temp_view_plus_text_length - step, y, paint); if(!isStarting) { return; } step += 0.5; if(step > temp_view_plus_two_text_length) step = textLength; invalidate();</div> <div>} <div>@Override public void onClick(View v) { if(isStarting) stopScroll(); else startScroll();</div> <div>} <div>} </div>
试试其它关键字
文字长短不限循环播放
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
JoanneTou
贡献的其它代码
(
3
)
.
textarea自动适应高度 无滚动条
.
textview(跑马灯效果)文字长短不限循环播放
.
读取活动目录AD
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3