溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android中怎么動(dòng)態(tài)調(diào)整圖片大小

發(fā)布時(shí)間:2021-06-26 15:00:07 來源:億速云 閱讀:126 作者:Leah 欄目:移動(dòng)開發(fā)

Android中怎么動(dòng)態(tài)調(diào)整圖片大小,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

昨天,動(dòng)態(tài)獲取圖片資源獲取的很爽啊,后來,換了一張png,128*128的,Run as android application,天哪,居然覆蓋了我大半個(gè)屏幕,都不留一點(diǎn)情面給我展示了。。。??磥?,必須要找個(gè)方法讓圖片自適應(yīng)大小,于是修改了一下獲取圖片的代碼,讓圖片能自適應(yīng)。

一下就是Android圖片大小調(diào)整的相關(guān)代碼示例:

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是欲縮放后的大小,這里加個(gè)參數(shù)how是防止有不需要縮放的情況~

看完上述內(nèi)容,你們掌握Android中怎么動(dòng)態(tài)調(diào)整圖片大小的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI