TextView文字特效設計----跑馬燈、閃爍文字
一、文字實現走馬燈的效果法一
package irdc.ScrollingText;
import android.app.Activity;import android.os.Bundle;import android.widget.TextView;
public class ScrollingText extends Activity{ public TextView t1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState)
t1.setHorizontallyScrolling(true);
t1.setFocusable(true); }}
設定文字t1的焦點為真(意思為焦點在t1上),設定文字
接下來我們看看main.xml檔案。
<?xml version="1.0" encoding="utf-8"?><AbsoluteLayoutandroid:id="@+id/widget35"android:layout_;fill_parent"android:layout_height="fill_parent"android:background="@drawable/black"
<ViewStub
android:layout_y="221dip"
android:layout_;wrap_content"
android:layout_x="103dip"
android:id="@+id/ViewStub01"
android:layout_height="wrap_content">
</ViewStub></AbsoluteLayout>
在TextView中添加了三行藍色的欄位,其中
singleLine表示TextView中文字為單行文字如果你在你的程式中設定了setHorizontallyScrolling(true);
關鍵之處ellipsize="marquee" 此語句表示我們將TextView設定為了一個走馬燈;
marqueeRepeatLimit="marquee_forever" 表示走馬燈的滾動效果重複的次數,你可以填一個自然數。
二、跑馬燈效果法二:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable ="true"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:textSize="24dp"
android:text="@string/hello"
android:background="@drawable/bg"
/>
<!--啟用焦點-->
android:focusable="true"
<!--單行顯示-->
android:singleLine="true"
<!--這裡設定為超出文字後滾動顯示-->
android:ellipsize="marquee"
<!--這個是設定滾動幾次,這裡是無限迴圈-->
android:marqueeRepeatLimit="marquee_forever"
<!--TouchMode模式的焦點啟用-->
android:focusableInTouchMode="true"
<!--橫向超出後是否有橫向滾動條-->
android:scrollHorizontally="true"
mText.setText(msgs);//設定顯示內容
mText.setSpeed(msgSpeed);//設定速度
mText.setDelayed(msgDelay);//設定時間間隔
mText.startScroll();
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;
//可控制滾動速度
public class MarqueeText extends TextView implements Runnable {
private int currentScrollX=0;// 初始滾動的位置
private int firstScrollX=0;
private boolean isStop = false;
private int textWidth;
private int mWidth=0; //控制元件寬度
private int speed=2;
private int delayed=1000;
private int endX; //滾動到哪個位置
private boolean isFirstDraw=true; //當首次或文字改變時重置
public MarqueeText(Context context) {
super(context);
}
public MarqueeText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(isFirstDraw){
getTextWidth();
firstScrollX=getScrollX();
//起始位置不一定為0,改變內容後會變,需重新賦值
currentScrollX=firstScrollX;
mWidth=this.getWidth();
endX=firstScrollX+textWidth-mWidth/2;
isFirstDraw=false;
}
}
//每次滾動幾點
public void setSpeed(int sp){
speed=sp;
}
//滾動間隔時間,毫秒
public void setDelayed(int delay){
delayed=delay;
}
/**
* 獲取文字寬度
*/
private void getTextWidth() {
Paint paint = this.getPaint();
String str = this.getText().toString();
textWidth = (int) paint.measureText(str);
}
@Override
public void run() {
//currentScrollX += 1;// 滾動速度
currentScrollX += speed;// 滾動速度,每次滾動幾點
scrollTo(currentScrollX, 0);
if (isStop) {
return;
}
//從頭開始
if (currentScrollX >= endX) {
//scrollTo(0, 0);
//currentScrollX = 0; //原文重置為0,發現控制元件所放的位置不同,初始位置不一定為0
scrollTo(firstScrollX,0);
currentScrollX=firstScrollX;
postDelayed(this,4000);
}else{
postDelayed(this, delayed);}
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore,int lengthAfter) {
isStop=true;
//停止滾動
this.removeCallbacks(this); //清空佇列
currentScrollX=firstScrollX; //滾動到初始位置
this.scrollTo(currentScrollX, 0);
super.onTextChanged(text, start, lengthBefore, lengthAfter);
isFirstDraw=true; //需重新設定引數
isStop=false;
postDelayed(this,4000); //頭部停4秒
}
// 開始滾動
public void startScroll() {
isStop = false;
this.removeCallbacks(this);
postDelayed(this,4000);
}
// 停止滾動
public void stopScroll() {
isStop = true;
}
// 從頭開始滾動
public void startFor0() {
currentScrollX = 0;
startScroll();
}
}
三、PS:閃爍文字的製作
例一:
private boolean change = false;
TextView touchScreen = (TextView)findViewById(R.id.touchscreen);//獲取頁面textview物件
Timer timer = new Timer();
timer.schedule(task,1,300); //引數分別是delay(多長時間後執行),duration(執行間隔)
TimerTask task = new TimerTask(){ public void run() { runOnUiThread(new Runnable(){ public void run() { if(change){ change = false; touchScreen.setTextColor(Color.TRANSPARENT); //這個是透明,=看不到文字 }else{ change = true; touchScreen.setTextColor(Color.RED); } }}); } };
方法一:Handler+Thread
package com.xunfang.handerDemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
/**
* handler定時器
*
* @author Smalt
*
*/
public class HanderDemoActivity extends Activity {
TextView tvShow;
private int i = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvShow = (TextView) findViewById(R.id.tv_show);
new Thread(new ThreadShow()).start();
}
// handler類接收資料
Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 1) {
tvShow.setText(Integer.toString(i++));
System.out.println("receive....");
}
};
};
// 執行緒類
class ThreadShow implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(1000);
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
System.out.println("send...");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("thread error...");
}
}
}
}
}
方法二:Handler類自帶的postDelyed
package com.xunfang.handerDemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
/**
* handler定時器使用postDelyed實現
*
* @author Smalt
*
*/
public class HanderDemoActivity extends Activity {
TextView tvShow;
private int i = 0;
private int TIME = 1000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvShow = (TextView) findViewById(R.id.tv_show);
handler.postDelayed(runnable, TIME); //每隔1s執行
}
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
// handler自帶方法實現定時器
try {
handler.postDelayed(this, TIME);
tvShow.setText(Integer.toString(i++));
System.out.println("do...");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("exception...");
}
}
};
}
方法三:Handler+Timer+TimerTask
package com.xunfang.handerDemo;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
/**
* 定時器實現:Handler+Timer+TimerTask
*
* @author Smalt
*
*/
public class HanderDemoActivity extends Activity {
TextView tvShow;
private int i = 0;
private int TIME = 1000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tvShow = (TextView) findViewById(R.id.tv_show);
timer.schedule(task, 1000, 1000); // 1s後執行task,經過1s再次執行
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 1) {
tvShow.setText(Integer.toString(i++));
}
super.handleMessage(msg);
};
};
Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
// 需要做的事:傳送訊息
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};