Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

transparency - Android transparent canvas (surfaceview)

I've got a panel which is placed on top of another view via a relativelayout.

I would like to give this panel a transparent background, but didn't find the correct way to do this after searching for some hours. When I set the alpha back to 0 I end up with a black background.

Hopefully someone here can help me with this.

Thanks a lot!

The panel is drawn via 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);
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the constructor:

setZOrderOnTop(true);

After holder.addCallback(this):

holder.setFormat(PixelFormat.TRANSPARENT);

At the beginning of drawing:

canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...