java 如何在android中将位图设置为ARGB_8888?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25576869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set bitmap to ARGB_8888 in android?
提问by omega
In my android project, I load a bitmap from a image in the phone. I then do various image manipulations to it like cropping, resizing, editing pixel values.
在我的 android 项目中,我从手机中的图像加载位图。然后我对其进行各种图像操作,例如裁剪、调整大小、编辑像素值。
But the problem is the format of the bitmap is not ARGB_8888, so its not really storing the alpha values. Or rather its always keeping them at 255.
但问题是位图的格式不是 ARGB_8888,所以它并没有真正存储 alpha 值。或者更确切地说,它始终将它们保持在 255。
How can I load the bitmap in ARGB_8888 format? This is my code to load and resize. How can I specify the format in any of these?
如何加载 ARGB_8888 格式的位图?这是我加载和调整大小的代码。如何指定其中任何一种格式?
Thanks
谢谢
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;
}
Pixel class
像素类
public static Color getTransparentColor() {
return new Color(0, 0, 0, 0);
}
Color class
颜色等级
public int getRGB() {
return ((A << 24) | 0xFF) + ((R << 16) | 0xFF) + ((G << 8) | 0xFF) + (B | 0xFF);
}
回答by Grambot
You can copy your bitmap using this type of function (or you know, not use a function depending on how you want to use it)
您可以使用这种类型的函数复制位图(或者您知道,不使用函数取决于您想如何使用它)
private Bitmap ARGBBitmap(Bitmap img) {
return img.copy(Bitmap.Config.ARGB_8888,true);
}
回答by TehCoder
BitmapFactory.Options op = new BitmapFactory.Options();
op.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeFile(path, op);
BitmapFactory.Options op = new BitmapFactory.Options();
op.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeFile(path, op);
from: How can I specify the bitmap format (e.g. RGBA_8888) with BitmapFactory.decode*()?
回答by Soham
You can use this:
你可以使用这个:
public Bitmap highlightImage(Bitmap src) {
Bitmap bmOut = Bitmap.createBitmap(src.getWidth() + 96, src.getHeight() + 96, Bitmap.Config.ARGB_8888);
return bmOut;
}
回答by AkashToSky
Hope this helps as this works for me.
希望这会有所帮助,因为这对我有用。
Bitmap bitmap = Bitmap.createBitmap(marker.getMeasuredWidth(), marker.getMeasuredHeight(), Bitmap.Config.ARGB_8888);