Java Android创建一个字幕文本

Java Android创建一个字幕文本,java,android,Java,Android,我正在开发一个新闻应用程序,我想在版面底部使用突发新闻横幅 布局如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="bottom"> &

我正在开发一个新闻应用程序,我想在版面底部使用突发新闻横幅

布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"
android:gravity="bottom">  


<com.example.test.ScrollTextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:background="#00ff00"
    android:id="@+id/scrolltext">
    </com.example.test.ScrollTextView>


</LinearLayout>
我正在使用ScrollTextView类:

package com.example.test;

import android.content.Context;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.widget.Scroller;
import android.widget.TextView;

public class ScrollTextView extends TextView {

    // scrolling feature
    private Scroller mSlr;

    // milliseconds for a round of scrolling
    private int mRndDuration = 10000;

    // the X offset when paused
    private int mXPaused = 0;

    // whether it's being paused
    private boolean mPaused = true;

    /*
    * constructor
    */
    public ScrollTextView(Context context) {
    this(context, null);
    // customize the TextView
    setSingleLine();
    setEllipsize(null);
    setVisibility(INVISIBLE);
    }

    /*
    * constructor
    */
    public ScrollTextView(Context context, AttributeSet attrs) {
    this(context, attrs, android.R.attr.textViewStyle);
    // customize the TextView
    setSingleLine();
    setEllipsize(null);
    setVisibility(INVISIBLE);
    }

    /*
    * constructor
    */
    public ScrollTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // customize the TextView
    setSingleLine();
    setEllipsize(null);
    setVisibility(INVISIBLE);
    }

    /**
    * begin to scroll the text from the original position
    */
    public void startScroll() {
    // begin from the very right side
    mXPaused = -1 * getWidth();
    // assume it's paused
    mPaused = true;
    resumeScroll();
    }

    /**
    * resume the scroll from the pausing point
    */
    public void resumeScroll() {

    if (!mPaused)
    return;

    // Do not know why it would not scroll sometimes
    // if setHorizontallyScrolling is called in constructor.
    setHorizontallyScrolling(true);

    // use LinearInterpolator for steady scrolling
    mSlr = new Scroller(this.getContext(), new LinearInterpolator());
    setScroller(mSlr);

    int scrollingLen = calculateScrollingLen();
    int distance = scrollingLen - (getWidth() + mXPaused);
    int duration = (new Double(mRndDuration * distance * 1.00000
    / scrollingLen)).intValue();

    setVisibility(VISIBLE);
    mSlr.startScroll(mXPaused,0, distance, 0, duration);
    invalidate();
    mPaused = false;
    }

    /**
    * calculate the scrolling length of the text in pixel
    *
    * @return the scrolling length in pixels
    */
    private int calculateScrollingLen() {
    TextPaint tp = getPaint();
    Rect rect = new Rect();
    String strTxt = getText().toString();
    tp.getTextBounds(strTxt, 0, strTxt.length(), rect);
    int scrollingLen = rect.width() + getWidth();
    rect = null;
    return scrollingLen;
    }

    /**
    * pause scrolling the text
    */
    public void pauseScroll() {
    if (null == mSlr)
    return;

    if (mPaused)
    return;

    mPaused = true;

    // abortAnimation sets the current X to be the final X,
    // and sets isFinished to be true
    // so current position shall be saved
    mXPaused = mSlr.getCurrX();

    mSlr.abortAnimation();
    }

    @Override
    /*
    * override the computeScroll to restart scrolling when finished so as that
    * the text is scrolled forever
    */
    public void computeScroll() {
    super.computeScroll();

    if (null == mSlr) return;

    if (mSlr.isFinished() && (!mPaused)) {
    this.startScroll();
    }
    }

    public int getRndDuration() {
    return mRndDuration;
    }

    public void setRndDuration(int duration) {
    this.mRndDuration = duration;
    }

    public boolean isPaused() {
    return mPaused;
    }
   }

我想要的是文本将从左向右滚动,与类所做的相反。我尝试更改行mSlr.startScroll中的参数(mxl,0,距离,0,持续时间);但滚动方向保持不变。请提供任何帮助

您尝试过字幕属性吗?这将滚动不适合视图的文本

<TextView
android:layout_width="10dip"
android:layout_height="wrap_content"
android:text="FooBar FooBar FooBar FooBar"
android:singleLine="true"
android:ellipsize="marquee" />

Marquee属性仅适用于聚焦视图,因此请使用此视图而不是android:ellipsize=“Marquee”旁边的textview:


Blundell这不是我想要的,我想要文本从左向右滚动就是这样我试过了,它只显示字符串的开头然后消失了,所以你需要调整开始位置的参数i文本向右移动,但全文不会显示,例如,如果我有以下字符串:“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa。
<TextView
android:layout_width="10dip"
android:layout_height="wrap_content"
android:text="FooBar FooBar FooBar FooBar"
android:singleLine="true"
android:ellipsize="marquee" />
mSlr.startScroll(mXPaused, 0, -distance, 0, duration);
public class TextViewMarquee extends TextView
{

    public TextViewMarquee(Context context, AttributeSet attrs)
    {
        super(context, attrs, R.attr.marqueeStyle);
    }

    public TextViewMarquee(Context context, AttributeSet attrs, int style)
    {
        super(context, attrs, style);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect)
    {
        if(focused) super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    @Override
    public void onWindowFocusChanged(boolean focused)
    {
        if(focused) super.onWindowFocusChanged(focused);
    }

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