自定义UI-TextView实现文本自动滚动显示
来源:互联网 发布:希尔薇 知乎 编辑:程序博客网 时间:2024/06/05 02:41
自定义UI是高手必经之路,本人目前也在加紧学习中,本篇博文中实现的事自定义TextView实现文本内容的滚动显示,在文本内容超出textview所能显示的区域之后,超出部分没有办法显示,为了能够显示,所以自定义一个属性,实现文本内容的滚动显示。(最后有源码下载)
废话不多说,先给出结果图:
相信大家一看结果图就明白了吧。
就是实现这种效果。比较简单。下面一一道来~~
因为在这个自定义组件中,使用到了滚动间隔时间,所以需要增加一个自定义属性,那么如何增加自定义属性呢?
首先在values文件夹下面,创建一个attr.xml文件(如果已经有了就不需要创建了),在attr.xml中增加如下的内容:
<?xml version="1.0" encoding="utf-8"?><resources> <attr name="duration" format="integer"/> <declare-styleable name="customTextView"> <attr name="duration" /> </declare-styleable></resources>
这样我们就完成了声明自定义的属性duration。并且,指定了该属性的类型integer类型。
(这里的format值有几种形式:string,color,demension,integer,enum,reference,float,boolean,fraction,flag等,都很简单,几个特殊的我在这里说一下:enum类型,相当于android:grivity属相的值,值是从固定的几个值中选择的。
reference类型,就是android:src属性的值类型,需要引用R.drawable.xx)
定义了属性之后,就要使用了,怎么使用呢?下面给出布局文件来使用:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:yin="http://schemas.android.com/apk/res/com.yin.customtextview" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.yin.customtextview.MainActivity" > <com.yin.customtextview.CustomTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textSize="16sp" android:lines="1" yin:duration="1000" android:layout_centerInParent="true" /></RelativeLayout>
这就是上面的图片的界面的布局文件,里面比较特殊的是这句代码:
xmlns:yin=”http://schemas.android.com/apk/res/com.yin.customtextview”
这是自定义的,yin是自定义的,自己想如何定义名字都是可以的,后面的值http://schemas.android.com/apk/res/com.yin.customtextview中前面部分是固定的,http://schemas.android.com/apk/res/xxx,xxx部分是项目的包名packagename,这样定义之后,就可以在下面的自定义组件中使用了。例如上面的布局文件中:yin:duration=”1000”
下面给出主界面的代码:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
非常简单吧,其实主要的是自定义的CustomTextView,代码如下:
public class CustomTextView extends TextView { private int duration; private Timer timer; private String text; private String textc; private int i = 0; public CustomTextView(Context context) { super(context); init(context,null,0); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs, 0); } public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context, attrs, defStyle); } private void init(Context context,AttributeSet attrs, int defStyle){ TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.customTextView, defStyle, 0); int j = typedArray.getIndexCount(); for (int i = 0; i < j; i++) { int k = typedArray.getIndex(i); switch (k) { case R.styleable.customTextView_duration: duration = typedArray.getInt(k, 0); break; default: break; } } typedArray.recycle();//清除设置,否则上次的定义值可能会影响本次的效果 } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); text = getText().toString(); timer = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { textc = text.substring(i, text.length()); i++; if (i>=text.length()) { i=0; } postInvalidate(); } }; timer.schedule(task, 100, duration); } @Override protected void onDraw(Canvas canvas) { if (textc != null) { canvas.drawText(textc, getBaseline(), getBaseline(), getPaint()); } } @Override public boolean isInEditMode() { return false; } //脱离Window,定时器取消 @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); timer.cancel(); }}
整个自定义TextView组件还是比较简单的,主要使用了一个定时器,在初始化时候,定义定时器并启动,每个duration时间,执行一次,这样就实现了文本内容的滚动显示。
各位朋友,给留个言,顶一下呗~~~^_^
源码下载
- 自定义UI-TextView实现文本自动滚动显示
- TextView实现自动滚动显示
- 实现TextView自动左右滚动文本
- Android中TextView文本过长滚动显示实现
- 垂直滚动textview自定义实现
- Android UI系列:TextView显示文本
- 自定义Textview显示自动换行
- Android 自定义TextView实现文本内容自动调整字体大小以适应TextView的大小
- Android 自定义TextView实现文本内容自动调整字体大小以适应TextView的大小
- 实现TextView文本的上下滚动
- 关于android TextView在不需要较焦点的情况下,自动滚动文本实现小技巧
- Actionbar 自定义多个TextView滚动显示
- 自定义控件实现文本滚动
- textview中自动换行显示文本内容
- TextView布局文件中实现文本过长时滚动显示效果
- js实现滚动文本显示
- 自定义TextView任意长度文本下自动改变文字大小以完全显示
- Android UI 之居中绘制文本内容的正确方法——实现自定义一个TextView
- DuiVision开发教程(1)-创建DuiVision工程
- IOS NSUserDefaults-轻量级本地数据存储
- C#树结构操作逻辑整理
- uml精粹——5.类图(高级概念)
- TCP粘包
- 自定义UI-TextView实现文本自动滚动显示
- 字母大小写分类排序输出
- 一个女人13小时内拍的两张照片,所有人看完都沉默了~
- android初学笔记1
- 将栈S1中的元素复制到栈s2中,并保持顺序不变,使用额外的一个栈
- 从HTTP说起,肆意漫谈
- 迷你MVVM框架 avalonjs 入门教程
- 基于依存树相似度算法讨论
- android开源项目