Android圖片大小調(diào)整動(dòng)態(tài)實(shí)現(xiàn)方法
Android操作系統(tǒng)中對(duì)于圖片的操作我們在以前的文章中也有所介紹。不過對(duì)于圖片的大小調(diào)整往往都局限于固定的調(diào)整。如何才能滿足動(dòng)態(tài)大小調(diào)整呢?我們?cè)谶@里就為大家詳細(xì)介紹有關(guān)Android圖片大小調(diào)整的動(dòng)態(tài)實(shí)現(xiàn)方法。
昨天,動(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是防止有不需要縮放的情況~
Android圖片大小調(diào)整的操作方法就為大家介紹到這里。
【編輯推薦】