滚动消息栏

 来看看要做成的效果

实现文字滚动显示

首先把界面做出来,界面比较简单,一个image,一个textview就实现了。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:gravity="center_vertical"
    android:padding="10dp"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="14dp"
        android:layout_height="14dp"
        android:src="@mipmap/ic_bulletin"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="6dp"
        android:text="暂无中奖公告,快来参加吧~"
        android:textColor="#999999"
        android:textSize="13sp" />

</LinearLayout>

重点来实现文字滚动,要实现文字滚动可以使用viewflipper,自定义控件来替换TextView.文字滚动用动画控制,直接上代码

public class TextViewFlipper extends ViewFlipper {
    /** 间隔播放时间 */
    private static final long HANDLER_WHAT_SHOW_NEXT_INTERVAL = 5 * 1000;

    private static final int HANDLER_WHAT_SHOW_NEXT = 1001;

    private TextView tv1, tv2;

    private MyHandler myHandler;
    private List<CharSequence> texts;
    private int curShowPos;

    public TextViewFlipper(Context context) {
        this(context, null);
    }

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

    private void init(Context context) {
        myHandler = new MyHandler(this);

        tv1 = new TextView(context);
        tv1.setSingleLine();
        tv1.setEllipsize(TextUtils.TruncateAt.END);
        tv1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
        tv1.setTextColor(0xFF999999);

        tv2 = new TextView(context);
        tv2.setSingleLine();
        tv2.setEllipsize(TextUtils.TruncateAt.END);
        tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
        tv2.setTextColor(0xFF999999);

        TranslateAnimation inAnim = new TranslateAnimation(0, 0, 0, 0,
                Animation.RELATIVE_TO_SELF, 1,
                Animation.RELATIVE_TO_SELF, 0);
        inAnim.setDuration(300);
        inAnim.setInterpolator(new AccelerateInterpolator());
        inAnim.setFillAfter(true);

        TranslateAnimation outAnim = new TranslateAnimation(0, 0, 0, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, -1);
        outAnim.setDuration(300);
        inAnim.setInterpolator(new AccelerateInterpolator());
        outAnim.setFillAfter(true);

        setInAnimation(inAnim);
        setOutAnimation(outAnim);
    }

    public void start(List<CharSequence> texts) {
        if (null == texts || texts.isEmpty()) {
            stop(true);
            return ;
        }

        this.texts = texts;

        // 初始化
        myHandler.removeMessages(HANDLER_WHAT_SHOW_NEXT);
        stopFlipping();
        removeAllViews();

        addView(tv1);
        addView(tv2);

        curShowPos = 0;
        tv1.setText(texts.get(0));

        myHandler.sendEmptyMessageDelayed(HANDLER_WHAT_SHOW_NEXT, HANDLER_WHAT_SHOW_NEXT_INTERVAL);
    }

    private void showNextText() {
        int oldShowPos = curShowPos;

        if (curShowPos + 1 == texts.size()) {
            curShowPos = 0;
        } else {
            curShowPos ++;
        }

        CharSequence curShowText = texts.get(curShowPos);
        CharSequence oldShowText = texts.get(oldShowPos);
        if (curShowPos % 2 == 0) {
            tv1.setText(curShowText);
            tv2.setText(oldShowText);
            super.showNext();
        } else {
            tv2.setText(curShowText);
            tv1.setText(oldShowText);
            super.showPrevious();
        }

        myHandler.sendEmptyMessageDelayed(HANDLER_WHAT_SHOW_NEXT, HANDLER_WHAT_SHOW_NEXT_INTERVAL);
    }

    /**
     * 停止播放
     */
    public void stop() {
        stop(false);
    }

    /**
     * 停止播放
     * @param isHideView 是否隐藏View
     */
    public void stop(boolean isHideView) {
        myHandler.removeMessages(HANDLER_WHAT_SHOW_NEXT);
        if (isHideView) {
            setVisibility(GONE);
        }
    }

    /**
     * 获取当前显示位置
     * @return
     */
    public int getCurrentShowPosition() {
        return curShowPos;
    }

    private static class MyHandler extends Handler {
        private WeakReference<TextViewFlipper> wrf;

        public MyHandler(TextViewFlipper textViewFlipper) {
            this.wrf = new WeakReference<>(textViewFlipper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            TextViewFlipper curTextViewFlipper = wrf.get();
            if (null == curTextViewFlipper) return ;

            switch (msg.what) {
                case HANDLER_WHAT_SHOW_NEXT:
                    // 显示下一个
                    curTextViewFlipper.showNextText();
                    break;
            }

        }
    }

}

  

取得数据,调用控件start传入数据,就可显示

原文地址:https://www.cnblogs.com/zyandroid/p/5018929.html