天天看點

android_文本垂直滾動

這個自定義view主要實作的是垂直文本自動滾動,當文本高度超出垂直高度時自動滾動。也可以修改成其他條件觸發滾動。參考了網上一篇文章,找不到出處了。

package com.serviatech.mediaplayer.view;

import com.serviatech.mediaplayer.utils.Logcat;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 裡面有一個textview,當textview文字高度小于1080時,設定textview為1080高度,如果大于就開始滾動
 */
public class AutoScrollView extends ScrollView {

  private final Handler handler = new Handler();
  private long duration = 50;
  private boolean isScrolled = false;
  private int currentIndex = 0;
  private long period = 1000;
  private int currentY = -1;

  private float mTextSize;
  private int mTextColor;
  private Typeface mTypeface;
  private String string;
  private Context mContext;

  public AutoScrollView(Context context) {
    this(context, null);
    init(context);
  }

  public AutoScrollView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
    init(context);
  }

  public AutoScrollView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
  }

  public void init(Context context) {
    this.mContext = context;

    mTextSize = 50;
    mTextColor = Color.RED;
    mTypeface = Typeface.DEFAULT;
  }

  // /

  public void setTextSize(float textSize) {
    this.mTextSize = textSize;
    caculate();
  }

  public void setTextColor(int textColor) {
    this.mTextColor = textColor;
  }

  public void setTypeface(Typeface typeface) {
    this.mTypeface = typeface;
    caculate();
  }

  public void setText(String string) {
    this.string = string;
    caculate();
  }

  public void setBackgroundColor(int color) {
    setBackgroundColor(color);
  }

  // /
  /**
   * 計算是否需要滾動
   * 
   * @data 2016-5-27 上午9:21:51
   */
  public void caculate() {

    stopScroll();
    final TextView textView = new TextView(mContext);
    textView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
        LayoutParams.WRAP_CONTENT));
    textView.setTextSize(mTextSize);
    textView.setTextColor(mTextColor);
    textView.setTypeface(mTypeface);
    textView.setText(string);
    textView.setGravity(Gravity.CENTER);
    removeAllViews();
    addView(textView);

    new Thread() {

      @Override
      public void run() {
        super.run();

        try {
          sleep(200);// 必須要休眠一段時間
          Logcat.d("@@@",
              "textView.getHeight(): " + textView.getHeight()
                  + ",getHeight(): " + getHeight());
          if (textView.getHeight() > getHeight()) {
            autoScroll();
          } else {
            stopScroll();
          }
        } catch (Exception e) {
        }
      }
    }.start();
  }

  private void stopScroll() {
    isScrolled = false;
  }

  private void autoScroll() {

    isScrolled = true;
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        boolean flag = isScrolled;
        if (flag) {
          // Log.d("test", "currentY = " + currentY
          // + "  getScrollY() = " + getScrollY());
          if (currentY == getScrollY()) {
            try {
              Thread.sleep(period);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            currentIndex = 0;
            scrollTo(0, 0);
            handler.postDelayed(this, period);
          } else {
            currentY = getScrollY();
            handler.postDelayed(this, duration);
            currentIndex++;
            scrollTo(0, currentIndex * 1);
          }
        } else {
          // currentIndex = 0;
          // scrollTo(0, 0);
        }
      }
    }, duration);
  }
  // 

}      

使用

<com.serviatech.mediaplayer.view.AutoScrollView
        android:id="@+id/tv_emergen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:background="#fff"
        android:fillViewport="true"
        android:gravity="center"
        android:text="遇到緊急情況!!!"
        android:textColor="#f00"
        android:textSize="50sp"
        android:visibility="gone" />