Textview's built-in drive lamp effect will be ineffective when the focus is lost. The company just needs this effect. Even if the focus of the drive lamp is lost, what should we do? There are a lot of messy code on the Internet, which is so complicated to write, so I wrote a simple example, and the code is directly listed below.
1. Custom textview:
Package COM. zhf. textautomovedemo; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. util. attributeset; import android. widget. textview;/*** custom textview. textview comes with this function, but it has limited functions. The most important thing is that when textview loses focus, the result will be paused! ** @ Author administrator **/public class mytextview extends textview implements runnable {private text; Public mytextview (context, attributeset attrs) {super (context, attrs ); TEXT = new text ("Lantern effect demo... ", 0, 20, 5);} public void startmove () {thread = new thread (this); thread. start () ;}@ overridepublic void run () {try {While (true) {// 1. refresh postinvalidate (); // 2. sleep thread. sleep (200l); // 3. text. move () ;}} catch (exception e) {e. printstacktrace () ;}@ overrideprotected void ondraw (canvas) {// background color canvas. drawcolor (color. white); // draw text. draw (canvas );}}
2. Object text
Package COM. zhf. textautomovedemo; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; public class text {private paint; private string content; // text content private float X; // X coordinate private float y; // y coordinate private float stepx; // mobile step size private float contentwidth; // text width public text (string content, float X, float y, float stepx) {This. content = content; this. X = x; this. y = y; this. stepx = stepx; // set paint = new paint (); paint. setcolor (color. red); paint. settextsize (20f); this. contentwidth = paint. measuretext (content);} public void move () {X-= stepx; If (x <-contentwidth) // after removing the screen, enter x = 320 from the right; // screen width, which should be dynamically obtained in actual situations and cannot be written to death} public void draw (canvas) {canvas. drawtext (content, X, Y, paint );}}
3. Main Activity
package com.zhf.TextAutoMoveDemo;import android.app.Activity;import android.os.Bundle;public class TextAutoMoveDemoActivity extends Activity {private MyTextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView=(MyTextView)findViewById(R.id.textView); textView.startMove(); }}
4. layout File
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <COM. zhf. textautomovedemo. mytextview Android: Id = "@ + ID/textview" Android: layout_margintop = "10dip" Android: layout_width = "fill_parent" Android: layout_height = "30dip"/> <edittext Android: layout_margintop = "40dip" Android: hint = "click to get focus" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"/> </linearlayout>
5.