android 环绕TextView效果
2015-10-12 20:00
447 查看
效果图:
共有三个TextView组成,上面两个,下面一个TextView。
实现代码:
原理: 首先获取屏幕宽度,减去左边的宽度,绘制一个字检测一次是否到屏幕最大宽度。
共有三个TextView组成,上面两个,下面一个TextView。
实现代码:
public class SurroundTextView { // private TextView leftTextView;//左边的文字,被环绕的文字 private TextView rightTextView;//右边的文字 private TextView bottomTextView;//下边的文字 private int count =0;//总共可以放多少个字 private float textTotalWidth = 0.0f;//textview全部字符的宽度 private float textWidth = 0.0f;//textview一个字的宽度 private Paint paint = new Paint(); private String textString;//需要显示的文字 private float moreWidth = 0;//多于加的宽度 private int height; /** * @param moreWidth 左边留出来的宽度 * @param rightText 右边文字 * @param bottomText 下边文字 * @param text 需要显示的文本 */ public SurroundTextView(float moreWidth,int height, TextView rightText, TextView bottomText, String text) { this.moreWidth = moreWidth; this.rightTextView = rightText; this.bottomTextView = bottomText; this.textString = text; this.height = height; } /** * 绘制文本 * @param screenWidth 屏幕宽度 */ public void drawImageViewDone(int screenWidth) { //获取一个字的宽度 float textWidth = rightTextView.getTextSize(); paint.setTextSize(textWidth); // float width = leftTextView.getTextSize()*leftTextView.getText().toString().length() + moreWidth; // int height = leftTextView.getLineHeight(); // 一行字体的高度 int lineHeight = rightTextView.getLineHeight(); // 可以放多少行 int lineCount = (int) Math.ceil((double) height / (double) lineHeight); // 一行的宽度 float rowWidth = screenWidth - moreWidth - rightTextView.getPaddingLeft() - rightTextView.getPaddingRight(); // 一行可以放多少个字 int columnCount = (int) (rowWidth / textWidth); // 总共字体数等于 行数*每行个数 count = lineCount * columnCount; // 一个TextView中所有字符串的宽度和(字体数*每个字的宽度) textTotalWidth = (float) ((float) count * textWidth); if (textWidth*textString.length() <=screenWidth){ System.out.println("这点东西不够换行啊----》"+textString); rightTextView.setText(textString); bottomTextView.setVisibility(View.GONE); return; } measureText(); rightTextView.setText(textString.substring(0, count)); // 检查行数是否大于设定的行数,如果大于的话,就每次减少一个字符,重新计算行数与设定的一致 while (rightTextView.getLineCount() > lineCount) { count -= 1; rightTextView.setText(textString.substring(0, count)); } bottomTextView.setPadding(0, lineCount * lineHeight - height, 0, 0); System.out.println("下面显示的东西---》"+textString.substring(count)); bottomTextView.setText(textString.substring(count)); } private void measureText(){ String string = textString.substring(0,count); float size = paint.measureText(string); int remainCount = (int)((textTotalWidth-size)/textWidth); if (remainCount>0){ count+=remainCount; measureText(); } } }
原理: 首先获取屏幕宽度,减去左边的宽度,绘制一个字检测一次是否到屏幕最大宽度。
相关文章推荐
- Android四大组件解析
- android 流量统计
- Android 6.0 新增API 简介(3)
- Android中AsyncTask源码解析
- Android Gson解析多层嵌套复杂数据
- Android-ViewPagerIndicator
- android亮度brightness
- Android5.1源码Xposed框架编译
- Android:Web App,Hybrid App,Native App之web:Android和javaScript相互调用
- Android studio 导入V7 支持包
- Android 编程下如何修改 jar 文件的包名
- 苦苦搜寻也没有找到Android的EditText与SeekBar联动例子,吐血分享哈!
- android:Kotlin Java Kotlin android Kotlin
- android:RxJava RxAndroid+Lambda
- Android持续集成--JENKINS+GRADLE+JACOCO+SONARQUBE(一)
- Android 运行时资源替换----Runtime Resource Overlay
- android 中字节流和字符流的区别
- Android系统特有的驱动
- Android 自定义View修炼-自定义弹幕效果View
- android制,点击EditText时刻,隐藏系统软键盘,显示光标