如何在Android中将位图设置为ARGB_8888? [英] How to set bitmap to ARGB_8888 in android?

查看:128
本文介绍了如何在Android中将位图设置为ARGB_8888?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

但是问题是位图的格式不是ARGB_8888,因此它实际上没有存储alpha值。或更确切地说,它始终使它们保持在255。

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格式的位图?这是我要加载和调整大小的代码。
如何指定任何一种格式?

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?

谢谢

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);
}


推荐答案

您可以使用以下方法复制位图这种类型的函数(或者,您知道,不使用函数取决于您的使用方式)

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);
}

这篇关于如何在Android中将位图设置为ARGB_8888?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆