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赋值了,而且服务端也返 ...
随机推荐
- Robot Framework-工具简介及入门使用
Robot Framework-Mac版本安装 Robot Framework-Windows版本安装 Robot Framework-工具简介及入门使用 Robot Framework-Databa ...
- 融云官方cordova示例使用指南
最近公司要在App里加IM功能,用融云IM.于是下载其cordova版示例:https://github.com/rongcloud/cordova-plugin-rongcloud-im-demo ...
- iOS开发零基础--Swift篇:逻辑分支
一. 分支的介绍 分支即if/switch/三目运算符等判断语句 通过分支语句可以控制程序的执行流程 二. if分支语句 和OC中if语句有一定的区别 判断句可以不加() 在Swift的判断句中必须有 ...
- delphi平方开方
用delphi自带的sqr和sqrt函数 procedure TForm2.SpeedButton3Click(Sender: TObject); begin ShowMessage(Sqr(6).T ...
- android firmware 利用UDP socket发送Magic Packet--c语言版本
android firmware 利用UDP socket发送Magic Packet 1 Magic Packet格式: 6个0xFF + 16个Dst Mac Address 2 代码需要设置目的 ...
- PHP会话处理相关函数介绍
PHP会话处理相关函数介绍 提交 我的评论 加载中 已评论 PHP会话处理相关函数介绍 2015-03-23 PHP100中文网 PHP100中文网 PHP100中文网 微信号 功能介绍 互联网开发者 ...
- jqPlot图表插件学习之饼状图和环状图
一.准备工作 官网下载(笔者选择的是jquery.jqplot.1.0.8r1250.zip这个版本) 然后读者需要根据自己的情况新建一个项目并且按照如下的方式加载对应的js和css(因为笔者在VS2 ...
- 跟我一起学WCF(13)——WCF系列总结
引言 WCF是微软为了实现SOA的框架,它是对微乳之前多种分布式技术的继承和扩展,这些技术包括Enterprise Service..NET Remoting.XML Web Service.MSMQ ...
- 深入浅出话VC++(3)——VC++实现绘图操作
VC++实现绘图操作,说白了也就是对API熟练操作了,下面介绍几种绘图 1. 绘制线条 具体实现代码如下: // 鼠标左键按下时的处理函数 void CDrawView::OnLButtonDown( ...
- Memcached分布式缓存初体验
1 Memcached简介/下载/安装 Memcached是一个高性能的不是内存对象缓存系统,用于动态Web应用以减轻数据库负载.Memcached基于一个存储键/值对的HashMap.其客户端可以使 ...