ImageSpan
自定义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的更多相关文章
- EditText添加了ImageSpan后,在两者中间不能输入纯文本
严格来说是连续插入两个ImageSpan之后,在其中间不能够输入纯文本内容. 最后发现问题出现在了SpannableString在设置ImageSpan的时候第四个参数flag的问题. spannab ...
- android平台TextView使用ImageSpan画廊GIF图像
android-gif-drawable(https://github.com/koral--/android-gif-drawable/releases)开源项目---是一个蛮不错的android ...
- 为Textview里面的ImageSpan添加点击响应事件
对于图文混排的TextView,用户在浏览到里面的图片的时候,往往有点击图片preview大图或者preview之后保存图片的需求,这就需要为Textview里面的ImageSpan设置点击响应事件. ...
- ImageSpan图片不能居中的问题
点击下载:MyImageSpan.txt 使用ImageSpan的童鞋应该都会遇到这样一个困惑,图片不能居中显示,ImageSpan中只有ImageSpan.ALIGN_BASELINE与ImageS ...
- Android ImageSpan的使用
编辑框中加图片 代码如下: mSubjectDetailView = (TextView) findViewById(R.id.subject_detail); CharSequence text = ...
- 我的Android进阶之旅------>Android关于ImageSpan和SpannableString的初步了解
最近要实现一个类似QQ聊天输入框,在输入框中可以同时输入文字和表情图像的功能.如下图所示的效果: 为了实现这个效果,先去了解了一下ImageSpan和SpannableString的用法.下面用一个小 ...
- 我的Android进阶之旅------>Android关于ImageSpan和SpannableString的初步了解
近期要实现一个类似QQ聊天输入框.在输入框中能够同一时候输入文字和表情图像的功能.例如以下图所看到的的效果: 为了实现这个效果.先去了解了一下ImageSpan和SpannableString的使用方 ...
- TextView和EditText中添加图片(ImageSpan)
编辑框中加图片,以前一直以为很复杂,后来发现android有些类已经很好的实现了这些功能. 代码如下: [java] view plaincopy mSubjectDetailView = (Text ...
- 自定义可点击的ImageSpan并在TextView中内置“View“
有的时候可能想在TextView中添加一些图片,比如下图,发短信输入联系人时,要把联系人号码换成一个图片,但这个图片无法用固定的某张图,而是根据内容进行定制的,这更像一个view. 当然,如果你不是v ...
随机推荐
- storm单机环境部署
前面说过storm集群的部署,这篇主要介绍storm单机环境部署,其实他们之间很类似,就是将之前配置文件中所有的集群条目改成本机的地址即可,部署之前应该按前面solr和zookeeper单机环境部署那 ...
- DELPHI XE5开发WEB服务器及安卓手机客户端
Xe5开发web服务端和手机客户端 ------------------------------------- Delphi xe5作为最新开发利器,就类似如当年的DELPHI,功能强大,快发速度快, ...
- java web 学习 --第五天(Java三级考试)
第四天的学习内容:http://www.cnblogs.com/tobecrazy/p/3454860.html Response对象 response对象主要是向客户端浏览器发送二进制数据,如输出C ...
- Java for LeetCode 223 Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...
- monitor disk
#!/bin/bash # #top #Big_USERS - find big disk space users in various directories ################### ...
- html中的alt和title用法区别
html中的alt和title用法区别 首先明确一下概念,alt是html标签的属性,而title既是html标签,又是html属性.title标签这个不用多说,网页的标题就是写在<title& ...
- 【leetcode】Word Search (middle)
今天开始,回溯法强化阶段. Given a 2D board and a word, find if the word exists in the grid. The word can be cons ...
- 【leetcode】Linked List Cycle II (middle)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- LeetCode 453 Minimum Moves to Equal Array Elements
Problem: Given a non-empty integer array of size n, find the minimum number of moves required to mak ...
- yii验证码不使用model在控制器中进行验证
控制器 public function actionCheckLogin(){ if(!$this->createAction('verify_code')->validate($_POS ...