在android中删除图像的白色背景

问题描述 投票:0回答:2

我在android中的框架布局中添加了一个内部图像。但问题是我得到了白色背景。我需要删除那个白色背景。任何帮助将不胜感激......

截图如下

enter image description here

xml 文件如下

<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView  
        android:id="@+id/imageView"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:scaleType="center"
         />

    <ImageView
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="left"
        android:background="@null"
        android:maxWidth="-5dp"  
        android:maxHeight="-5dp"
        android:src="@drawable/driveimg"
         />

</FrameLayout>
android image android-layout android-xml
2个回答
5
投票

您可以将背景设置为空(

android:background="@null"
)或使用透明颜色(
android:background="@android:color/transparent"


0
投票

你可以试试这个

// Load the image
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.your_image);

// Define the background color to remove (example: white)
int backgroundColorToRemove = Color.WHITE;

// Create a copy of the original bitmap
Bitmap modifiedBitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);

// Iterate through each pixel
for (int x = 0; x < modifiedBitmap.getWidth(); x++) {
    for (int y = 0; y < modifiedBitmap.getHeight(); y++) {
        int pixelColor = modifiedBitmap.getPixel(x, y);
        if (pixelColor == backgroundColorToRemove) {
            modifiedBitmap.setPixel(x, y, Color.TRANSPARENT); // or set to another color
        }
    }
}

// Set the modified bitmap to an ImageView
imageView.setImageBitmap(modifiedBitmap);
© www.soinside.com 2019 - 2024. All rights reserved.