首页 > 其他分享 >android_文本垂直滚动

android_文本垂直滚动

时间:2022-11-08 10:33:53浏览次数:41  
标签:滚动 void private context android 文本 public textView



这个自定义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" />





标签:滚动,void,private,context,android,文本,public,textView
From: https://blog.51cto.com/u_13316945/5832010

相关文章

  • android-code-saveLog
    一个工具类。第一个用来将logcat保存到文件,第二个用来获得logcat字符串。publicclassLogcat{publicfinalstaticStringTAG="VLC/Util/Logcat";/***Wr......
  • android-code-调整音量
    这是不弹出界面的方式:/***@paramvalue*0-15*/publicvoiddealCMDSound(intvalue){AudioManagermAudioManager=(AudioManager)getSy......
  • el-data-picker不能随滚动条滚动问题
    el-data-picker不能随滚动条滚动问题解决方法:主要跟父容器设置overflow属性有关。详细解决过程描述:有问题的页面元素上下包裹层级是:template>div>el-container->el-ma......
  • 新建文本文档
    正则表达式普通字符【】应用常用字符(87条消息)VScode使用正则表达式快速删除注释_spicyChickenJZ-CSDN博客_vscode删除注释里面的\s\S刚好一正一反,就能够匹......
  • contentEditable 富文本,中文输入法触发多次oninput事件解决办法
    1. contentEditable富文本,没有onchange事件,有oninput事件,但是遇见中文输入法时,会触发多次,而我们只想输入结束时触发。那我们需要用到compositionstart和compositionend事......
  • Android 接收微信、QQ其他应用打开,第三方分享
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助在AndroidManifest.xml注册ACTION事件<activityandroid:name="com.test.app.MainActivity"......
  • 修改滚动条样式
    <style>::-webkit-scrollbar{/*滚动条整体样式*/width:10px;/*高宽分别对应横竖滚动条的尺寸*/height:10px;}::-webk......
  • vue表格滚动vue-seamless-scroll
    引入  npminstallvue-seamless-scroll--save  在找到main.js引入importscrollfrom'vue-seamless-scroll'Vue.use(scroll)主要代码<template>......
  • Android的Service作用和使用方法
    首先Service是干嘛的就是你Activity,finish之后你创建的Service还不会死,注意关闭软件这里是finish就是返回操作,不是清理后台,这时候你可以让用户干别的,你的软件依然可以......
  • android-创建无界面的服务运行在后台
    android-创建无界面的服务运行在后台RT,目标是新建一个服务运行在后台,但不能有界面具体步骤如下:1.新建一个无界面的Activity新建一个无界面的Activity,但是不给它s......