簡體   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