开发者

how to change the color of certain pixels in bitmap android

i've a bitmap that i want to change c开发者_StackOverflow中文版ertain pixels. i've got the data from the bitmap into an array, but how would i set a pixels colour in that array?

thanks

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


To set the colors of the pixels in your pixels array, get values from the static methods of Android's Color class and assign them into your array. When you're done, use setPixels to copy the pixels back to the bitmap.

For example, to turn the first five rows of the bitmap blue:

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

You can also set a pixel's color in a Bitmap object one at a time without having to set up a pixel buffer with the setPixel() method:

myBitmap.setPixel(x, y, Color.rgb(45, 127, 0));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜