Android處理Bitmap使其能夠不失真等比縮放裁剪後顯示在ImageView上

來源:互聯網
上載者:User

標籤:

Android開發過程中,我們有時需要動態得顯示一些圖片,並且這些圖片的大小差距會十分大,如果需求並不是需要圖片完整顯示,但是需要不失真,並且要圖片中間部分的情況下,我們需要做一系列處理,因為這個時候ImageView的各種scale type都不適用。具體步驟詳見下面代碼,大家也可以直接拷過去作為工具類使用

 1 /** 2      * 擷取正確縮放裁剪適應IamgeView的Bitmap 3      * @param imageView 4      * @param bitmap 5      * @return 6      */ 7     public static Bitmap createFitBitmap(ImageView imageView, Bitmap bitmap) { 8         Log.i(TAG, "createFitBitmap<---------------------"); 9         int widthTarget = imageView.getWidth();10         int heightTarget = imageView.getHeight();11         int widthBitmap = bitmap.getWidth();12         int heightBitmap = bitmap.getHeight();13         Log.i(TAG, "widthTarget = " + widthTarget );14         Log.i(TAG, "heightTarget = " + heightTarget );15         Log.i(TAG, "widthBitmap = " + widthBitmap );16         Log.i(TAG, "heightBitmap = " + heightBitmap );17         Bitmap result = null;18         if( widthBitmap >= widthTarget && heightBitmap >= heightTarget ){19             result = createLargeToSmallBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);20         }21         else if( widthBitmap >= widthTarget && heightBitmap < heightTarget ){22             Bitmap temp = createLargeWidthToEqualHeightBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);23             result = createLargeToSmallBitmap(temp.getWidth(), temp.getHeight(), widthTarget, heightTarget, temp);24         }25         else if( widthBitmap < widthTarget && heightBitmap >= heightTarget ){26             Bitmap temp = createLargeHeightToEqualWidthBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);27             result = createLargeToSmallBitmap(temp.getWidth(), temp.getHeight(), widthTarget, heightTarget, temp);28         }29         else{30             Bitmap temp = createSmallToEqualBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);31             result = createFitBitmap(imageView, temp);32         }33         Log.i(TAG, "createFitBitmap--------------------->");34         return result;35     }36     37     38     39     private static Bitmap createLargeToSmallBitmap( int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){40         Log.i(TAG, "createLargeToSmallBitmap<---------------------");41         Log.i(TAG, "widthTarget = " + widthTarget );42         Log.i(TAG, "heightTarget = " + heightTarget );43         Log.i(TAG, "widthBitmap = " + widthBitmap );44         Log.i(TAG, "heightBitmap = " + heightBitmap );45         int x = (widthBitmap - widthTarget) / 2;46         int y = (heightBitmap - heightTarget) / 2;47         Log.i(TAG, "createLargeToSmallBitmap--------------------->");48         return Bitmap.createBitmap(bitmap, x, y, widthTarget, heightTarget);49     }50         51     private static Bitmap createLargeWidthToEqualHeightBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){52         53         Log.i(TAG, "createLargeWidthToEqualHeightBitmap<---------------------");54         Log.i(TAG, "widthTarget = " + widthTarget );55         Log.i(TAG, "heightTarget = " + heightTarget );56         Log.i(TAG, "widthBitmap = " + widthBitmap );57         Log.i(TAG, "heightBitmap = " + heightBitmap );58         double scale = ( heightTarget * 1.0 ) / heightBitmap;59         Log.i(TAG, "createLargeWidthToEqualHeightBitmap--------------------->");60         return Bitmap.createScaledBitmap(bitmap, (int)(widthBitmap * scale) , heightTarget, false);61     }62     63     private static Bitmap createLargeHeightToEqualWidthBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){64         65         Log.i(TAG, "createLargeHeightToEqualWidthBitmap<---------------------");66         Log.i(TAG, "widthTarget = " + widthTarget );67         Log.i(TAG, "heightTarget = " + heightTarget );68         Log.i(TAG, "widthBitmap = " + widthBitmap );69         Log.i(TAG, "heightBitmap = " + heightBitmap );70         double scale = ( widthTarget * 1.0 ) / widthBitmap;71         Log.i(TAG, "createLargeHeightToEqualWidthBitmap--------------------->");72         return Bitmap.createScaledBitmap(bitmap, widthTarget , (int)(heightTarget * scale), false);73     }74     75     private static Bitmap createSmallToEqualBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){76         77         Log.i(TAG, "createSmallToEqualBitmap<---------------------");78         Log.i(TAG, "widthTarget = " + widthTarget );79         Log.i(TAG, "heightTarget = " + heightTarget );80         Log.i(TAG, "widthBitmap = " + widthBitmap );81         Log.i(TAG, "heightBitmap = " + heightBitmap );82         double scaleWidth = ( widthTarget * 1.0 ) / widthBitmap;83         double scaleHeight = ( heightTarget * 1.0 ) / heightBitmap;84         double scale = Math.min(scaleWidth, scaleHeight);85         Log.i(TAG, "createSmallToEqualBitmap--------------------->");86         return Bitmap.createScaledBitmap(bitmap, (int)(widthBitmap * scale), (int)(heightBitmap * scale), false);87     }

 

Android處理Bitmap使其能夠不失真等比縮放裁剪後顯示在ImageView上

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.