Android transparent canvas (surface overview)

I have a panel that is placed on top of another view through relativelayout.

I would like to give this panel a transparent background, but have not found the right way to do this after searching for several hours. When I set the alpha back to 0, I end up with a dark background.

Hope someone here can help me.

Thank you so much!

The panel is drawn through this code:

import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.view.SurfaceHolder; import android.view.SurfaceView; public class Panel extends SurfaceView implements SurfaceHolder.Callback { private ViewThread mThread; Paint paint = new Paint(); public Panel(Context context, AttributeSet attributeSet) { super(context, attributeSet); getHolder().addCallback(this); mThread = new ViewThread(this); } public void doDraw(Canvas canvas) { canvas.drawARGB(50, 120, 120, 120); paint.setARGB(255, 255, 0, 0); paint.setStrokeWidth(2); int CanvasHeight = canvas.getHeight(); int CanvasWidth = canvas.getWidth(); canvas.drawLine(LeftStartX, LeftStartY, StopX, StopY, paint); } public void updateDrawing(float LB, float RB, float BD, float AH, float AD ){ Left = LB; Right = RB; Distance = BD; AHeight = AH; ADistance = AD; } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {} public void surfaceCreated(SurfaceHolder holder) { if (!mThread.isAlive()) { mThread = new ViewThread(this); mThread.setRunning(true); mThread.start(); } } public void surfaceDestroyed(SurfaceHolder holder) { if (mThread.isAlive()) { mThread.setRunning(false); } } } 
+8
android transparency android-canvas draw
source share
4 answers

After searching using the keyword surfaceview instead of canvas iI, we found out that this was not possible. For more information, see how to make transparency transparent.

Since the canvas background is static, I gave it the same background. Now it looks like transparent :)

  Bitmap bg = BitmapFactory.decodeResource(getResources(), R.drawable.background_panel_800_480); canvas.drawBitmap(bg, 0, 0, null); 
+2
source share

In the constructor:

 setZOrderOnTop(true); 

After holder.addCallback(this) :

 holder.setFormat(PixelFormat.TRANSPARENT); 

At the beginning of the picture:

 canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); 
+26
source share

I copied the solution on the same issue: how to make transparency transparent , and made it work with a setting similar to yours.

The critical part for me was setting setZOrderOnTop (true), which I foolishly ignored in the first pass. I put this in the constructor and set the format of my image to RGBA_8888 inside surfaceCreated.

At this moment, the background from the top-level layout was visible.

+4
source share

Try the following:

 getHolder().setFormat(PixelFormat.TRANSLUCENT); 
+3
source share

All Articles