如何更改位图android中某些像素的颜色

tur*_*boy 13 android bitmap image-processing

我有一个位图,我想改变某些像素.我已经将位图中的数据转换为数组,但是如何在该数组中设置像素颜色?

谢谢

int[] pixels = new int[myBitmap.getHeight()*myBitmap.getWidth()];
            myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

            for(int i =0; i<500;i++){
                //Log.e(TAG, "pixel"+i +pixels[i]);
Run Code Online (Sandbox Code Playgroud)

BHS*_*key 20

要设置pixels数组中像素的颜色,请从Android的Color类的静态方法中获取值,并将它们分配到数组中.完成后,使用setPixels将像素复制回位图.

例如,要将位图的前五行变为蓝色:

import android.graphics.Color;

int[] pixels = new int[myBitmap.getHeight()*myBitmap.getWidth()];
myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
for (int i=0; i<myBitmap.getWidth()*5; i++)
    pixels[i] = Color.BLUE;
myBitmap.setPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
Run Code Online (Sandbox Code Playgroud)

您还可以在Bitmap对象中一次设置一个像素的颜色,而无需使用setPixel()方法设置像素缓冲区:

myBitmap.setPixel(x, y, Color.rgb(45, 127, 0));
Run Code Online (Sandbox Code Playgroud)