1
我在SD卡上有3張圖片(1 jpg和2 gif)。我想減少它們以節省內存。調整JPG大小調整大小後,但GIF圖像具有原始大小。見代碼和日誌:如何在Android中調整GIF圖像大小?
public class BitmapHelper
{
private static int calculateInSampleSize(BitmapFactory.Options options, int imgHeigth, int imgWidth)
{
int inSampleSize = 1;
if(imgHeigth > 80 || imgWidth > 120)
{
final int heightRatio = Math.round((float) imgHeigth/80);
final int widthRatio = Math.round((float) imgWidth/120);
if(heightRatio < widthRatio)
return heightRatio;
if(heightRatio > widthRatio)
return widthRatio;
if(heightRatio == widthRatio)
return heightRatio;
}
return inSampleSize;
}
public Bitmap decodeBitmapFromFile(String imageUrl, Resources res)
{
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imageUrl, options);
if(options.outMimeType != null)
{
options.inSampleSize = calculateInSampleSize(options, options.outHeight, options.outWidth);
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeFile(imageUrl, options);
Log.d("Debugger", "Height: " + options.outHeight + "; Width: " + options.outWidth);
return bmp;
}
else
{
options.inSampleSize = 2;
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, R.drawable.play, options);
}
}
}
登錄:
Height: 97; Width: 175
Height: 324; Width: 550
Height: 97; Width: 175
Height: 226; Width: 400
Height: 97; Width: 175
Height: 324; Width: 550
Height: 226; Width: 400
97x175 - JPEG - (Original size: 388x698)
324x550 - GIF - original size
226x400 - GIF - original size
錯了呢?提前感謝很多。
你有沒有得到這個整理出來?我遇到同樣的問題,無法調整GIF的大小。 – mraviator 2013-03-26 18:49:04