不超过屏幕剧中,超过滚动

public static void setInfoTv(Context context, ScrollTextView info_tv, String infomation) {
   final TextPaint mTextPaint = info_tv.getPaint();
   mTextPaint.setTextSize(info_tv.getTextSize());
   int mTextViewWidth = (int) mTextPaint.measureText(infomation);
   info_tv.setText(infomation);
   if (mTextViewWidth > info_tv.getWidth()) {// 超出一行
      // //info_tv.setText("超出一行");
      info_tv.setPadding(Util.dip2px(context, 13), 0, 0, 0);
      info_tv.setGravity(Gravity.LEFT | Gravity.BOTTOM);
      info_tv.startScroll();
      // info_tv.setRndDuration(10);
   } else { // info_tv.setText("未超出一行");
      info_tv.setPadding(Util.dip2px(context, 0), 0, 0, 0);
      info_tv.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
      info_tv.pauseScroll();
   }
   LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(info_tv.getLayoutParams());
   params.gravity = Gravity.CENTER_VERTICAL;
   info_tv.setLayoutParams(params);

}

package com.transengines.js614m.ces;
import android.annotation.SuppressLint;
import android.widget.Scroller;
import android.widget.TextView;
import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;
import android.graphics.Rect;
import android.text.*;

public class ScrollTextView extends androidx.appcompat.widget.AppCompatTextView {

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

    /*
     * constructor
     */
    public ScrollTextView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.textViewStyle);
    }

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

        // use LinearInterpolator for steady scrolling
        mSlr = new Scroller(this.getContext(), new LinearInterpolator());
        // Do not know why it would not scroll sometimes
        // if setHorizontallyScrolling is called in constructor.
        setHorizontallyScrolling(true);
        setScroller(mSlr);
    }


    /**
     * 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 = -1;
        if (rect.width() > -10 + getWidth())
            scrollingLen = rect.width() + getWidth();
        rect = null;
        return scrollingLen;
    }

    /**
     * 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;

        int scrollingLen = calculateScrollingLen();
        if (scrollingLen > 0) {
            setGravity(3);
            int distance = scrollingLen - (getWidth() + mXPaused);
            int duration = (new Double(mRndDuration * distance * 1.00000
                    / scrollingLen)).intValue();
            // setVisibility(VISIBLE);
            mSlr.startScroll(mXPaused, 0, distance, 0, duration);
            mPaused = false;
        } else {
            reset();
        }
    }

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

        if (!mPaused) {
            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();
        }
    }

    public void reset() {
        setGravity(17);
        this.mXPaused = 0;
        pauseScroll();
        this.mPaused = true;
    }

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

}
// Ô­ÎÄÁ´½Ó£ºhttps://blog.csdn.net/owillll/article/details/8879896


package android_serialport_api.com;

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

public class ScrollTextView extends androidx.appcompat.widget.AppCompatTextView {

   // scrolling feature
   private Scroller mSlr;

   //10s 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);
      init();
   }

   /*
    * constructor
    */
   public ScrollTextView(Context context, AttributeSet attrs) {
      this(context, attrs, android.R.attr.textViewStyle);
      init();
   }

   /*
    * constructor
    */
   public ScrollTextView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      init();
   }

   private void init() {
      // customize the TextView
      setEllipsize(TextUtils.TruncateAt.MARQUEE);
      setMarqueeRepeatLimit(-1);
      setSingleLine();
      setVisibility(VISIBLE);
   }

   /**
    * 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);
      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;
   }
}

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值