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

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

Java代码是:

package com.example.test;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.*;
import java.util.Map;
public class Newstest extends Activity
{
TextView label1;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.news);
ScrollTextView scrolltext=(ScrollTextView) findViewById(R.id.scrolltext);
scrolltext.setText("dddddddddddddddddddddddddddddddddd sajd asjd lksdddddddddddddddddddddddddddddddddd sajd asjd lksdddddddddddddddddddddddddddddddddd sajd asjd lksdddddddddddddddddddddddddddddddddd sajd asjd lks");
scrolltext.setTextColor(Color.BLACK);
scrolltext.startScroll();
}
}

我使用的是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行中的参数(mXPaused,0,distance,0,duration);但是滚动方向保持不变。如有任何帮助,请

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

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

http://developer.android.com/reference/android/widget/TextView.html#attr_android:ellipsize


通过查看API文档:http://developer.android.com/reference/android/widget/Scroller.html#startScroll(int,int,int)

似乎你可以通过负数向相反的方向滚动,所以试试:

mSlr.startScroll(mXPaused, 0, -distance, 0, duration);

Marquee属性仅适用于聚焦视图,因此使用此视图而不是android:ellipsize="marquee":旁边的文本视图

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;
    }
}

最新更新