小笔记,记录下

因为要做一个由下向上滚动的文本,懒得自定义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);            }        });

 

更多相关文章

  1. Android(安卓)的Activity.getWindowManager().getDefaultDisplay
  2. Android中为edittext预设文字(使用hint)
  3. RecyclerView高度随Item自适应 GridLayoutManager和LinearLayout
  4. android ImageView 图片宽度全屏,高度自适应的写法
  5. Android(安卓)UI滑动菜单之SlidingMenu
  6. Android(安卓)点击文字实现跳转
  7. android 自动换行
  8. 让TextView的drawable与文字一起居中
  9. Android实现多个跑马灯效果,多个文本框TextView的跑马灯

随机推荐

  1. Android 他人面试题总结
  2. android 城市列表
  3. Android中的开机和关机广播
  4. android运行后台服务de生命周期yu使用技
  5. Android(安卓)O上获取Adaptive Icon的Bit
  6. eclipse无法导入Android工程的解决办法
  7. android service 学习(上)
  8. 利用drozer进行Android渗透测试
  9. (转)Android(安卓)TextView背景色、圆角
  10. android_camera