最近需要实现跑马灯效果,安卓自带的效果必须获得焦点才能有动画,坑爹~~~

尝试了网上诸多方法,总算下面这个方法可以~~~

==============================================================================================================

有时候在xml中写的跑马灯效果不滚动:原因有以下

 

Android系统中TextView实现跑马灯效果,必须具备以下几个条件: 1、android:ellipsize=”marquee” 2、TextView必须单行显示,即内容必须超出TextView大小 3、TextView要获得焦点才能滚动( 如果还不行,就要用自定义的TextView控件中重写isFocused()返回true就行【方法代码在下面的AlwaysMarqueeTextView 类】,但是遇到新问题就是界面有多个这样的控件显示时当弹出Dialog后这些自定义的TextView控件就会失去焦点,跑马灯效果又变成“...”的形式了,当Dialog消失之后又恢复正常) XML代码: android:ellipsize="marquee", android:singleLine="true" Java代码: mTVText.setText("哼唱接撒砥砺风节雷锋精神http://orgcent.com/,很长很长很长很长很长很长的数据"); mTVText.setSingleLine(true); mTVText.setEllipsize(TruncateAt.MARQUEE); PS: TextView.setHorizontallyScrolling(true); //让文字可以水平滑动 TextView还可以设置跑马灯效果的滚动次数,如下: XML代码设置: android:marqueerepeatlimit="1"。1代表1次,-1代表无限循环。 Java代码设置: mTVText.setMarqueeRepeatLimit(-1);  

 

 

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

 

 

第一种:

控件类: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 CREATOR
                = new Parcelable.Creator() {
           
            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中使用方法:

    

    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">

   

 

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
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”
/>

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


来源:http://www.cnblogs.com/hsbd-zyb/p/5849964.html

更多相关文章

  1. android中实现tab标签切换时的一些效果
  2. Android Fragment 生命周期及回调方法
  3. 部分 CM11 系统 Android 平板执行植物大战僵尸 2 黑屏的解决的方
  4. Android 在Service开启对话框的方法
  5. Android dialog回弹效果
  6. android tab选项卡效果
  7. Android React Native在Android Studio中执行bundleReleaseJsAnd
  8. Android圆角按钮的制作方法
  9. greendao3.2.0使用方法(超详细)

随机推荐

  1. Python进阶:自定义对象实现切片功能
  2. Python之父重回决策层,未来如何发展?
  3. 聊聊技术写作的个人体会
  4. Python进阶:迭代器与迭代器切片
  5. 华熙LIVE以创新模式 打造城市活力聚集地
  6. [译]PEP 380--子生成器的语法
  7. Python骚操作:动态定义函数
  8. 没有食材,数据分析师如何做饭?
  9. 深度辨析 Python 的 eval() 与 exec()
  10. 条码读取控件Dynamsoft Barcode Reader v