首页 > 代码库 > android自定义闪烁的文本

android自定义闪烁的文本

自定义闪烁文本,直接代码搞上:

package custom.text.view;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

/***
 * @author tianbx
 * @version 1.0
 * 闪烁的文本
 * */
public class FlickerTextView extends TextView {
	private static final String LOG_TAG = "FlickerTextView";
	boolean change = false;
	private Handler handler = null;

	public FlickerTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		Log.e(LOG_TAG, "super(context, attrs, defStyle)");
		startFlicker();
		// TODO Auto-generated constructor stub
	}

	public FlickerTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		startFlicker();
		Log.e(LOG_TAG, "super(context, attrs)");
		// TODO Auto-generated constructor stub
	}

	public FlickerTextView(Context context) {
		super(context);
		startFlicker();
		Log.e(LOG_TAG, "FlickerTextView(Context context)");
	}
	
	
	
	public void startFlicker(){
		handler = new Handler(){
			@Override
			public void dispatchMessage(Message msg) {
				if(change){
					change = false;
					setTextColor(Color.TRANSPARENT); //这个是透明,=看不到文字
				}else{
					change = true;
					setTextColor(Color.RED);
				}
			}
		};
		
		Timer timer = new Timer();
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				Message msg = new Message();
				handler.sendMessage(msg);
			}
		};
		timer.schedule(task,1,300);  //参数分别是delay(多长时间后执行),duration(执行间隔)
	}
	

}



解释:重要的是开启一个定时任务,执行线程。