Android TextView实现可暂停的跑马灯效果

Android TextView 实现可以暂停的跑马灯效果:

content_main.xml

<com.jackie.marqueetextview.MarqueeTextView
        android:id="@+id/tv_marquee"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever" />
MarqueeTextView.java

让MarqueeTextView一直有焦点,就可以一直滚动。

package com.jackie.marqueetextview;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by Jackie on 2015/11/18.
 */
public class MarqueeTextView extends TextView {
    public MarqueeTextView(Context context) {
        super(context);
    }

    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MarqueeTextView(Context context, AttributeSet attrs, int 

defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean isFocused() {
        return true;
    }
}
MainActivity.java

mMarqueeTextView = (MarqueeTextView) findViewById(R.id.tv_marquee);
        mMarqueeTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMarqueeTextView.getEllipsize() != null) {
                    mMarqueeTextView.setEllipsize(null);
                } else {
                    mMarqueeTextView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
                }
            }
        });





你可能感兴趣的:(TextView跑马灯效果)