Android 如何以编程方式压缩图像
我的资源文件夹中有30到40个图像。我目前正在使用DrawBitpMap在我的屏幕上绘制它。但它给了我一个错误,一个记忆错误。所以我试着调整图像的大小。现在我的图像分辨率是160x160。现在我不明白这个错误。它运行没有任何问题。所以我的问题是,, 我是否可以通过编程方式压缩图像,而不是在参考资料中手动调整图像的大小?尝试使用此示例Android 如何以编程方式压缩图像,android,image,Android,Image,我的资源文件夹中有30到40个图像。我目前正在使用DrawBitpMap在我的屏幕上绘制它。但它给了我一个错误,一个记忆错误。所以我试着调整图像的大小。现在我的图像分辨率是160x160。现在我不明白这个错误。它运行没有任何问题。所以我的问题是,, 我是否可以通过编程方式压缩图像,而不是在参考资料中手动调整图像的大小?尝试使用此示例 public class bitmaptest extends Activity { @Override public void onCreate(
public class bitmaptest extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linLayout = new LinearLayout(this);
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.android);
int width = bitmapOrg.width();
int height = bitmapOrg.height();
int newWidth = 200;
int newHeight = 200;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(45);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
ImageView imageView = new ImageView(this);
// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);
// center the Image
imageView.setScaleType(ScaleType.CENTER);
// add ImageView to the Layout
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
// set LinearLayout as ContentView
setContentView(linLayout);
}
}
你也可以
Bitmap.createScaledBitmap(yourimage, 160, 160, true);
尝试使用这个例子
public class bitmaptest extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linLayout = new LinearLayout(this);
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.android);
int width = bitmapOrg.width();
int height = bitmapOrg.height();
int newWidth = 200;
int newHeight = 200;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(45);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
ImageView imageView = new ImageView(this);
// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);
// center the Image
imageView.setScaleType(ScaleType.CENTER);
// add ImageView to the Layout
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
// set LinearLayout as ContentView
setContentView(linLayout);
}
}
你也可以
Bitmap.createScaledBitmap(yourimage, 160, 160, true);
要在android中压缩图像,请使用“从类位图压缩”方法 但我相信你想要缩放,而不是压缩。同一个类中有一个方法可以对其进行缩放
或者,与其每次读取图像时都调整其大小,不如在批处理文件中使用ImageMagick在PC中调整其大小,然后将其复制到已调整大小的res/文件夹中 要在android中压缩图像,请使用“从类位图压缩”方法 但我相信你想要缩放,而不是压缩。同一个类中有一个方法可以对其进行缩放
或者,与其每次读取图像时都调整其大小,不如在批处理文件中使用ImageMagick在PC中调整其大小,然后将其复制到已调整大小的res/文件夹中 这就是我解决这个问题的方法
Bitmap originalImage= Bitmap.createScaledBitmap (BitmapFactory.decodeResource(getResources(), imageId), 160, 160, true);
这就是我解决这个问题的方法
Bitmap originalImage= Bitmap.createScaledBitmap (BitmapFactory.decodeResource(getResources(), imageId), 160, 160, true);
正如我在回答中所说,除非你有令人信服的理由这样做,否则我建议你在android之外扩展图像。每次应用程序需要膨胀屏幕时缩放每个位图的成本太高。为什么每次都这么做?正如我在回答中所说的,除非你有令人信服的理由这样做,否则我建议你在android之外扩展图像。每次应用程序需要膨胀屏幕时缩放每个位图的成本太高。为什么每次都这么做?