Android之重寫TextView實現走馬燈效果

來源:互聯網
上載者:User

   TextView內建的走馬燈效果在失去焦點的情況下會無效,公司正好需要一個這樣的效果,即使失去焦點走馬燈效果依然存在,那麼該怎麼做呢?網上亂七八糟的代碼一大堆,寫的那麼複雜,所以我就寫了一個簡單的例子,下面直接上代碼了。

1.自訂TextView:

package com.zhf.TextAutoMoveDemo;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.util.AttributeSet;import android.widget.TextView;/** * 自訂TextView,TextView內建了該功能,但功能有限,最重要的是TextView失去焦點的情況下走馬燈效果會暫停! *  * @author administrator *  */public class MyTextView extends TextView implements Runnable {private Text text;public MyTextView(Context context, AttributeSet attrs) {super(context, attrs);text = new Text("走馬燈效果示範...",0, 20, 5);}public void startMove() {Thread thread = new Thread(this);thread.start();}@Overridepublic void run() {try {while (true) {// 1.重新整理postInvalidate();// 2.睡眠Thread.sleep(200L);// 3.移動text.move();}} catch (Exception e) {e.printStackTrace();}}@Overrideprotected void onDraw(Canvas canvas) {// 背景色canvas.drawColor(Color.WHITE);// 繪製文字text.draw(canvas);}}

2.實體類Text

package com.zhf.TextAutoMoveDemo;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;public class Text {private Paint paint;private String content;//文字內容private float x;//x座標private float y;//y座標private float stepX;//移動步長private float contentWidth;//文字寬度public Text(String content, float x, float y, float stepX) {this.content = content;this.x = x;this.y = y;this.stepX = stepX;//畫筆參數設定paint = new Paint();paint.setColor(Color.RED);paint.setTextSize(20f);this.contentWidth = paint.measureText(content);}public void move() {x -= stepX;if (x < -contentWidth)//移出螢幕後,從右側進入x = 320;//螢幕寬度,真實情況下應該動態擷取,不能寫死}public void draw(Canvas canvas) {canvas.drawText(content, x, y, paint);}}

3.主Activity

package com.zhf.TextAutoMoveDemo;import android.app.Activity;import android.os.Bundle;public class TextAutoMoveDemoActivity extends Activity {private MyTextView textView;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        textView=(MyTextView)findViewById(R.id.textView);        textView.startMove();    }}

4.布局檔案

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <com.zhf.TextAutoMoveDemo.MyTextView        android:id="@+id/textView"        android:layout_marginTop="10dip"        android:layout_width="fill_parent"        android:layout_height="30dip" />    <EditText        android:layout_marginTop="40dip"        android:hint="點擊獲得焦點"        android:layout_width="fill_parent"        android:layout_height="wrap_content" /></LinearLayout>

5.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.