Snippet: align a TextView around an image
A few weeks ago I discovered the Spans on Android,after reading the wonderful post by Flavien Laurent.
In this post I will describe how to realize a particular layout not very common on Android: a text around an image.
This layout is not an Android Pattern, but it can be useful in same cases.
As always it is just an example, and you should improve some points in your real project.
Use a simple layout:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TextView> <ImageView
android:id="@+id/icon"
android:src="@drawable/rectangle"
android:layout_width="150dp"
android:layout_height="150dp"></ImageView> </RelativeLayout> </ScrollView>
To achieve our scope, we can use a LeadingMarginSpan.LeadingMarginSpan2.
This span allows the implementor to specify the number of lines of text to which this object is attached that the "first line of paragraph" margin width will be applied to.
/**
*
*/
class MyLeadingMarginSpan2 implements LeadingMarginSpan.LeadingMarginSpan2 { private int margin;
private int lines; MyLeadingMarginSpan2(int lines, int margin) {
this.margin = margin;
this.lines = lines;
} /**
* Apply the margin
*
* @param first
* @return
*/
@Override
public int getLeadingMargin(boolean first) {
if (first) {
return margin;
} else {
return 0;
}
} @Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir,
int top, int baseline, int bottom, CharSequence text,
int start, int end, boolean first, Layout layout) {} @Override
public int getLeadingMarginLineCount() {
return lines;
}
};
We only need to calculate the number of lines where we would like applying a margin and the right margin.
In this case we will get number of lines = height of image and margin = width of image + little extra margin.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.text);
mImageView = (ImageView) findViewById(R.id.icon);
final ViewTreeObserver vto = mImageView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mImageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
finalHeight = mImageView.getMeasuredHeight();
finalWidth = mImageView.getMeasuredWidth();
makeSpan();
}
});
}
}
This code can be improved.
I am using a very simple (and raw) float textLineHeight = mTextView.getPaint().getTextSize(); to calculate the number of lines.
You can add paddings, margins or you can use a Rect to calculate the text bounds.
/**
* This method builds the text layout
*/
private void makeSpan() { /**
* Get the text
*/
String plainText=getResources().getString(R.string.text_sample); int allTextStart = 0;
int allTextEnd = htmlText.length() - 1; /**
* Calculate the lines number = image height.
* You can improve it... it is just an example
*/
int lines;
Rect bounds = new Rect();
mTextView.getPaint().getTextBounds(plainText.substring(0,10), 0, 1, bounds); //float textLineHeight = mTextView.getPaint().getTextSize();
float fontSpacing=mTextView.getPaint().getFontSpacing();
lines = (int) (finalHeight/fontSpacing); /**
* Build the layout with LeadingMarginSpan2
*/
MyLeadingMarginSpan2 span = new MyLeadingMarginSpan2(lines, finalWidth +10 );
mSpannableString.setSpan(span, allTextStart, allTextEnd,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); mTextView.setText(mSpannableString);
}

You can get code from GitHub:
Snippet: align a TextView around an image的更多相关文章
- textview 显示html方法解析
现在网络的繁盛时代,光文字是不能满足人们的胃口的,图片,flash,音频,视频就成为浏览网页的主流显示,在手机上也一样.在手机上显示从网络端获取的数据显示,大家很自然的想起两种方式,一种就是webvi ...
- Android之TextView文字绘制流程
一:TextView的onDraw()方法: 1.第一句restartMarqueeIfNeeded()绘制字幕滚动. protected void onDraw(Canvas canvas) { r ...
- TextView里的文 html
一.[Android实例]实现TextView里的文字有不同颜色 转eoe:http://www.eoeandroid.com/thread-4496-1-1.html import android. ...
- 自定义控件 TextView 歌词 Lrc
演示 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> < ...
- Android的TextView使用Html来处理图片显示、字体样式、超链接等
一.[Android实例]实现TextView里的文字有不同颜色 转eoe:http://www.eoeandroid.com/thread-4496-1-1.html import android. ...
- Android 获取View的高度或TextView的行数, 实现自适应的textview
大家都遇到过项目中需要获控件的的高度或者列如文章开头说TextView的行数 但是很多人在实际操作中getLineCount()获取到值是零,其实只是我们没在正确的位置获取. 这是因为activtiy ...
- 【转载】TextView源码解析
原文地址:https://github.com/7heaven/AndroidSdkSourceAnalysis/blob/master/article/textview%E6%BA%90%E7%A2 ...
- 关于textview显示特殊符号居中的问题
话说这是2017年的第一篇博客,也是一篇技术博客.先从简单的一篇解决问题开始吧,千里之行,始于足下! ------------------------------------------------- ...
- 奇葩问题-TextView无法获取值
问题场景 前几天写一个界面的时候,遇到一个非常奇葩的问题.app第一次安装的时候,这里针对用户第一次安装的时候,后来是不会出现这个问题了.我明明是对某个界面的一个textview赋值了,而且服务端也返 ...
随机推荐
- “”?: H3C SSH 配置+
开启ssh 服务 ssh service enable 创建用户 使用ssh local-user ssh 用户级别 authorization-attribute user-role level-1 ...
- [原] XAF 如何将数据库中Byte array图片显示出来
问题比较简单,直接上代码. private Image _Cover; [Size(SizeAttribute.Unlimited), ValueConverter(typeof(ImageValue ...
- hadoop 突然断电数据丢失问题
HDFS-Could not obtain block MapReduce Total cumulative CPU time: 33 seconds 380 msec Ended Job = j ...
- 降龙十八掌之一:(亢龙有悔)SQL Server Profiler和数据库引擎优化顾问
简介 说到Sql的[性能工具]真是强大,SQL Server Profiler的中文意思是SQL Server事件探查,这个到底是做什么用的呢?我们都知道探查的意思大多是和监视有关,其实这个SQL S ...
- SRS文档 软件需求说明书
[摘要] 随着信息时代科技的飞速发展,经济全球化已广为人知,英语作为全球最主要的语言之一,受到越来越多的人的喜爱,不仅为了增长知识,也为了能适应社会发展的需求.但是,学英语最重要的事首先是积累词汇,没 ...
- JavaScript自定义事件
很多DOM对象都有原生的事件支持,向div就有click.mouseover等事件,事件机制可以为类的设计带来很大的灵活性,相信.net程序员深有体会.随着web技术发展,使用JavaScript自定 ...
- [读书笔记]C#学习笔记三: C#类型详解..
前言 这次分享的主要内容有五个, 分别是值类型和引用类型, 装箱与拆箱,常量与变量,运算符重载,static字段和static构造函数. 后期的分享会针对于C#2.0 3.0 4.0 等新特性进行. ...
- 分页sql语句优化
MySQL的limit工作原理就是先读取n条记录,然后抛弃前n条,读m条想要的,所以n越大,性能会越差. 一般的分页做法,测试耗时 10.961s SELECT * FROM v_history_ ...
- Bruce Eckel:编程生涯(转载)
Bruce Eckel:编程生涯(转载) 说明:Bruce Eckel 著有大名鼎鼎的<Thinking in C++>和<Thinking in Java>.本文是他对程序员 ...
- InnoSetup能够实现“安装细节描述”界面吗?
QUOTE( Example_Test.iss ) // 脚本使用了 增强版脚本编辑器 build 091218:Beta2// 编译器版本为 5.3.6.ee1 [Setup]AppName=My ...