如何更改位图android中某些像素的颜色 [英] how to change the color of certain pixels in bitmap android

查看:41
本文介绍了如何更改位图android中某些像素的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位图,我想更改某些像素.我已将位图中的数据放入一个数组中,但如何在该数组中设置像素颜色?

i've a bitmap that i want to change certain pixels. i've got the data from the bitmap into an array, but how would i set a pixels colour in that array?

谢谢

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

推荐答案

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

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

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

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

这篇关于如何更改位图android中某些像素的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆