Android圖片大小調整動態實現方法
Android操作系統中對于圖片的操作我們在以前的文章中也有所介紹。不過對于圖片的大小調整往往都局限于固定的調整。如何才能滿足動態大小調整呢?我們在這里就為大家詳細介紹有關Android圖片大小調整的動態實現方法。
昨天,動態獲取圖片資源獲取的很爽啊,后來,換了一張png,128*128的,Run as android application,天哪,居然覆蓋了我大半個屏幕,都不留一點情面給我展示了。。。。看來,必須要找個方法讓圖片自適應大小,于是修改了一下獲取圖片的代碼,讓圖片能自適應。
一下就是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圖片大小調整的操作方法就為大家介紹到這里。
【編輯推薦】