目标是把一张bitmap中的红色“#ffef3a2e”的像素点全部设置成透明“0x00000000”,但是出现了边界红色没有透明的情况(图中下面的钢铁侠的边框还是存在的,没有透明)。搞了一下午也没弄出来为什么(图是自己画的,所以我确定这张图红色部分全部是ffef3a2e,绝对包括外围边框)。来点思路给我?为什么?package com.example.test_pixels;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;public class PView extends View implements Runnable { int BACKWIDTH;
int BACKHEIGHT; int[] Bitmap1;
int[] Bitmap2; Bitmap image = null; int bg = Color.WHITE; public PView(Context context) { super(context);

image = BitmapFactory.decodeResource(this.getResources(),
R.drawable.ironman);

BACKWIDTH = image.getWidth();
BACKHEIGHT = image.getHeight(); Bitmap1 = new int[BACKWIDTH * BACKHEIGHT];
Bitmap2 = new int[BACKWIDTH * BACKHEIGHT]; image.getPixels(Bitmap1, 0, BACKWIDTH, 0, 0, BACKWIDTH, BACKHEIGHT); for (int i = 0; i < BACKWIDTH * BACKHEIGHT; i++) { Bitmap2[i] = Bitmap1[i];

//drawable 里 ironman.png这张图是我自己画的,其中我想在Bitmap2里设置成透明的红色的
//部分,我十分确定是#ffef3a2e
if (Integer.toHexString(Bitmap1[i]).equals("ffef3a2e")
) {
//想将红色的ffef3a2e全部设置成透明,但是外围出现了一个像素的边框没有被设置成透明
//why?
Bitmap2[i] = 0x00000000;
}

} new Thread(this).start();
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(bg);
canvas.drawBitmap(image, 0, 0, null);
canvas.drawBitmap(Bitmap2, 0, BACKWIDTH, 0, 250, BACKWIDTH,
BACKHEIGHT, true, null);
} public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
postInvalidate();
}
}}
BitmapCanvasgetpixel

解决方案 »

  1.   


    这是drawable里我自己画的ironman.png的图
      

  2.   

    自己又想了下改变上述for循环中的判断 for (int i = 0; i < BACKWIDTH * BACKHEIGHT; i++) { Bitmap2[i] = Bitmap1[i];

    s3 = Integer.toHexString(Bitmap1[i]);
    if (s3.length() == 8) {

    //现在只取像素点ARGB的后六位,因为我怀疑在调用原图时,
                                    //图像可能被压缩或者其他方式处理过了,
    //可能产生了ARGB的前两位Alpha即透明度被改
                                    //变了,于是只判断后6位RGB是否是ef3a2e

    s4 = s3.substring(2, 8);
    if (s4.equals("ef3a2e")) {

    Bitmap2[i] = 0x00000000;
    }
    } }结果确实是的,改动后又使部分边沿像素点透明,但仍有残留,这些残留应该是RGB被改变了的点怎样才能使附近的残余像素全部消失?
    再把判断条件放宽?即像素点AGRB后6位在ef3a2e附近即可?
    我再去试试。