自定义ImageSpan继承类,可以设置图片大小和位置

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.style.ImageSpan; import com.xuehu365.xuehu.utils.LogHelp; /**
* Created by Administrator on 2016/10/20.
*/
public class MyImageSpan extends ImageSpan {
public MyImageSpan(Context arg0, int arg1) {
super(arg0, arg1);
} public int getSize(Paint paint, CharSequence text, int start, int end,
Paint.FontMetricsInt fm) {
Drawable d = getDrawable();
// Drawable d2 = zoomDrawable(d, 50, 50);
Rect rect = d.getBounds();
if (fm != null) {
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;
}
return rect.right;
} @Override
public void draw(Canvas canvas, CharSequence text, int start, int end,
float x, int top, int y, int bottom, Paint paint) {
Drawable b = getDrawable();
canvas.save();
int transY = 0;
transY = ((bottom - top) - b.getBounds().bottom) / 2 + top;
canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
} public Drawable zoomDrawable(Drawable drawable, int w, int h) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap oldbmp = drawableToBitmap(drawable);// drawable转换成bitmap
Matrix matrix = new Matrix(); // 创建操作图片用的Matrix对象
float scaleWidth = ((float) w / width); // 计算缩放比例
LogHelp.i("move", "width:" + width + ";;w:" + w + ";;scaleWidth:" + scaleWidth);
float scaleHeight = ((float) h / height);
matrix.postScale(scaleWidth, scaleHeight); // 设置缩放比例
Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true); // 建立新的bitmap,其内容是对原bitmap的缩放后的图
return new BitmapDrawable(newbmp); // 把bitmap转换成drawable并返回
} public Bitmap drawableToBitmap(Drawable drawable)// drawable 转换成bitmap
{
int width = drawable.getIntrinsicWidth(); // 取drawable的长宽
int height = drawable.getIntrinsicHeight();
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; // 取drawable的颜色格式
Bitmap bitmap = Bitmap.createBitmap(width, height, config); // 建立对应bitmap
Canvas canvas = new Canvas(bitmap); // 建立对应bitmap的画布
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas); // 把drawable内容画到画布中
return bitmap;
}
}

ImageSpan的更多相关文章

  1. EditText添加了ImageSpan后,在两者中间不能输入纯文本

    严格来说是连续插入两个ImageSpan之后,在其中间不能够输入纯文本内容. 最后发现问题出现在了SpannableString在设置ImageSpan的时候第四个参数flag的问题. spannab ...

  2. android平台TextView使用ImageSpan画廊GIF图像

    android-gif-drawable(https://github.com/koral--/android-gif-drawable/releases)开源项目---是一个蛮不错的android ...

  3. 为Textview里面的ImageSpan添加点击响应事件

    对于图文混排的TextView,用户在浏览到里面的图片的时候,往往有点击图片preview大图或者preview之后保存图片的需求,这就需要为Textview里面的ImageSpan设置点击响应事件. ...

  4. ImageSpan图片不能居中的问题

    点击下载:MyImageSpan.txt 使用ImageSpan的童鞋应该都会遇到这样一个困惑,图片不能居中显示,ImageSpan中只有ImageSpan.ALIGN_BASELINE与ImageS ...

  5. Android ImageSpan的使用

    编辑框中加图片 代码如下: mSubjectDetailView = (TextView) findViewById(R.id.subject_detail); CharSequence text = ...

  6. 我的Android进阶之旅------>Android关于ImageSpan和SpannableString的初步了解

    最近要实现一个类似QQ聊天输入框,在输入框中可以同时输入文字和表情图像的功能.如下图所示的效果: 为了实现这个效果,先去了解了一下ImageSpan和SpannableString的用法.下面用一个小 ...

  7. 我的Android进阶之旅------>Android关于ImageSpan和SpannableString的初步了解

    近期要实现一个类似QQ聊天输入框.在输入框中能够同一时候输入文字和表情图像的功能.例如以下图所看到的的效果: 为了实现这个效果.先去了解了一下ImageSpan和SpannableString的使用方 ...

  8. TextView和EditText中添加图片(ImageSpan)

    编辑框中加图片,以前一直以为很复杂,后来发现android有些类已经很好的实现了这些功能. 代码如下: [java] view plaincopy mSubjectDetailView = (Text ...

  9. 自定义可点击的ImageSpan并在TextView中内置“View“

    有的时候可能想在TextView中添加一些图片,比如下图,发短信输入联系人时,要把联系人号码换成一个图片,但这个图片无法用固定的某张图,而是根据内容进行定制的,这更像一个view. 当然,如果你不是v ...

随机推荐

  1. codeforces 556C. Case of Matryoshkas 解题报告

    题目链接:http://codeforces.com/contest/556/problem/C 题目意思:有 n 个数(1,2,...,n)组成 k 条链.第 i 条链由 mi 个数组成.每一秒只可 ...

  2. js计算在线时长

    后台传来毫秒,前台转换成对应的时分秒 //在线时长 function longTime(seconds){ //总秒数 //seconds=seconds/1000; //得到小时 var hour ...

  3. 3.UNION

    --联合结果集union(集合运算符-- group by StudentNo union ---union可以合并多个结果集 --它有两个前提和一个注意: --1.合并的结果集的列数必须完全一致 - ...

  4. You know元音字母吗?

    所谓元音字母,或者母音字母,就是语言里起着发声作用的字母.在英语中,A.E.I.O.U属于元音字母,其中U是半元音开音节和闭音节为数不多的5个元音字母看似简单,他们却能像变戏子一样跟辅音组合拼读成不同 ...

  5. IOS- 快速排序,冒泡排序,直接插入排序和折半插入排序,希尔排序,堆排序,直接选择排序

    /*******************************快速排序 start**********************************///随即取 当前取第一个,首先找到第一个的位置 ...

  6. September 10th 2016 Week 37th Saturday

    An innovation that goes beyond imagination again raised the standard. 颠覆想象的创新,再一次刷新标准. An advertisem ...

  7. 如何激活webstorm 11

    以前使用webstorm 10,可以在网上搜个注册码进行激活.后来升级了webstorm 11,发现原来的注册码和注册机已经不能激活了.查询后,才知道WebStorm 11改变了注册方法,可以在Lic ...

  8. UITableView的scrollToRowAtIndexPath:atScrollPosition:animated的崩溃

      UITableView的scrollToRowAtIndexPath:atScrollPosition:animated的崩溃 [摘要:reason: '-[UITableView _conten ...

  9. grep -v 排除多人字符串

    # egrep -v '^$|^#' /etc/httpd/conf/httpd.conf # grep -v '^$\|^#' /etc/httpd/conf/httpd.conf

  10. SQL学习笔记----更改SQL默认的端口号

    1.SQLServer配置管理器----SQLServer网络配置----MSSQLSERVER的协议---TCP/IP(已启用)---IP地址 清空素有的IP,在IPALL下更改默认的端口: 2. ...