code:android-code:image-convert
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| code:android-code:image-convert [2011/11/17 20:31] – percy | code:android-code:image-convert [2016/05/05 13:07] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 531: | Line 531: | ||
| </ | </ | ||
| + | |||
| + | ====== 图像等比例伸缩 ====== | ||
| + | <code java> | ||
| + | public static Bitmap createFixedBitmapWithImageView(ImageView imageView, Bitmap bitmap) { | ||
| + | float bitmapHeight = bitmap.getHeight(); | ||
| + | float bitmapWidth = bitmap.getWidth(); | ||
| + | |||
| + | float imageViewWidth = imageView.getWidth(); | ||
| + | float imageViewHeight = imageView.getHeight(); | ||
| + | |||
| + | float imageViewRate = (float) imageViewWidth / (float) imageViewHeight; | ||
| + | float bitmapRate = (float) bitmapWidth / (float) bitmapHeight; | ||
| + | float newWidth = 0; | ||
| + | float newHeight = 0; | ||
| + | if (bitmapHeight > imageViewHeight && bitmapWidth > imageViewWidth) { | ||
| + | if (bitmapRate > imageViewRate) { | ||
| + | newWidth = imageViewWidth; | ||
| + | newHeight = (imageViewWidth / bitmapWidth) * imageViewHeight; | ||
| + | } else { | ||
| + | newHeight = imageViewHeight; | ||
| + | newWidth = (imageViewHeight / bitmapHeight) * bitmapWidth; | ||
| + | } | ||
| + | } else if (bitmapHeight >= imageViewHeight && bitmapWidth <= imageViewWidth) { | ||
| + | if (bitmapRate > imageViewRate) { | ||
| + | newWidth = imageViewWidth; | ||
| + | newHeight = (imageViewWidth / bitmapWidth) * imageViewHeight; | ||
| + | } else { | ||
| + | newHeight = imageViewHeight; | ||
| + | newWidth = imageViewHeight / bitmapHeight * bitmapWidth; | ||
| + | } | ||
| + | } else if (bitmapHeight < imageViewHeight && bitmapWidth > imageViewWidth) { | ||
| + | if (bitmapRate > imageViewRate) { | ||
| + | newWidth = imageViewWidth; | ||
| + | newHeight = (imageViewWidth / bitmapWidth) * imageViewHeight; | ||
| + | } else { | ||
| + | newHeight = imageViewHeight; | ||
| + | newWidth = (imageViewHeight / bitmapHeight) * bitmapWidth; | ||
| + | } | ||
| + | } else { | ||
| + | if (bitmapRate > imageViewRate) { | ||
| + | newWidth = imageViewWidth; | ||
| + | newHeight = (imageViewWidth) / bitmapWidth * imageViewHeight; | ||
| + | } else { | ||
| + | newHeight = imageViewHeight; | ||
| + | newWidth = (imageViewHeight / bitmapHeight) * bitmapWidth; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | Bitmap resizedBitmap = resizeImage(bitmap, | ||
| + | |||
| + | return resizedBitmap; | ||
| + | } | ||
| + | </ | ||
| + | |||
/var/www/dokuwiki/wiki/data/attic/code/android-code/image-convert.1321533088.txt.gz · Last modified: 2016/05/05 13:06 (external edit)
