android 计算textView文本的高度

小笔记,记录下

因为要做一个由下向上滚动的文本,懒得自定义View了,直接使用anim来做,animal无限循环就可以了,这里就要计算滚动的长度

代码如下

    private void bottomToUpAnimal(float textHeight) {
        tv_text_show.setText(textShow);
        int top = (int) (0 - textHeight - 1);
        int bottom = viewHeight;
        int trandLength = bottom + viewHeight;
        int duration = trandLength / speed;
        MyLog.cdl("=======文字的高度==" + duration + " /bottom=" + bottom + " /top=" + top);
        animation = new TranslateAnimation(0, 0, bottom, top);
        animation.setDuration(duration * 1000);
        animation.setRepeatCount(-1);
        tv_text_show.setAnimation(animation);
        animation.setInterpolator(new LinearInterpolator());
        animation.start();
    }

滚动的长度就是控件的高度+文字的高度,文字的高度,网上用仁兄这样写的,貌似没什么效果,

   int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        tv_text_show.measure(w, h);
        int height = tv_text_show.getMeasuredHeight();
        int width = tv_text_show.getMeasuredWidth();
        bottomToUpAnimal(height);

 

这边我是通过先获取textView的行数,然后通过字号计算每行的高度,然后相乘就可以了,计算需要等待加载完成才能操作,所以使用了Runnable里面来计算,代码比较简单,做个笔记吧。

 

        tv_text_show.post(new Runnable() {
            @Override
            public void run() {
                int numheight = tv_text_show.getLineCount();
                float textHeight = (float) ((textSize + 0.00000007) / 0.7535);
//                Width = size(宽度和字体字号是相同的, 12sp字体, 单字宽为12dp)
                textHeight = numheight * textHeight;
                bottomToUpAnimal(textHeight);
            }
        });

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值