Android自定義TextView跑馬燈效果
Android自帶的跑馬燈效果不太好控制,還必須要滿足條件才能有效果,而且速度不受控制。前面我的博客中有一篇就是用Android自帶的跑馬燈效果的,但是基于不同的使用效果,這里在網(wǎng)上找到了一個(gè)更好的方法。沿用了作者的一些方法,但是添加了更好的擴(kuò)展功能,和大家一起分享。這里面有控制往左往右兩個(gè)方向的實(shí)現(xiàn)。
1、首先是簡(jiǎn)單的布局main.xml
<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="startFromHead"
android:text="重置" />
<com.xuhui.customrolllight.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="滾動(dòng)效果,不管多少字"
android:ellipsize = "marquee" // 跑馬燈效果,字?jǐn)?shù)不超過就不動(dòng),超過就滾動(dòng)
android:textColor="#000000"
android:textSize="20dp" >
</com.xuhui.customrolllight.MarqueeText>
</LinearLayout>
2、自定義滾動(dòng)方法MarqueeText.Java
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; // 當(dāng)前滾動(dòng)的位置
private boolean isStop = false;
private int textWidth;
private boolean isMeasure = false;
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 onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
currentScrollX = this.getWidth();
}
protected void onDraw(Canvas canvas) {
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)速度.+號(hào)表示往左邊-
* scrollTo(currentScrollX,0); if(isStop){ return; }
* if(getScrollX()<=-(this.getWidth())){ scrollTo(textWidth,0);
* currentScrollX=textWidth; } postDelayed(this, 5); }
*/
public void run() {
currentScrollX += 2;// 滾動(dòng)速度.+號(hào)表示往左邊-
scrollTo(currentScrollX, 0);
if (isStop) {
return;
}
if (getScrollX() >= (textWidth)) {
currentScrollX = -(this.getWidth());// 當(dāng)前出現(xiàn)的位置
}
postDelayed(this, 1);
}
/*( public void run() {
// currentScrollX += 3;// 滾動(dòng)速度.+號(hào)表示往左邊-
// scrollTo(currentScrollX, 0);
if (textWidth>this.getWidth()) {
currentScrollX += 3;// 滾動(dòng)速度.+號(hào)表示往左邊-
scrollTo(currentScrollX, 0);
}
if (getScrollX() >= (textWidth)) {
// scrollTo(this.getWidth(),0);
currentScrollX = -(this.getWidth());// 當(dāng)前出現(xiàn)的位置
}
postDelayed(this, 5);
})這里面實(shí)現(xiàn)的是沒有省略號(hào)的效果。文字沒有超出框的長(zhǎng)度就不滾,超出就滾*/
// 開始滾動(dòng)
public void startScroll() {
isStop = false;
this.removeCallbacks(this);
post(this);
}
// 停止?jié)L動(dòng)
public void stopScroll() {
isStop = true;
}
// 從頭開始滾動(dòng)
public void startFromHead() {
currentScrollX = 0;
startScroll();
}
}
上面注釋掉的代碼是實(shí)現(xiàn)文字往右邊跑
3、下面是主程序MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
private MarqueeText test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test=(MarqueeText) findViewById(R.id.test);
}
public void start(View v){
test.startScroll();
}
public void stop(View v){
test.stopScroll();
}
public void startFromHead(View v){
test.startFromHead();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android輸入框內(nèi)容改變的監(jiān)聽事件實(shí)例
下面小編就為大家分享一篇android輸入框內(nèi)容改變的監(jiān)聽事件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02
DataBinding onClick的七種點(diǎn)擊方式
這篇文章主要給大家介紹了關(guān)于DataBinding onClick的七種點(diǎn)擊方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Android屬性動(dòng)畫實(shí)現(xiàn)布局的下拉展開效果
這篇文章主要為大家詳細(xì)介紹了Android屬性動(dòng)畫實(shí)現(xiàn)布局的下拉展開效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
Android多線程斷點(diǎn)續(xù)傳下載示例詳解
這篇文章主要為大家詳細(xì)介紹了Android多線程斷點(diǎn)續(xù)傳下載示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Kotlin中空判斷與問號(hào)和感嘆號(hào)標(biāo)識(shí)符使用方法
最近使用kotlin重構(gòu)項(xiàng)目,遇到了一個(gè)小問題,在Java中,可能會(huì)遇到判斷某個(gè)對(duì)象是否為空,為空?qǐng)?zhí)行一段邏輯,不為空?qǐng)?zhí)行另外一段邏輯,下面這篇文章主要給大家介紹了關(guān)于Kotlin中空判斷與問號(hào)和感嘆號(hào)標(biāo)識(shí)符處理操作的相關(guān)資料,需要的朋友可以參考下2022-12-12
Android簡(jiǎn)單的利用MediaRecorder進(jìn)行錄音的實(shí)例代碼
MediaRecorder可以進(jìn)行簡(jiǎn)單的錄音,由于操作簡(jiǎn)單所以可以用來進(jìn)行基本的錄音。下面提供一個(gè)簡(jiǎn)單的例子,記得在Mainfest文件中添加權(quán)限2013-08-08
Android學(xué)習(xí)之Intent中顯示意圖和隱式意圖的用法實(shí)例分析
這篇文章主要介紹了Android學(xué)習(xí)之Intent中顯示意圖和隱式意圖的用法,以實(shí)例形式分析了Intent通訊的相關(guān)技巧與注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10

