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




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值