代码如下: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生成的图片,如何能实现放大和缩小的功能呢?

解决方案 »

  1.   

    参考 /* 图片缩小的method */  
        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);   
                }   
            }    
           
    }