繁体   English   中英

如何在Android中将位图设置为ARGB_8888?

[英]How to set bitmap to ARGB_8888 in android?

在我的android项目中,我从手机中的图片加载了位图。 然后,我对其进行各种图像处理,例如裁剪,调整大小,编辑像素值。

但是问题在于位图的格式不是ARGB_8888,因此它没有真正存储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);
}

您可以使用这种类型的函数来复制位图(或者,根据使用方式,不使用函数)

private Bitmap ARGBBitmap(Bitmap img) {
  return img.copy(Bitmap.Config.ARGB_8888,true);
}

BitmapFactory.Options op = new BitmapFactory.Options(); op.inPreferredConfig = Bitmap.Config.ARGB_8888; bitmap = BitmapFactory.decodeFile(path, op);

来自: 如何使用BitmapFactory.decode *()指定位图格式(例如RGBA_8888)?

您可以使用此:

public Bitmap highlightImage(Bitmap src) { 
    Bitmap bmOut = Bitmap.createBitmap(src.getWidth() + 96, src.getHeight() + 96, Bitmap.Config.ARGB_8888); 
    return bmOut;
}

希望这对我有用。

Bitmap bitmap = Bitmap.createBitmap(marker.getMeasuredWidth(), marker.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM