Android开发之单行文本自动播放效果

来源:互联网 发布:和文化精髓知乎 编辑:程序博客网 时间:2024/06/04 19:06

最近在玩游戏时,看见了一个很不错的东西,就是单行文本自动播放效果,想了想在Android中是否也能实现,就上网搜了一下,发现有很多例子。

先上图生气(gif效果不是很好尴尬


先来看布局:

<com.example.scrolltext.AutoScrollTextView        android:id="@+id/TextViewNotic"        android:layout_width="fill_parent"        android:layout_height="30dp"        android:background="#EEE"        android:inputType="text"        android:textColor="#000"        android:textSize="20sp" />
布局很简单,直接引用就可以了。

接下来看如何使用:

scrollTextView=(AutoScrollTextView) findViewById(R.id.TextViewNotic);scrollTextView.setText(R.string.text);scrollTextView.init(getWindowManager());scrollTextView.startScroll();
在改变文字内容后,必须调用init()方法。

下面是具体的代码:

package com.example.scrolltext;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.os.Parcel;import android.os.Parcelable;import android.util.AttributeSet;import android.widget.TextView;import android.view.Display;import android.view.WindowManager;/** * 单行文本自动播放效果 * @author Administrator * */public class ScrollTextView extends TextView {public final static String TAG = ScrollTextView.class.getSimpleName();private float textLength = 0f;// 文本长度private float viewWidth = 0f;private float x = 0f;// 文字的横坐标private float y = 0f;// 文字的纵坐标private float temp_view_plus_text_length = 0.0f;// 用于计算的临时变量private float temp_view_plus_two_text_length = 0.0f;// 用于计算的临时变量public boolean isStarting = false;// 是否开始滚动private Paint paint = null;// 绘图样式private String text = "";// 文本内容public ScrollTextView(Context context) {super(context);}public ScrollTextView(Context context, AttributeSet attrs) {super(context, attrs);}public ScrollTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}/** * 文本初始化,每次更改文本内容或者文本效果等之后都需要重新初始化一下 */public void init(WindowManager windowManager) {paint = getPaint();text = getText().toString();textLength = paint.measureText(text);viewWidth = getWidth();if (viewWidth == 0) {if (windowManager != null) {Display display = windowManager.getDefaultDisplay();viewWidth = display.getWidth();}}x = textLength;temp_view_plus_text_length = viewWidth + textLength;temp_view_plus_two_text_length = viewWidth + textLength * 2;y = getTextSize() + getPaddingTop();}@Overridepublic Parcelable onSaveInstanceState() {Parcelable superState = super.onSaveInstanceState();SavedState ss = new SavedState(superState);ss.step = x;ss.isStarting = isStarting;return ss;}@Overridepublic void onRestoreInstanceState(Parcelable state) {if (!(state instanceof SavedState)) {super.onRestoreInstanceState(state);return;}SavedState ss = (SavedState) state;super.onRestoreInstanceState(ss.getSuperState());x = ss.step;isStarting = ss.isStarting;}public static class SavedState extends BaseSavedState {public boolean isStarting = false;public float step = 0.0f;SavedState(Parcelable superState) {super(superState);}@Overridepublic void writeToParcel(Parcel out, int flags) {super.writeToParcel(out, flags);out.writeBooleanArray(new boolean[] { isStarting });out.writeFloat(step);}public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {public SavedState[] newArray(int size) {return new SavedState[size];}@Overridepublic SavedState createFromParcel(Parcel in) {return new SavedState(in);}};private SavedState(Parcel in) {super(in);boolean[] b = null;in.readBooleanArray(b);if (b != null && b.length > 0) {isStarting = b[0];}step = in.readFloat();}}/** * 开始滚动 */public void startScroll() {isStarting = true;invalidate();}/** * 停止滚动 */public void stopScroll() {isStarting = false;invalidate();}@Overridepublic void onDraw(Canvas canvas) {canvas.drawText(text, temp_view_plus_text_length - x, y, paint);if (!isStarting) {return;}x += 1.0;//文字滚动速度if (x > temp_view_plus_two_text_length) {x = textLength;}invalidate();}}

文字的滚动速度可以自己设定。


喜欢的话可以点击这里进行下载

0 0