在我的Android项目中,我从手机上的图像加载了一个位图。然后,我对它进行各种图像操作,例如裁剪、调整大小和编辑像素值。
但问题在于,该位图的格式不是ARGB_8888,因此它实际上并没有存储alpha值。或者说它始终将alpha值保持为255。
那么我该如何在ARGB_8888格式下加载位图呢?下面是我的加载和调整大小代码,请问我应该在哪里指定格式?
谢谢。
像素类
颜色类
但问题在于,该位图的格式不是ARGB_8888,因此它实际上并没有存储alpha值。或者说它始终将alpha值保持为255。
那么我该如何在ARGB_8888格式下加载位图呢?下面是我的加载和调整大小代码,请问我应该在哪里指定格式?
谢谢。
private static Bitmap resize(Bitmap img, int newW, int newH) throws IOException {
Bitmap resizedImg = Bitmap.createScaledBitmap(img, newW, newH, false);
img.recycle();
Bitmap newresizedImg = resizedImg.copy(Bitmap.Config.ARGB_8888, true);
resizedImg.recycle();
Pixel initialPixel = Function.getPixel(0, 0, newresizedImg, null);
int a = initialPixel.getColor().getAlpha(); // -> 255
newresizedImg.setPixel(0, 0, Pixel.getTransparentColor().getRGB());
initialPixel = Function.getPixel(0, 0, newresizedImg, null);
int new_a = initialPixel.getColor().getAlpha(); // -> 255
return newresizedImg;
}
private static Bitmap getImage(String from) throws IOException {
File file = new File(from);
if (file.exists()) {
BitmapFactory.Options op = new BitmapFactory.Options();
op.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bufferedImage = BitmapFactory.decodeFile(from, op);
return bufferedImage;
}
return null;
}
像素类
public static Color getTransparentColor() {
return new Color(0, 0, 0, 0);
}
颜色类
public int getRGB() {
return ((A << 24) | 0xFF) + ((R << 16) | 0xFF) + ((G << 8) | 0xFF) + (B | 0xFF);
}