最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android自定義TextView跑馬燈效果

 更新時(shí)間:2017年05月25日 16:44:15   作者:a15838319826  
這篇文章主要為大家詳細(xì)介紹了Android自定義TextView跑馬燈效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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="開(kāi)始" /> 
 
 
  <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ù)不超過(guò)就不動(dòng),超過(guò)就滾動(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)的是沒(méi)有省略號(hào)的效果。文字沒(méi)有超出框的長(zhǎng)度就不滾,超出就滾*/ 
 
// 開(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 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中的存儲(chǔ)詳解

    Android中的存儲(chǔ)詳解

    大家好,本篇文章主要講的是Android中的存儲(chǔ)詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • Android實(shí)現(xiàn)帶有進(jìn)度條的按鈕效果

    Android實(shí)現(xiàn)帶有進(jìn)度條的按鈕效果

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶有進(jìn)度條的按鈕效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • android簡(jiǎn)單自定義View實(shí)現(xiàn)五子棋

    android簡(jiǎn)單自定義View實(shí)現(xiàn)五子棋

    這篇文章主要為大家詳細(xì)介紹了android簡(jiǎn)單自定義View實(shí)現(xiàn)五子棋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Android 馬賽克(Mosaics)效果

    Android 馬賽克(Mosaics)效果

    這篇文章主要為大家介紹了Android 馬賽克(Mosaics)效果,馬賽克(Mosaics)效果應(yīng)用非常廣泛,想要學(xué)習(xí)的不要錯(cuò)過(guò)本文,可以借鑒參考一下
    2016-01-01
  • Android自定義實(shí)現(xiàn)循環(huán)滾輪控件WheelView

    Android自定義實(shí)現(xiàn)循環(huán)滾輪控件WheelView

    滾輪布局WheelView大家經(jīng)常使用,比如在選擇生日的時(shí)候,風(fēng)格類(lèi)似系統(tǒng)提供的DatePickerDialog,這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)循環(huán)滾輪控件WheelView,感興趣的小伙伴們可以參考一下
    2016-07-07
  • 基于Android Flutter編寫(xiě)貪吃蛇游戲

    基于Android Flutter編寫(xiě)貪吃蛇游戲

    貪吃蛇是一款足夠經(jīng)典的游戲。本文將利用Android中的Flutter編寫(xiě)這一經(jīng)典的小游戲,文中的示例代碼講解詳細(xì),感興趣的可以了解一下
    2022-03-03
  • Android GridView不改變背景色實(shí)現(xiàn)網(wǎng)格線效果

    Android GridView不改變背景色實(shí)現(xiàn)網(wǎng)格線效果

    這篇文章主要介紹了Android GridView不改變背景色實(shí)現(xiàn)網(wǎng)格線效果,需要的朋友可以參考下
    2016-03-03
  • android利用消息機(jī)制獲取網(wǎng)絡(luò)圖片

    android利用消息機(jī)制獲取網(wǎng)絡(luò)圖片

    這篇文章主要為大家詳細(xì)介紹了android利用消息機(jī)制獲取網(wǎng)絡(luò)圖片的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android手機(jī)聯(lián)系人快速索引(手機(jī)通訊錄)

    Android手機(jī)聯(lián)系人快速索引(手機(jī)通訊錄)

    最近需要實(shí)現(xiàn)一個(gè)手機(jī)通訊錄的快速索引功能。根據(jù)姓名首字母快速索引功能,下面通過(guò)本篇文章給大家介紹Android手機(jī)聯(lián)系人快速索引(手機(jī)通訊錄)的相關(guān)代碼,需要的朋友參考下
    2015-12-12
  • Android視頻懸浮窗口實(shí)現(xiàn)的示例代碼

    Android視頻懸浮窗口實(shí)現(xiàn)的示例代碼

    這篇文章主要介紹了Android視頻懸浮窗口實(shí)現(xiàn)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04

最新評(píng)論

兴宁市| 明光市| 普定县| 搜索| 泰来县| 普陀区| 吉安县| 寿光市| 天长市| 宁武县| 紫阳县| 简阳市| 登封市| 万州区| 中方县| 垫江县| 遂川县| 武强县| 金昌市| 仁化县| 翁牛特旗| 全椒县| 福泉市| 邛崃市| 望奎县| 潞西市| 洪泽县| 札达县| 唐河县| 开鲁县| 方城县| 堆龙德庆县| 石景山区| 余庆县| 双城市| 咸阳市| 建瓯市| 会东县| 靖远县| 米易县| 工布江达县|