简体   繁体   English

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

[英]Android: strange green pixels on Bitmap with Alpha 200

I just get green Pixels at the border of my drawn bitmap if I set the alpha of the paint to 200. 如果我将绘制的alpha设置为200,我只会在绘制的位图边框处获得绿色像素。

The problem does not appear if I set the alpha to 100 or 255. 如果我将alpha设置为100或255,则不会出现此问题。

在此输入图像描述

How can I fix this? 我怎样才能解决这个问题?

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

I think I have solved the problem. 我想我已经解决了这个问题。 A friend has told me this solution: 一位朋友告诉我这个解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM