由于项目中需要用到图文混排技术,在此稍微研究了两天,出来一个效果还算不错的东西
布局中的RichTextView是另外封装的一个实现Span的TextView,就是实现效果图中表情啊,@啊,#话题# 之类的,了解Span的都懂的,就不细说了。读者研究这个图文混排的时候,可以直接用TextView替代。
RichTextImageView
public void setText(String t) {
mTopText.setText("");
mBottomText.setText("");
mTopText.setVisibility(View.INVISIBLE);
mBottomText.setVisibility(View.GONE);
bRequestBottomLayout = true;
if (t == null)
t = "";
// Log.e("setText", t);
mTextContent = t;
mTopText.setText(mTextContent);
requestLayout();
}
public void setImage(int id) {
mImageContent = id;
mPreviewImage.setImageResource(id);
mPreview.setVisibility(View.VISIBLE);
if (!Util.isStringEmpty(mTextContent))
setText(mTextContent);
}
private void iniViews() {
mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
mTopText = (RichTextView) findViewById(R.id.lefttext);
mBottomText = (RichTextView) findViewById(R.id.bottomtext);
if(IMAGE_LOCATION == IMAGE_LOCATION_RIGHT){
mPreview = (RelativeLayout) findViewById(R.id.layout_preimage_isgif_right);
mPreviewImage = (ImageView) findViewById(R.id.preimage_statues_right);
mPreviewImageIsGif = (ImageView) findViewById(R.id.preimage_isgif_right);
}else if(IMAGE_LOCATION == IMAGE_LOCATION_LEFT){
mPreview = (RelativeLayout) findViewById(R.id.layout_preimage_isgif_left);
mPreviewImage = (ImageView) findViewById(R.id.preimage_statues_left);
mPreviewImageIsGif = (ImageView) findViewById(R.id.preimage_isgif_left);
}
}
public void setImageLocation(int l){
if(l != IMAGE_LOCATION_RIGHT && l != IMAGE_LOCATION_LEFT)
return;
else{
IMAGE_LOCATION = l;
mPreview.setVisibility(View.GONE);
iniViews();
setImage(mImageContent);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
super.onLayout(changed, l, t, r, b);
if (Util.isStringEmpty(mTextContent))
return;
// Log.e("top text 宽 高",
// mTopText.getWidth() + " " + mTopText.getHeight());
// Log.e("top text 单行高度", "" + mTopText.getLineHeight());
// Rect rect = getStringRect(mTextContent, mTopText.getPaint());
// Log.e("文本的宽 高", rect.width() + " " + rect.height());
// toptext最多能显示的行数
int topTextMaxRows = mTopText.getHeight() / mTopText.getLineHeight();
// 显示这些内容真实占用的行数
int textRows = mTopText.getLineCount();
// Log.e("top text最多能显示的行数", "" + topTextMaxRows);
// Log.e("当前文本实际占用的行数", "" + textRows);
//由于文本可能带有表情,重新计算显示行数,以保证显示正确
Rect lineR = new Rect();
int realH = 0; //toptext真实高度
int i = 0;
for(i = 0; i < topTextMaxRows; i++){
try{
mTopText.getLineBounds(i, lineR);
}catch(IndexOutOfBoundsException e){
break;
}
realH += lineR.height();
if(realH >= mTopText.getHeight())
break;
}
// Log.e("当前view实际能显示的行数为", "" + i);
topTextMaxRows = i;
//如果toptext显示不下的话,显示到bottomtext里面去
if (textRows >= topTextMaxRows && bRequestBottomLayout) {
// toptext最后一个可见字符的位置
int lastindex = mTopText.getLayout().getLineVisibleEnd(
topTextMaxRows - 1);
ClickableSpan[] cs = mTopText.getSpans();
int spanstart = 0;
int spanend = 0;
spanstring = "";
STATE = 0; // 1网页 2人名 3话题
for (ClickableSpan c : cs) {
spanstart = mTopText.getSpanStart(c);
spanend = mTopText.getSpanEnd(c);
if (spanstart <= lastindex && spanend > lastindex) {
if (c instanceof LinkClickableSpan) {
// Log.e("转角span类型", "网页");
spanstring = ((LinkClickableSpan) c).getLink();
STATE = 1;
}
if (c instanceof NameClickableSpan) {
// Log.e("转角span类型", "人名");
spanstring = ((NameClickableSpan) c).getName();
STATE = 2;
}
if (c instanceof TopicClickableSpan) {
// Log.e("转角span类型", "话题");
spanstring = ((TopicClickableSpan) c).getTopic();
STATE = 3;
}
break;
}
}
mTopText.setText(mTextContent.substring(0, lastindex));
mTopText.setVisibility(View.VISIBLE);
// 当行数不是整数时,调整内容会有下面半行没遮住,所以干脆都处理完以后再显示出来
mBottomText.setText(mTextContent.substring(lastindex,
mTextContent.length()) + mBottomText.getText().toString());
if (mBottomText.getText().length() > 0 && bRequestBottomLayout){
mBottomText.setVisibility(View.VISIBLE);
mHandler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
mBottomText.requestLayout();
bRequestBottomLayout = false;
}
});
}
if (STATE != 0 || !bRequestBottomLayout) {
Log.e("spanstring", spanstring);
// Log.e("", "移除转角span");
mTopText.getSpannable().removeSpan(mTopText.getSpans()[mTopText.getSpans().length - 1]);
switch (STATE) {
case 1:
mTopText.getSpannable().setSpan(
mTopText.new LinkClickableSpan(spanstring),
spanstart,
lastindex,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mBottomText.getSpannable().setSpan(
mBottomText.new LinkClickableSpan(spanstring),
0,
spanend - lastindex,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Log.e("", "网页");
break;
case 2:
mTopText.getSpannable().setSpan(
mTopText.new NameClickableSpan(spanstring),
spanstart,
lastindex,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mBottomText.getSpannable().setSpan(
mBottomText.new NameClickableSpan(spanstring),
0,
spanend - lastindex,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Log.e("", "人名");
break;
case 3:
mTopText.getSpannable().setSpan(
mTopText.new TopicClickableSpan(spanstring),
spanstart,
lastindex,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mBottomText.getSpannable().setSpan(
mBottomText.new TopicClickableSpan(spanstring),
0,
spanend - lastindex,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Log.e("", "话题");
break;
}
mTopText.setText(mTopText.getSpannable(), BufferType.SPANNABLE);
mBottomText.setText(mBottomText.getSpannable(), BufferType.SPANNABLE);
}
}
}
![[]Android <wbr>文字环绕 <wbr>图文混排 <wbr>支持Span折叠 []Android <wbr></p>文字环绕 <wbr>图文混排 <wbr>支持Span折叠](upload/attach/wp/2015/06/2015060814054739.gif)
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2313
- 用户1336
- 访客11756567
每日一句
Life is short; Live it!
人生苦短,活出精彩。
人生苦短,活出精彩。
信鸽推送报错NSObject checkTargetOtherLinkFlagForObjc
简单利用Clover四叶草安装U盘安装黑苹果
学习使用Java注解
OllyDbg中如何找出B模块中所有调用了A模块的C方法的地方
解决SSH客户端中文乱码
10年后,Android应用程序仍然比iOS应用程序差
C++11特性里面的thread
XPosed微信自动生成二维码
解决android studio "found an invalid color"的问题
T9社区注册方法【勼适様鲃女尔懟死】
Thinkpad x1 Extreme黑苹果10.14.5安装完成
基于大白主题增加图片本地化的功能
Linux系统查看CPU使用率的几个命令
新会员