我可以改进我的位图处理吗?



我正在下载位图。将其复制到可变位图中,并将所有品红像素替换为透明像素。特别是复制部分对我来说似乎是浪费资源。我该如何改进呢?

    Bitmap mIcon11 = null;
    try {
        InputStream in = new java.net.URL(urldisplay).openStream();
        mIcon11 = BitmapFactory.decodeStream(in);
    } catch (Exception e) {
        Log.d(TAG, e.getMessage());
        e.printStackTrace();
    }

    Bitmap copy = mIcon11.copy(Bitmap.Config.ARGB_8888, true);
    mIcon11.recycle();
    int [] allpixels = new int [ copy.getHeight()*copy.getWidth()];
    copy.getPixels(allpixels, 0, copy.getWidth(), 0, 0, copy.getWidth(), copy.getHeight());
    for(int i =0; i < copy.getHeight() * copy.getWidth(); i++)
    {
        if( allpixels[i] == Color.MAGENTA)
        {
            allpixels[i] = Color.TRANSPARENT;
        }
    }
    copy.setPixels(allpixels, 0, copy.getWidth(), 0, 0, copy.getWidth(), copy.getHeight());

尝试如下:

Bitmap finalBitmap = Bitmap.createBitmap(mIcon11.getWidth(), mIcon11.getHeight, Bitmap.Config.ARGB_8888);
for(int x = 0; x < mIcon11.getWidth(); x++){
    for(int y = 0; y < mIcon11.getHeight(); y++){
        if(mIcon11.getPixel(x, y) != Color.MAGENTA){
            finalBitmap.setPixel(x, y, mIcon11.getPixel(x, y)); 
        }else{
            finalBitmap.setPixel(x, y, Color.TRANSPARENT);
        }
    }
}

我刚做了这个我的头,它应该工作。如果这不起作用,尝试使用十六进制值代替颜色(例如:Color.MAGENTA;0xff00ff),如果你的品红是不一样的颜色,可以使用。洋红,或任何你想要的颜色。大多数图像编辑程序显示颜色的十六进制值。

最新更新