user2810895
user2810895

Reputation: 1364

Draw on transparent SurfaceView

I have the following problem: I made a class that extends from SurfaceView, it also implements SurfaceHolder.Callback but when I try to draw a red square, it's always black. Below my code:

First I call the following method in the constructor:

 private void initTransparentBackgroundColor(){
        this.setBackgroundColor(Color.TRANSPARENT);
        this.setZOrderOnTop(true);
        getHolder().setFormat(PixelFormat.TRANSPARENT);
 }

Then, I start drawing:

 protected void onDraw(Canvas canvas){
        Paint p = new Paint();
        p.setColor(Color.RED);
        p.setStyle(Paint.Style.FILL_AND_STROKE);
        canvas.drawRect(10,10,20,20,p);
}

What am I doing wrong?

Upvotes: 1

Views: 550

Answers (1)

Hemendra Sharma
Hemendra Sharma

Reputation: 1060

Replace

getHolder().setFormat(PixelFormat.TRANSPARENT);

To this

getHolder().setFormat(PixelFormat.TRANSLUCENT);

Good Luck. :)

Upvotes: 2

Related Questions