码迷,mamicode.com
首页 > 移动开发 > 详细

android自定义闪烁的文本

时间:2014-08-17 14:24:02      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:android   style   color   java   os   io   ar   art   

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

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(执行间隔)
	}
	

}



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



android自定义闪烁的文本,布布扣,bubuko.com

android自定义闪烁的文本

标签:android   style   color   java   os   io   ar   art   

原文地址:http://my.oschina.net/winHerson/blog/303149

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!