安卓 文字环绕图片

安卓代码实现

// 文字环绕

boolean imageMeasured = false;
// 屏幕的高度
int screenWidth = 0;
// 总共可以放多少个字
int count = 0;
// textView全部字符的宽度
float textTotalWidth = 0.0f;
// textView一个字的宽度
float textWidth = 0.0f;
Paint paint = new Paint();


private void initMeasure(String text) {
final String te = text;
screenWidth = getWindowManager().getDefaultDisplay().getWidth();


/**
* 获取一个字的宽度
*/
textWidth = questionright.getTextSize();
paint.setTextSize(textWidth);


/**
* 因为图片一开始的时候,高度是测量不出来的,通过增加一个监听器,即可获取其图片的高度和长度
*/
// ViewTreeObserver vto = askImg.getViewTreeObserver();
// vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
// public boolean onPreDraw() {
// if (!imageMeasured) {
// imageMeasured = true;
//
// }
// return imageMeasured;
// }
// });
int height = (int) (askImg.getHeight() * 0.5);
int width = askImg.getWidth();
drawImageViewDone(width, height, te);
}


private void drawImageViewDone(int width, int height, String text) {
// 一行字体的高度
int lineHeight = questionright.getLineHeight();
// 可以放多少行
int lineCount = (int) Math.ceil((double) height / (double) lineHeight);
// 一行的宽度
float rowWidth = screenWidth - width - questionright.getPaddingLeft()
- questionright.getPaddingRight();
// 一行可以放多少个字
int columnCount = (int) (rowWidth / textWidth);


// 总共字体数等于 行数*每行个数
count = lineCount * columnCount;
// 一个TextView中所有字符串的宽度和(字体数*每个字的宽度)
textTotalWidth = (float) ((float) count * textWidth);


if (text.length() <= count) {
questionright.setText(text);
return;
}
measureText(text);
if (text.length() > count) {
questionright.setText(text.substring(0, count));
}


// 检查行数是否大于设定的行数,如果大于的话,就每次减少一个字符,重新计算行数与设定的一致
while (questionright.getLineCount() > lineCount) {
count -= 1;
// questionright.setText(text.substring(0, count));
if (text.length() > count) {
questionright.setText(text.substring(0, count));
} else {
questionright.setText(text);
}
}
if (text.length() > count) {
questionbottom.setPadding(0, lineCount * lineHeight - height, 0, 0);
questionbottom.setText(text.substring(count));
}
}


/**
* 测量已经填充的长度,计算其剩下的长度
*/
private void measureText(String text) {
String string;
if (text.length() > count) {
string = text.substring(0, count);
} else {
string = text;
}
float size = paint.measureText(string);
int remainCount = (int) ((textTotalWidth - size) / textWidth);
if (remainCount > 0) {
count += remainCount;
measureText(text);
}

}



xml布局实现


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >


<ImageView
android:id="@+id/response_askimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/reaponse_item_bg" />


<TextView
android:id="@+id/response_asktextbottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/response_askimg"
android:gravity="fill_horizontal"
android:textColor="#000000"
android:textSize="16sp" />


<TextView
android:id="@+id/response_asktext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_alignBottom="@+id/response_askimg"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/response_askimg"
android:gravity="fill_horizontal"
android:textColor="#000000" />
</RelativeLayout>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值