国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義TextView跑馬燈效果

2019-10-23 18:27:55
字體:
來源:轉載
供稿:網友

Android自帶的跑馬燈效果不太好控制,還必須要滿足條件才能有效果,而且速度不受控制。前面我的博客中有一篇就是用Android自帶的跑馬燈效果的,但是基于不同的使用效果,這里在網上找到了一個更好的方法。沿用了作者的一些方法,但是添加了更好的擴展功能,和大家一起分享。這里面有控制往左往右兩個方向的實現。

1、首先是簡單的布局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="滾動效果,不管多少字"      android:ellipsize = "marquee" // 跑馬燈效果,字數不超過就不動,超過就滾動     android:textColor="#000000"     android:textSize="20dp" >   </com.xuhui.customrolllight.MarqueeText>   </LinearLayout> 

2、自定義滾動方法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; // 當前滾動的位置 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;//滾動速度.+號表示往左邊- * scrollTo(currentScrollX,0); if(isStop){ return; } * if(getScrollX()<=-(this.getWidth())){ scrollTo(textWidth,0); * currentScrollX=textWidth; } postDelayed(this, 5); } */ public void run() { currentScrollX += 2;// 滾動速度.+號表示往左邊- scrollTo(currentScrollX, 0); if (isStop) { return; } if (getScrollX() >= (textWidth)) { currentScrollX = -(this.getWidth());// 當前出現的位置 } postDelayed(this, 1); } /*( public void run() {   // currentScrollX += 3;// 滾動速度.+號表示往左邊- // scrollTo(currentScrollX, 0);   if (textWidth>this.getWidth()) { currentScrollX += 3;// 滾動速度.+號表示往左邊- scrollTo(currentScrollX, 0); } if (getScrollX() >= (textWidth)) { // scrollTo(this.getWidth(),0); currentScrollX = -(this.getWidth());// 當前出現的位置 } postDelayed(this, 5); })這里面實現的是沒有省略號的效果。文字沒有超出框的長度就不滾,超出就滾*/  // 開始滾動 public void startScroll() { isStop = false; this.removeCallbacks(this); post(this); }   // 停止滾動 public void stopScroll() { isStop = true; }   // 從頭開始滾動 public void startFromHead() { currentScrollX = 0; startScroll(); } } 

上面注釋掉的代碼是實現文字往右邊跑

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(); } } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久治县| 广丰县| 丽水市| 遂宁市| 巴林右旗| 呈贡县| 陕西省| 岐山县| 庐江县| 喀什市| 吉水县| 延川县| 杭锦旗| 泉州市| 长沙市| 奎屯市| 抚宁县| 香格里拉县| 嘉黎县| 湘潭市| 邳州市| 肇州县| 邢台市| 叙永县| 荆州市| 乌拉特前旗| 昂仁县| 漳州市| 南昌市| 武山县| 泸溪县| 靖边县| 昌江| 海宁市| 得荣县| 磐石市| 喀喇沁旗| 娱乐| 水城县| 晋中市| 亚东县|