【发布时间】:2013-12-26 20:37:27
【问题描述】:
我试图实现一个实际上很简单的动作,即改变普通位图的颜色。 不幸的是,出现了一些错误。就我而言,我想要一张灰色钢的位图变成更红的钢。因此,我编写了一些代码,它获取每个像素的颜色 int 并提高每个像素的红色值。现在发生了两件事: 首先,转换所有像素确实需要很长时间,即使我使用 AsyncTask。 其次,当它完成一个循环时,整个位图会像下图一样旋转和倍增。 有什么方法可以顺利实现我的目标吗?问题是我经常在其他应用程序中看到此操作而没有问题,因此这一定是实现我的目标的一种方式。
谢谢!
PS:请不要被cmets激怒,他们只是想另辟蹊径!
“钢铁形象”https://drive.google.com/file/d/0B72QIg-baxzjakJsQkFRZFVFOFU/edit?usp=sharing
public void adjustColor(Bitmap bmp)
{
/*for(int i=0; i<bmp.getHeight()-1; i++) {
for(int j=0; j<bmp.getWidth()-1; j++) {
if(bmp.getPixel(j, i) != Color.TRANSPARENT) {
if(Color.green(bmp.getPixel(j, i)) <= 175 && Color.green(bmp.getPixel(j, i)) >= 65) {
red = Color.red(bmp.getPixel(j, i));
green = Color.green(bmp.getPixel(j, i));
blue = Color.blue(bmp.getPixel(j, i));
if (i == bmp.getHeight()-1) {
red = Color.red(bmp.getPixel(j, bmp.getHeight()));
green = Color.green(bmp.getPixel(j, bmp.getHeight()));
blue = Color.blue(bmp.getPixel(j, bmp.getHeight()));}
if ((red + mOfen.heatQ/10) <= 205) red = red + mOfen.heatQ/10;
bmp.setPixel(j, i, Color.rgb(red, green, blue));
}}
}
}*/
for(int h=0; h<bmp.getWidth()*bmp.getHeight(); h++) {
int x = h-bmp.getWidth()*(((int)h/bmp.getWidth()+1)-1);
int y = h/bmp.getWidth();
Log.d("roh", "y " + Integer.toString(y));
Log.d("roh", "height " + Integer.toString(bmp.getHeight()));
if(Color.green(bmp.getPixel(x, y)) <= 175 && Color.green(bmp.getPixel(x, y)) >= 65) {
bmp.setPixel(x, y, Color.WHITE);
red = Color.red( allpixels[h] );
green = Color.green( allpixels[h] );
blue = Color.blue( allpixels[h] );
/* for(int n=1; n<=10; n++) {
/*Log.d("roh", "left " + Float.toString(mOfen.rRoh[n-1].left));
Log.d("roh", "right " + Float.toString(mOfen.rRoh[n-1].right));
Log.d("roh", "n " + Integer.toString(n));
Log.d("roh", "x+RohX " + Float.toString(x+RohX));
Log.d("roh", "top " + Float.toString(mOfen.rRoh[n-1].top));
Log.d("roh", "bottom " + Float.toString(mOfen.rRoh[n-1].bottom));
Log.d("roh", "n " + Integer.toString(n));
Log.d("roh", "y+RohY " + Float.toString(y+RohY)); */
/* if( mOfen.rRoh[n-1].left < x+RohX && y+RohY > mOfen.rRoh[n-1].top &&
mOfen.rRoh[n-1].right > x+RohX && y+RohY < mOfen.rRoh[n-1].bottom) {
/*if ((mOfen.heat[n-1]) <= 245 && (mOfen.heat[n-1]) > red ) red = mOfen.heat[n-1]; */ if(red<255) red++;
/* Log.d("red", "red" + Integer.toString(red));
Log.d("red", Float.toString(x+mOfen.rRoh[n-1].left));
Log.d("red", Float.toString((mOfen.rRoh[n-1].left)));
}} */
allpixels[h] = Color.rgb(red, green, blue);
}
}
copyArrayIntoBitmap(bmp);
//bmp.setPixels(allpixels, 0, bmp.getWidth(), 0, 0, bmp.getWidth(), bmp.getHeight());
}
public void copyBitmapIntoArray(Bitmap bmp)
{
allpixels = new int[bmp.getWidth()*bmp.getHeight()];
int count = 0;
for(int i=0; i<bmp.getHeight()-1; i++) {
for(int j=0; j<bmp.getWidth()-1; j++) {
allpixels[count] = bmp.getPixel(j, i);
count++;
}
}
}
public void copyArrayIntoBitmap(Bitmap bmp)
{
int count = 0;
for(int i=0; i<bmp.getHeight()-1; i++) {
for(int j=0; j<bmp.getWidth()-1; j++) {
bmp.setPixel(j, i, allpixels[count]);
count++;
}
}
}
【问题讨论】: