TextView设置圆角,内容太多可以滚动

public class MainActivity extends Activity {
private ScrollTextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv = (ScrollTextView) findViewById(R.id.tv);

tv.setText("明月几时有?把酒问青天。不知天上宫阙,今夕是何年。\n"  
            +"我欲乘风归去,又恐琼楼玉宇,高处不胜寒。\n"  
            +"起舞弄清影,何似在人间。\n"  
            +"转朱阁,低绮户,照无眠。不应有恨,何事长向别时圆?\n"  
            +"人有悲欢离合,月有阴晴圆缺,此事古难全。\n"  
            +"但愿人长久,千里共婵娟。明月几时有?把酒问青天。不知天上宫阙,今夕是何年。\n"  
            +"我欲乘风归去,又恐琼楼玉宇,高处不胜寒。\n"  
            +"起舞弄清影,何似在人间。\n"  
            +"转朱阁,低绮户,照无眠。不应有恨,何事长向别时圆?\n"  
            +"人有悲欢离合,月有阴晴圆缺,此事古难全。\n"  
            +"但愿人长久,千里共婵娟。明月几时有?把酒问青天。不知天上宫阙,今夕是何年。\n"  
            +"我欲乘风归去,又恐琼楼玉宇,高处不胜寒。\n"  
            +"起舞弄清影,何似在人间。\n"  
            +"转朱阁,低绮户,照无眠。不应有恨,何事长向别时圆?\n"  
            +"人有悲欢离合,月有阴晴圆缺,此事古难全。\n"  
            +"但愿人长久,千里共婵娟。明月几时有?把酒问青天。不知天上宫阙,今夕是何年。\n"  
            +"我欲乘风归去,又恐琼楼玉宇,高处不胜寒。\n"  
            +"起舞弄清影,何似在人间。\n"  
            +"转朱阁,低绮户,照无眠。不应有恨,何事长向别时圆?\n"  
            +"人有悲欢离合,月有阴晴圆缺,此事古难全。\n"  
            +"但愿人长久,千里共婵娟。");

// 
}

}


ScrollTextView类:

public class ScrollTextView extends TextView {


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

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

@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
startScroll();
}


private void startScroll() {
initScroll();
scrollTo(0, 0);
mHandler.removeCallbacks(mRunnable);
mHandler.postDelayed(mRunnable, 7000);//首行滚动延时
}

Handler mHandler = new Handler();
Runnable mRunnable = new Runnable() {
        public void run() {  
            int height = getHeight();  
            int scrollY = getScrollY();  
            int lineHeight = getLineHeight();  
            int lineCount = getLineCount();
            double viewCount = Math.floor(height / lineHeight);//可见区域最大显示的行数
            
            //能显示得下就无需滚动
            if (lineCount <= viewCount) {
            return;
            }
                       
            //最大需要滚动的长度
            int maxScrollY = lineCount * lineHeight;//等于文字总长                
            
            if (scrollY >= maxScrollY) {  //滚动头时则复位
            scrollTo(0, 0);
                mHandler.postDelayed(this, 1000); //回滚后延时再滚动
            } else {  
                scrollBy(0, 1);
                mHandler.postDelayed(this, 50);
            }                  
  
        } 
    };

private void initScroll() {
if (mHandler == null) {
mHandler = new Handler();
}

if (mRunnable == null) {
mRunnable = new Runnable() {
       public void run() {
           int textAreaheight = getHeight() - getPaddingBottom() - getPaddingTop();  
           int scrollY = getScrollY();  
           int lineHeight = getLineHeight();  
           int lineCount = getLineCount();
           double viewCount = Math.floor(textAreaheight / lineHeight);//可见区域最大显示的行数
           
//         Log.d("ScrollTextView", "in run, viewCount=" + viewCount + " lineCount=" + lineCount + " height=" + textAreaheight + " lineHeight=" + lineHeight + " scrollY=" + scrollY);
           //能显示得下就无需滚动
           if (lineCount <= viewCount) {
            return;
           }
                       
           //最大需要滚动的长度
           int maxScrollY = lineCount * lineHeight;//等于文字总长                
           
           if (scrollY >= maxScrollY) {  //滚动头时则复位
            scrollTo(0, 0);
               mHandler.postDelayed(this, 3000);//回滚后延时再滚动
           } else {  
               scrollBy(0, 1);
               mHandler.postDelayed(this, 50);
           }                 
 
       } 
   };
}
}

}


布局文件 activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <com.example.demo.ScrollTextView
        android:id="@+id/tv"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@drawable/left_btn"
        />
</LinearLayout>


在drawable文件夹下建立XML:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#00000000"/>
    <stroke android:width="2dip" android:color="#ff000000" />
    <corners android:radius="15px" /> 
</shape>
<!-- solid设置填充颜色,颜色值以#80开头表示透明
stroke 设置边框宽度,颜色值
corners设置圆角 -->


效果图:


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值