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(執行間隔) } }解釋:重要的是開啟一個定時任務,執行線程。
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!