自定義TextView跑馬燈效果可控制啟動(dòng)/停止/速度/焦點(diǎn)
Android自帶的跑馬燈效果不太好控制,不能控制速度,不能即時(shí)停止和啟動(dòng),而且還受焦點(diǎn)的影響不已。由于項(xiàng)目需求需要用的可控制性高的跑馬燈效果,所以自己寫(xiě)了一個(gè)自定義的TextView
注意:在布局文件引用本view時(shí),paddingLeft,paddingRigh都必須為0dp,需要增加這兩個(gè)屬性的,大家可以自行修改代碼。
android:ellipsize="marquee" android:singleLine="true" 這兩個(gè)屬性也要加上
public class MarqueeText extends TextView implements Runnable {
private int currentScrollX;// 當(dāng)前滾動(dòng)的位置
private boolean isStop = false;
private int textWidth;
private boolean isMeasure = false;
public MarqueeText(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
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) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (!isMeasure) {// 文字寬度只需獲取一次就可以了
getTextWidth();
isMeasure = true;
}
}
/**
* 獲取文字寬度
*/
private void getTextWidth() {
Paint paint = this.getPaint();
String str = this.getText().toString();
textWidth = (int) paint.measureText(str);
}
@Override
public void run() {
currentScrollX -= 2;// 滾動(dòng)速度
scrollTo(currentScrollX, 0);
if (isStop) {
return;
}
if (getScrollX() <= -(this.getWidth())) {
scrollTo(textWidth, 0);
currentScrollX = textWidth;
// return;
}
postDelayed(this, 5);
}
// 開(kāi)始滾動(dòng)
public void startScroll() {
isStop = false;
this.removeCallbacks(this);
post(this);
}
// 停止?jié)L動(dòng)
public void stopScroll() {
isStop = true;
}
// 從頭開(kāi)始滾動(dòng)
public void startFor0() {
currentScrollX = 0;
startScroll();
}
}
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="start"
android:text="走起" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="stop"
android:text="停止" />
<Button
android:id="@+id/startfor0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startFor0"
android:text="從頭開(kāi)始" />
<simtice.demo.marqueetext.MarqueeText
android:id="@+id/test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#339320"
android:ellipsize="marquee"
android:singleLine="true"
android:text="這才是真正的文字跑馬燈效果這才是真正的字跑馬燈效果這才是真正的"
android:textColor="#000000"
android:textSize="20dp" >
</simtice.demo.marqueetext.MarqueeText>
</LinearLayout>
MainActivity
public class MainActivity extends Activity {
private MarqueeText test;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test = (MarqueeText) this.findViewById(R.id.test);
}
public void start(View v) {
test.startScroll();
}
public void stop(View v) {
test.stopScroll();
}
public void startFor0(View v){
test.startFor0();
}
}
- android中設(shè)置TextView/Button 走馬燈(Marquee)效果示例
- Android使用TextView跑馬燈效果
- Android基于TextView實(shí)現(xiàn)跑馬燈效果
- Android基于TextView不獲取焦點(diǎn)實(shí)現(xiàn)跑馬燈效果
- Android 中TextView中跑馬燈效果的實(shí)現(xiàn)方法
- Android基于TextView屬性android:ellipsize實(shí)現(xiàn)跑馬燈效果的方法
- Android 實(shí)現(xiàn)不依賴(lài)焦點(diǎn)和選中的TextView跑馬燈
- Android基于TextView實(shí)現(xiàn)的跑馬燈效果實(shí)例
- Android TextView實(shí)現(xiàn)跑馬燈效果的方法
- Android TextView跑馬燈效果實(shí)現(xiàn)方法
- Android中使用TextView實(shí)現(xiàn)文字跑馬燈效果
相關(guān)文章
Android利用AsyncTask異步類(lèi)實(shí)現(xiàn)網(wǎng)頁(yè)內(nèi)容放大縮小
這篇文章主要為大家介紹了利用AsyncTask異步類(lèi)實(shí)現(xiàn)網(wǎng)頁(yè)內(nèi)容放大縮小的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07

Android 桌面圖標(biāo)右上角顯示未讀消息數(shù)字

Android基于MLKit實(shí)現(xiàn)條形碼掃碼的代碼示例

Android下拉刷新控件SwipeRefreshLayout源碼解析

Android開(kāi)發(fā)解決字符對(duì)齊問(wèn)題方法

Android 創(chuàng)建與解析XML(四)——詳解Pull方式

Android自動(dòng)填充短信驗(yàn)證碼功能(demo)