Android圖片大小調(diào)整動態(tài)實現(xiàn)方法
Android操作系統(tǒng)中對于圖片的操作我們在以前的文章中也有所介紹。不過對于圖片的大小調(diào)整往往都局限于固定的調(diào)整。如何才能滿足動態(tài)大小調(diào)整呢?我們在這里就為大家詳細介紹有關(guān)Android圖片大小調(diào)整的動態(tài)實現(xiàn)方法。
昨天,動態(tài)獲取圖片資源獲取的很爽啊,后來,換了一張png,128*128的,Run as android application,天哪,居然覆蓋了我大半個屏幕,都不留一點情面給我展示了。。。??磥?,必須要找個方法讓圖片自適應(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是欲縮放后的大小,這里加個參數(shù)how是防止有不需要縮放的情況~
Android圖片大小調(diào)整的操作方法就為大家介紹到這里。
【編輯推薦】