代码如下:spanned = Html.fromHtml(builder.toString(),
new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
byte[] phoByte = null;
Drawable drawable = null;
Bitmap phoMap = null,newMap = null;
try {
phoByte = fileService
.downloadFileFroHttpSvr(source);
phoMap = BitmapFactory
.decodeByteArray(phoByte, 0,
phoByte.length);
//设置图片大小
Matrix matrix = new Matrix();
matrix.postScale((float)150/phoMap.getWidth(),(float)150/phoMap.getHeight());
newMap = Bitmap.createBitmap(phoMap, 0, 0,
phoMap.getWidth(), phoMap.getHeight(), matrix, true);
drawable = new ZoomableDrawable(newMap);
drawable.setBounds(0, 0,
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
} catch (Exception e) {
Log.e(tag,
null == e.getMessage()
|| e.getMessage().isEmpty() ? "error"
: e.getMessage());
}finally{
if(null != phoMap && phoMap.isRecycled())phoMap.recycle();
if(null != newMap && newMap.isRecycled())newMap.recycle();
}
return drawable;
}
}, null);
}
etxMassage.append(spanned);
请问当点击通过Html.fromhtml生成的图片,如何能实现放大和缩小的功能呢?
new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
byte[] phoByte = null;
Drawable drawable = null;
Bitmap phoMap = null,newMap = null;
try {
phoByte = fileService
.downloadFileFroHttpSvr(source);
phoMap = BitmapFactory
.decodeByteArray(phoByte, 0,
phoByte.length);
//设置图片大小
Matrix matrix = new Matrix();
matrix.postScale((float)150/phoMap.getWidth(),(float)150/phoMap.getHeight());
newMap = Bitmap.createBitmap(phoMap, 0, 0,
phoMap.getWidth(), phoMap.getHeight(), matrix, true);
drawable = new ZoomableDrawable(newMap);
drawable.setBounds(0, 0,
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
} catch (Exception e) {
Log.e(tag,
null == e.getMessage()
|| e.getMessage().isEmpty() ? "error"
: e.getMessage());
}finally{
if(null != phoMap && phoMap.isRecycled())phoMap.recycle();
if(null != newMap && newMap.isRecycled())newMap.recycle();
}
return drawable;
}
}, null);
}
etxMassage.append(spanned);
请问当点击通过Html.fromhtml生成的图片,如何能实现放大和缩小的功能呢?
解决方案 »
- 问个基础问题!
- 下载中点击暂停没反应
- tabhost总是抛java.lang.IllegalStateException
- 详解Activity生命周期
- ExpandableListView与ScrollView
- 学习了Rexsee的移动Web应用,不知Web开发是否会成为移动应用的趋势?
- android sqlite,如何用代码创建2个数据库
- 为什么accessibilit不能使用?
- 急!开发的安卓程序在三星的Galaxy Tab3 P5210上打开提示已停止!在爱国者平板上可以使用!
- Eclipse配置SDK问题,新人求指导
- android 求一个简单的屏保源码
- android实时传输视频流到流媒体服务器
private void small() {
int bmpWidth=bmp.getWidth();
int bmpHeight=bmp.getHeight();
Log.i(TAG, "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);
/* 设置图片缩小的比例 */
double scale=0.8;
/* 计算出这次要缩小的比例 */
scaleWidth=(float) (scaleWidth*scale);
scaleHeight=(float) (scaleHeight*scale);
/* 产生reSize后的Bitmap对象 */
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,
bmpHeight,matrix,true);
if(id==0) {
/* 如果是第一次按,就删除原来默认的ImageView */
layoutImage.removeView(mImageView);
} else {
/* 如果不是第一次按,就删除上次放大缩小所产生的ImageView */
layoutImage.removeView((ImageView)findViewById(id));
}
/* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */
id++;
ImageView imageView = new ImageView(this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layoutImage.addView(imageView);
Log.i(TAG, "imageView.getWidth() = " + imageView.getWidth()
+ ", imageView.getHeight() = " + imageView.getHeight());
setContentView(layout1);
/* 因为图片放到最大时放大按钮会disable,所以在缩小时把它重设为enable */
mButton02.setEnabled(true);
mButton02.setTextColor(Color.MAGENTA);
}
/* 图片放大的method */
private void big() {
int bmpWidth=bmp.getWidth();
int bmpHeight=bmp.getHeight();
Log.i(TAG, "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);
/* 设置图片放大的比例 */
double scale=1.25;
/* 计算这次要放大的比例 */
scaleWidth=(float)(scaleWidth*scale);
scaleHeight=(float)(scaleHeight*scale);
/* 产生reSize后的Bitmap对象 */
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,
bmpHeight,matrix,true);
if(id==0) {
/* 如果是第一次按,就删除原来设置的ImageView */
layoutImage.removeView(mImageView);
} else {
/* 如果不是第一次按,就删除上次放大缩小所产生的ImageView */
layoutImage.removeView((ImageView)findViewById(id));
}
/* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */
id++;
ImageView imageView = new ImageView(this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layoutImage.addView(imageView);
setContentView(layout1);
/* 如果再放大会超过屏幕大小,就把Button disable */
if( scaleWidth * scale * bmpWidth > bmpWidth * 3 ||
scaleHeight * scale * bmpHeight > bmpWidth * 3 ||
scaleWidth * scale * bmpWidth > displayWidth * 5 ||
scaleHeight * scale * bmpHeight > displayHeight * 5) {
mButton02.setEnabled(false);
mButton02.setTextColor(Color.GRAY);
} else {
mButton02.setEnabled(true);
mButton02.setTextColor(Color.MAGENTA);
}
}
}