您的位置:首页 > 移动开发 > Android开发

Android 文字自动滚动(跑马灯)效果的两种实现方法

2014-12-15 17:09 791 查看

总结一下跑马灯的实现效果,网上比较流行的有两种,测试过了都可以实现文字滚动效果,建议使用第一种,因为可以更好地控制文字滚动速度、样式、字体等属性,第二种方法,还没有找到控制的方法!

第一种:
控件类:AutoScrollTextView 继承了TextView并做了一些修改,实现了宽度的判断,文本自动滚动及开始和停止滚动等功能。

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.view.Display;

import android.view.View;

import android.view.WindowManager;

import android.view.View.OnClickListener;

import android.widget.TextView;

public class AutoScrollTextView extends TextView implements OnClickListener {

public final static String TAG = AutoScrollTextView.class.getSimpleName();

private float textLength = 0f;//文本长度

private float viewWidth = 0f;

private float step = 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 AutoScrollTextView(Context context) {

super(context);

initView();

}
public AutoScrollTextView(Context context, AttributeSet attrs) {

super(context, attrs);

initView();

}
public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

initView();

}

private void initView()

{

setOnClickListener(this);

}

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

}

}

step = textLength;

temp_view_plus_text_length = viewWidth + textLength;

temp_view_plus_two_text_length = viewWidth + textLength * 2;

y = getTextSize() + getPaddingTop();

}

@Override

public Parcelable onSaveInstanceState()

{

Parcelable superState = super.onSaveInstanceState();

SavedState ss = new SavedState(superState);

ss.step = step;

ss.isStarting = isStarting;

return ss;

}

@Override

public void onRestoreInstanceState(Parcelable state)

{

if (!(state instanceof SavedState)) {

super.onRestoreInstanceState(state);

return;

}

SavedState ss = (SavedState)state;

super.onRestoreInstanceState(ss.getSuperState());

step = 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);

}
@Override

public 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];

}
@Override

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

}

@Override

public void onDraw(Canvas canvas) {

canvas.drawText(text, temp_view_plus_text_length - step, y, paint);

if(!isStarting)

{

return;

}

step += 0.5;//0.5为文字滚动速度。

if(step > temp_view_plus_two_text_length)

step = textLength;

invalidate();
}
@Override

public void onClick(View v) {

if(isStarting)

stopScroll();

else

startScroll();

}
}

xml中使用方法:
<cn.tigertian.ui.AutoScrollTextView
android:id="@+id/TextViewNotice"

android:layout_height="30px"
android:layout_width="fill_parent"

android:text="@string/test_notice_1"
android:textColor="#000"
android:inputType="text"

android:background="#EEE"
android:textSize="20px">
</cn.tigertian.ui.AutoScrollTextView>

Activity中使用方法:
//启动公告滚动条

autoScrollTextView = (AutoScrollTextView)findViewById(R.id.TextViewNotice);

autoScrollTextView.init(getWindowManager());

autoScrollTextView.startScroll();
注:如果想改变跑马灯的文字内容或者文字效果,则在调用完setText方法之后,需要再调用一下init方法,重新进行初始化和相关参数的计算。

第二种:

TextView实现文字滚动需要以下几个要点:

1.文字长度长于可显示范围:android:singleLine="true"

2.设置可滚到,或显示样式:android:ellipsize="marquee"

3.TextView只有在获取焦点后才会滚动显示隐藏文字,因此需要在包中新建一个类,继承TextView。重写isFocused方法,这个方法默认行为是,如果TextView获得焦点,方法返回true,失去焦点则返回false。跑马灯效果估计也是用这个方法判断是否获得焦点,所以把它的返回值始终设置为true。

以下转自他人:

Java语言:AlwaysMarqueeTextView 类

publicclassAlwaysMarqueeTextViewextendsTextView{
publicAlwaysMarqueeTextView(Contextcontext){

super(context);

}
publicAlwaysMarqueeTextView(Contextcontext,AttributeSetattrs){

super(context,attrs);

}
publicAlwaysMarqueeTextView(Contextcontext,AttributeSetattrs,intdefStyle){

super(context,attrs,defStyle);

}
@Override

publicbooleanisFocused(){

returntrue;

}

在布局XML文件中加入这么一个AlwaysMarqueeTextView,这个加入方法也是刚刚学的。

XML语言:layout.xml

<com.examples.AlwaysMarqueeTextView

android:id=“@+id/AMTV1″

android:layout_width=“fill_parent”

android:layout_height=“wrap_content”

android:lines=“1″

android:focusable=“true”

android:focusableInTouchMode=“true”

android:scrollHorizontally=“true”

android:marqueeRepeatLimit=“marquee_forever”

android:ellipsize=“marquee”

android:background=“@android:color/transparent”

/>
ellipsize属性

设置当文字过长时,该控件该如何显示。有如下值设置:”start”—–省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动)
marqueeRepeatLimit属性

在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。
focusable属性

自己猜测的,应该是能否获得焦点,同样focusableInTouchMode应该是滑动时能否获得焦点。
组合View的问题:

XML语言:组合View

< LinearLayout

xmlns:android =“http://schemas.android.com/apk/res/android”

android:orientation =“vertical”

android:gravity =“center_vertical”

android:background =“@drawable/f_background”

android:layout_width =“fill_parent”

android:focusable =“true”

android:layout_height =“50px”>

< TextView

android:id =“@+id/info_text”

android:focusable =“true”

android:layout_width =“fill_parent”

android:layout_height =“wrap_content”

android:text =“test marquee .. “

android:textColor =“@color/black”

android:singleLine =“true”

android:ellipsize =“marquee”

android:marqueeRepeatLimit =“3″

android:textSize =“18sp”

/>

< TextView

android:id =“@+id/date_text”

android:layout_width =“fill_parent”

android:layout_height =“wrap_content”

android:layout_gravity =“bottom”

android:textColor =“@color/gray”

android:text =“2010/05/28″

android:textSize =“12sp”

/>

</ LinearLayout >
上面示例中2个TextView组合为一个View,由于设置了LinearLayout为focusable而TextView就没法取得焦点了,这样 这个TextView的跑马灯效果就显示不出来,就算你也设置TextView的
android:focusable=
"true"
也是
没用的. 这个时候就要使用addStatesFromChildren 这个属性了,在LinearLayout中设置这个属性,
然后设置TextView的focusable=
"true"
就可以了.关于
addStatesFromChildren的说明:

Sets whether
this
ViewGroup's
drawable states
also include its children's drawable
states.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: