L遷客

          技術(shù)博客
          隨筆 - 1, 文章 - 12, 評(píng)論 - 1, 引用 - 0
          數(shù)據(jù)加載中……

          自定義TextView跑馬燈效果,可控制啟動(dòng),停止,和速度

          自定義TextView跑馬燈效果,可控制啟動(dòng),停止,和速度

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

                  }

          }

          posted on 2013-02-24 16:44 L遷客 閱讀(83) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 金山区| 龙山县| 双峰县| 桦南县| 陵水| 白山市| 佛山市| 正蓝旗| 清丰县| 文山县| 贵州省| 望谟县| 郯城县| 丰县| 铁岭县| 财经| 千阳县| 海南省| 柘荣县| 土默特右旗| 东至县| 怀来县| 靖边县| 宜兴市| 喀喇| 沐川县| 洛宁县| 图木舒克市| 阿克苏市| 象州县| 东乡族自治县| 刚察县| 平定县| 嘉禾县| 竹北市| 安徽省| 鱼台县| 北碚区| 龙州县| 南昌县| 铜山县|