您的位置:首页 > 移动开发 > Android开发

android动态调整图片大小

2010-01-27 10:41 459 查看
昨天,动态获取图片资源获取的很爽啊,后来,换了一张png,128*128的,Run as android application,天哪,居然覆盖了我大半个屏幕,都不留一点情面给我展示了。。。。看来,必须要找个方法让图片自适应大小,于是修改了一下获取图片的代码,让图片能自适应:

private  Bitmap getImageFromAssetFile(String fileName,int how){
Bitmap image = null ;
try {
AssetManager  am = game.getAssets();
InputStream is = am.open(fileName);
image = BitmapFactory.decodeStream(is);
is.close();
}catch (Exception e){
}
return  zoomImage(image,how);
}

public Bitmap zoomImage(Bitmap bgimage,int how) {
int bmpwidth = bgimage.getWidth();
int bmpheight = bgimage.getHeight();
float scaleWidth=0;
float scaleHeight=0;
Matrix matrix = new Matrix();
if(how==0){
scaleWidth = ((float) width) / bmpwidth;
scaleHeight = ((float) height) / bmpheight;
}else{
scaleWidth=Math.min(width,height)/bmpwidth;
scaleHeight=Math.min(width, height)/bmpheight;
}


其中,scaleWidth和scaleHeight是欲缩放后的大小,这里加个参数how是防止有不需要缩放的情况~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: