如何在Android中调整GIF图像的大小?
我有3张SD卡上的图像(1张jpg和2张gif)。我想减少它们以节省内存。调整大小后,JPG成功调整大小,但GIF图像具有原始大小。请参阅代码和日志:如何在Android中调整GIF图像的大小?,android,bitmap,resize,Android,Bitmap,Resize,我有3张SD卡上的图像(1张jpg和2张gif)。我想减少它们以节省内存。调整大小后,JPG成功调整大小,但GIF图像具有原始大小。请参阅代码和日志: public class BitmapHelper { private static int calculateInSampleSize(BitmapFactory.Options options, int imgHeigth, int imgWidth) { int inSampleSize = 1;
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);
}
}
}
怎么了?提前感谢您。请尝试以下代码重新调整大小:
Display display = getWindow().getWindowManager().getDefaultDisplay();
int hsize= (int) ((int)display.getHeight()-(display.getHeight()*(0.50)));
int wsize= (int) ((int)display.getWidth()-(display.getWidth()*0.25));
Bitmap originalImage=decodeSampledBitmapFromResource(getResources(),imageId,wsize,hsize);
ImageView imageView = new ImageView(mContext);
imageView.setImageBitmap(scaleBitmap(originalImage));
imageView.setLayoutParams(new CoverFlow.LayoutParams(wsize,hsize));
imageView.setScaleType(ScaleType.MATRIX);
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
{
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth)
{
if (width > height)
{
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
private Bitmap scaleBitmap(Bitmap bitmap)
{
int iWidth=bitmap.getWidth() ;
int iHeight=bitmap.getHeight();
int newWidth=0,newHeight=0;
float fFactor=(float)iHeight/(float)iWidth;
if(iWidth<=iHeight)
{
newWidth = 300;
newHeight =(int) (newWidth * fFactor) ;//370;
}
else
{
newWidth = 400;
newHeight =(int) (newWidth * fFactor) ;//370;
}
float fScaleWidth = ((float) newWidth) / iWidth;
float fScaleHeight = ((float) newHeight) / iHeight;
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(fScaleWidth, fScaleHeight);
Bitmap resizedBitmappreview = Bitmap.createBitmap(bitmap, 0, 0,iWidth, iHeight, matrix, true);
return resizedBitmappreview;
}
Display Display=getWindow().getWindowManager().getDefaultDisplay();
inthsize=(int)((int)display.getHeight()-(display.getHeight()*(0.50));
int wsize=(int)((int)display.getWidth()-(display.getWidth()*0.25));
位图originalImage=decodeSampledBitmapFromResource(getResources(),imageId,wsize,hsize);
ImageView ImageView=新的ImageView(mContext);
setImageBitmap(scaleBitmap(originalImage));
setLayoutParams(新CoverFlow.LayoutParams(wsize,hsize));
imageView.setScaleType(ScaleType.MATRIX);
公共静态位图decodeSampledBitmapFromResource(资源res、int resId、int reqWidth、int reqHeight)
{
//使用INJUSTDECBOUNDS首次解码=true检查尺寸
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码资源(res、resId、options);
//计算样本大小
options.inSampleSize=计算样本大小(options、reqWidth、reqHeight);
//使用inSampleSize集合解码位图
options.inJustDecodeBounds=false;
返回BitmapFactory.decodeResource(res、resId、options);
}
公共静态int-calculateInSampleSize(BitmapFactory.Options选项、int-reqWidth、int-reqHeight)
{
//图像的原始高度和宽度
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度)
{
如果(宽度>高度)
{
inSampleSize=数学圆((浮动)高度/(浮动)要求高度);
}否则{
inSampleSize=数学圆((浮动)宽度/(浮动)宽度);
}
}
返回样本大小;
}
专用位图缩放位图(位图位图)
{
int-iWidth=bitmap.getWidth();
int iHeight=bitmap.getHeight();
int newWidth=0,newHeight=0;
float fFactor=(float)iHeight/(float)iWidth;
如果(iWidth我担心这是一个android错误:
你有没有解决过这个问题?我也有同样的问题,调整GIF的大小不起作用。
Display display = getWindow().getWindowManager().getDefaultDisplay();
int hsize= (int) ((int)display.getHeight()-(display.getHeight()*(0.50)));
int wsize= (int) ((int)display.getWidth()-(display.getWidth()*0.25));
Bitmap originalImage=decodeSampledBitmapFromResource(getResources(),imageId,wsize,hsize);
ImageView imageView = new ImageView(mContext);
imageView.setImageBitmap(scaleBitmap(originalImage));
imageView.setLayoutParams(new CoverFlow.LayoutParams(wsize,hsize));
imageView.setScaleType(ScaleType.MATRIX);
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
{
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth)
{
if (width > height)
{
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
private Bitmap scaleBitmap(Bitmap bitmap)
{
int iWidth=bitmap.getWidth() ;
int iHeight=bitmap.getHeight();
int newWidth=0,newHeight=0;
float fFactor=(float)iHeight/(float)iWidth;
if(iWidth<=iHeight)
{
newWidth = 300;
newHeight =(int) (newWidth * fFactor) ;//370;
}
else
{
newWidth = 400;
newHeight =(int) (newWidth * fFactor) ;//370;
}
float fScaleWidth = ((float) newWidth) / iWidth;
float fScaleHeight = ((float) newHeight) / iHeight;
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(fScaleWidth, fScaleHeight);
Bitmap resizedBitmappreview = Bitmap.createBitmap(bitmap, 0, 0,iWidth, iHeight, matrix, true);
return resizedBitmappreview;
}