android 计算textView文本的高度
16lz
2021-01-25
小笔记,记录下
因为要做一个由下向上滚动的文本,懒得自定义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); } });
更多相关文章
- Android(安卓)的Activity.getWindowManager().getDefaultDisplay
- Android中为edittext预设文字(使用hint)
- RecyclerView高度随Item自适应 GridLayoutManager和LinearLayout
- android ImageView 图片宽度全屏,高度自适应的写法
- Android(安卓)UI滑动菜单之SlidingMenu
- Android(安卓)点击文字实现跳转
- android 自动换行
- 让TextView的drawable与文字一起居中
- Android实现多个跑马灯效果,多个文本框TextView的跑马灯