Android图片大小调整的相关代码示例:
view plaincopy to clipboardprint? 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; } 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是防止有不需要缩放的情况。
上一篇:Android中如何实现垃圾回收
下一篇:Android中如何设置屏幕大小