图文混排
来源:互联网 发布:打鸟汽枪650淘宝 编辑:程序博客网 时间:2024/05/16 06:17
package com.example.administrator.customimagespan;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Rect;import android.graphics.drawable.Drawable;import android.text.style.ImageSpan;import android.util.Log;/** * 自定义imageSpan实现图片与文字的居中对齐 * * 参考 http://www.cnblogs.com/withwind318/p/5541267.html * * http://mp.weixin.qq.com/s/2TTc-KucG6nH2upHqiZeOw * * http://mp.weixin.qq.com/s/2TTc-KucG6nH2upHqiZeOw * * top , ascent,descent bottom leading 是以基线为参照物,我的理解是相当于基线位横坐标轴线,上面位负数,下面为正,所以正常情况 * ascent top 值位负数 bottom descent 为正数 * */class CustomImageSpan extends ImageSpan { //自定义对齐方式--与文字中间线对齐 private int ALIGN_FONTCENTER = 2; public CustomImageSpan(Context context, int resourceId) { super(context, resourceId); } public CustomImageSpan(Context context, int resourceId, int verticalAlignment) { super(context, resourceId, verticalAlignment); } @Override public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { //draw 方法是重写的ImageSpan父类 DynamicDrawableSpan中的方法,在DynamicDrawableSpan类中,虽有getCachedDrawable(), // 但是私有的,不能被调用,所以调用ImageSpan中的getrawable()方法,该方法中 会根据传入的drawable ID ,获取该id对应的 // drawable的流对象,并最终获取drawable对象 Drawable drawable = getDrawable(); //调用imageSpan中的方法获取drawable对象 canvas.save(); //获取画笔的文字绘制时的具体测量数据 Paint.FontMetricsInt fm = paint.getFontMetricsInt(); //系统原有方法,默认是Bottom模式) int transY = bottom - drawable.getBounds().bottom; if (mVerticalAlignment == ALIGN_BASELINE) { transY -= fm.descent; } else if (mVerticalAlignment == ALIGN_FONTCENTER) { //此处加入判断, 如果是自定义的居中对齐 //与文字的中间线对齐(这种方式不论是否设置行间距都能保障文字的中间线和图片的中间线是对齐的) // y+ascent得到文字内容的顶部坐标,y+descent得到文字的底部坐标,(顶部坐标+底部坐标)/2=文字内容中间线坐标 transY = ((y + fm.descent) + (y + fm.ascent)) / 2 - drawable.getBounds().bottom / 2; } canvas.translate(x, transY); drawable.draw(canvas); canvas.restore(); } /** * 重写getSize方法,只有重写该方法后,才能保证不论是图片大于文字还是文字大于图片,都能实现中间对齐 */ public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) { Drawable d = getDrawable(); Rect rect = d.getBounds(); if (fm != null) { Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt(); int fontHeight = fmPaint.bottom - fmPaint.top; int drHeight = rect.bottom - rect.top; int top = drHeight / 2 - fontHeight / 4; int bottom = drHeight / 2 + fontHeight / 4; fm.ascent = -bottom; fm.top = -bottom; fm.bottom = top; fm.descent = top; Log.d("test",fm.ascent+" "+ fm.top+" "+ fm.bottom+" "+ fm.descent); } return rect.right; }}
阅读全文
0 0
- 图文混排
- 图文混排
- 图文混排
- CSS图文混排
- 图文混排TLFTextField
- 图文混排
- TTStyledTextLabel 图文混排
- FloatImageText图文混排
- 图文混排
- coreText图文混排
- Button 图文混排
- 图文混排
- TextView 图文混排
- qt 图文混排
- CT 图文混排
- android图文混排
- coretext 图文混排
- CoreText图文混排
- mysql表添加字段和注释
- ArrayList源码解析
- virtualenv command
- 正确在WPF中对Windows窗体背景进行设置
- Android 使用Picasso加载网络图片等比例缩放
- 图文混排
- 关于使用sklearn进行数据预处理 —— 归一化/标准化/正则化
- iOS学习笔记之xcode工具查看内存泄漏
- iOS开发——openssl生成CSR (pkcs10) 请求证书
- 2.线性表—顺序表
- 在jsp页面中设置<c:foreach>中序号
- SVN Cornerstone 使用详解
- C++字符串格式化 sprintf、printf
- JVM垃圾回收小结