Android:位图与Alpha 200上的奇怪绿色像素

时间:2015-02-27 15:47:14

标签: android bitmap surfaceview alpha pixels

如果我将绘制的alpha设置为200,我只会在绘制的位图边框处获得绿色像素。

如果我将alpha设置为100或255,则不会出现此问题。

enter image description here

我该如何解决这个问题?

public class GameView extends SurfaceView implements SurfaceHolder.Callback {

private DrawThread drawThread;
private boolean surfaceCreated;

Paint paint = new Paint();
private Bitmap bitmap;


public GameView(Context context, AttributeSet attrs) {
    super(context, attrs);

    getHolder().addCallback(this);

    bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.circleyellow);

    paint.setAlpha(200);

}

protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(bitmap, 200, 200, paint);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    setSurfaceCreated(true);
    createDrawThread();                     
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    setSurfaceCreated(false);
}

public void setSurfaceCreated(boolean surfaceCreated) {
    this.surfaceCreated = surfaceCreated;
}

public boolean getSurfaceCreated() {
    return surfaceCreated;
}

public void createDrawThread(){
    if (drawThread != null) {
        drawThread.destroy();           
    }
    drawThread = new DrawThread(getHolder(), this);
    drawThread.setRunning(true);
    drawThread.start();
}

public DrawThread getDrawThread(){
    return drawThread;
}

1 个答案:

答案 0 :(得分:2)

我想我已经解决了这个问题。一位朋友告诉我这个解决方案:

public GameView(Context context, AttributeSet attrs) {
    super(context, attrs);

    getHolder().addCallback(this);
    getHolder().setFormat(PixelFormat.TRANSLUCENT);

    bitmap = BitmapFactory.decodeResource(
        getResources(), 
        R.drawable.circleyellow
    );

    paint.setAlpha(200); 
}