Android文字跑馬燈控制項(文本自動水平滾動)

來源:互聯網
上載者:User

代碼如下:

AutoScrollTextView.java

package com.cn.lhq;<br />import android.content.Context;<br />import android.graphics.Canvas;<br />import android.graphics.Paint;<br />import android.os.Parcel;<br />import android.os.Parcelable;<br />import android.util.AttributeSet;<br />import android.view.Display;<br />import android.view.View;<br />import android.view.WindowManager;<br />import android.view.View.OnClickListener;<br />import android.widget.TextView;<br />/**<br /> * TODO 單行文本跑馬燈控制項<br /> */<br />public class AutoScrollTextView extends TextView implements OnClickListener {<br />//public final static String TAG = AutoScrollTextView.class.getSimpleName();<br />private float textLength = 0f;// 文本長度<br />private float viewWidth = 0f;<br />private float step = 0f;// 文字的橫座標<br />private float y = 0f;// 文字的縱座標<br />private float temp_view_plus_text_length = 0.0f;// 用於計算的臨時變數<br />private float temp_view_plus_two_text_length = 0.0f;// 用於計算的臨時變數<br />public boolean isStarting = false;// 是否開始滾動<br />private Paint paint = null;// 繪圖樣式<br />private String text = "";// 常值內容<br />public AutoScrollTextView(Context context) {<br />super(context);<br />initView();<br />}<br />public AutoScrollTextView(Context context, AttributeSet attrs) {<br />super(context, attrs);<br />initView();<br />}<br />public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {<br />super(context, attrs, defStyle);<br />initView();<br />}<br />/**<br /> * 初始化控制項<br /> */<br />private void initView() {<br />setOnClickListener(this);<br />}<br />/**<br /> * 文本初始化,每次更改常值內容或者文字效果等之後都需要重新初始化一下<br /> */<br />public void init(WindowManager windowManager) {<br />paint = getPaint();<br />text = getText().toString();<br />textLength = paint.measureText(text);<br />viewWidth = getWidth();<br />if (viewWidth == 0) {<br />if (windowManager != null) {<br />Display display = windowManager.getDefaultDisplay();<br />viewWidth = display.getWidth();<br />}<br />}<br />step = textLength;<br />temp_view_plus_text_length = viewWidth + textLength;<br />temp_view_plus_two_text_length = viewWidth + textLength * 2;<br />y = getTextSize() + getPaddingTop();<br />}<br />@Override<br />public Parcelable onSaveInstanceState() {<br />Parcelable superState = super.onSaveInstanceState();<br />SavedState ss = new SavedState(superState);<br />ss.step = step;<br />ss.isStarting = isStarting;<br />return ss;<br />}<br />@Override<br />public void onRestoreInstanceState(Parcelable state) {<br />if (!(state instanceof SavedState)) {<br />super.onRestoreInstanceState(state);<br />return;<br />}<br />SavedState ss = (SavedState) state;<br />super.onRestoreInstanceState(ss.getSuperState());<br />step = ss.step;<br />isStarting = ss.isStarting;<br />}<br />public static class SavedState extends BaseSavedState {<br />public boolean isStarting = false;<br />public float step = 0.0f;<br />SavedState(Parcelable superState) {<br />super(superState);<br />}<br />@Override<br />public void writeToParcel(Parcel out, int flags) {<br />super.writeToParcel(out, flags);<br />out.writeBooleanArray(new boolean[] { isStarting });<br />out.writeFloat(step);<br />}<br />public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {<br />public SavedState[] newArray(int size) {<br />return new SavedState[size];<br />}<br />@Override<br />public SavedState createFromParcel(Parcel in) {<br />return new SavedState(in);<br />}<br />};<br />private SavedState(Parcel in) {<br />super(in);<br />boolean[] b = null;<br />in.readBooleanArray(b);<br />if (b != null && b.length > 0)<br />isStarting = b[0];<br />step = in.readFloat();<br />}<br />}<br />/**<br /> * 開始滾動<br /> */<br />public void startScroll() {<br />isStarting = true;<br />invalidate();<br />}<br />/**<br /> * 停止滾動<br /> */<br />public void stopScroll() {<br />isStarting = false;<br />invalidate();<br />}<br />@Override<br />public void onDraw(Canvas canvas) {<br />canvas.drawText(text, temp_view_plus_text_length - step, y, paint);<br />if (!isStarting) {<br />return;<br />}<br />step += 0.5;<br />if (step > temp_view_plus_two_text_length)<br />step = textLength;<br />invalidate();<br />}<br />@Override<br />public void onClick(View v) {<br />if (isStarting)<br />stopScroll();<br />else<br />startScroll();<br />}<br />}

 

實現了寬度的判斷,文本自動滾動及開始和停止滾動等功能。

在UI xml中的配置如下:

<com.cn.lhq.AutoScrollTextView<br /> android:id="@+id/TextViewNotice"<br />android:layout_height="30px"<br />android:layout_width="fill_parent"<br />android:text="abcdefghijklmn"<br />android:textColor="#000"<br />android:inputType="text"<br />android:background="#EEE"<br />android:textSize="20px"/>

 

在Activity中的使用方法如下:

//啟動公告捲軸<br />autoScrollTextView = (AutoScrollTextView)findViewById(R.id.TextViewNotice);<br />autoScrollTextView.init(getWindowManager());<br />autoScrollTextView.startScroll();

 

如果想改變跑馬燈的文字內容或者文字效果,則在調用完setText方法之後,需要再調用一下init(WindowManager windowManager)方法,重新進行初始化和相關參數的計算。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.