Android透明画布(surfaceview)

发布于 2024-12-02 13:33:54 字数 1729 浏览 0 评论 0原文

我有一个面板,通过相对布局放置在另一个视图的顶部。

我想给这个面板一个透明的背景,但在搜索了几个小时后没有找到正确的方法。当我将 alpha 设置回 0 时,我最终得到黑色背景。

希望这里有人能帮助我解决这个问题。

多谢!

该面板是通过以下代码绘制的:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

说好的呢 2024-12-09 13:33:54

在构造函数中:

setZOrderOnTop(true);

holder.addCallback(this) 之后:

holder.setFormat(PixelFormat.TRANSPARENT);

绘图开始时:

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

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);
嘿嘿嘿 2024-12-09 13:33:54

我复制了同一问题的解决方案: 如何使 SurfaceView 透明 并得到它使用与您类似的设置。

对我来说最关键的部分是“setZOrderOnTop(true)”设置,我在第一次通过时愚蠢地忽略了它。我将其放入构造函数中,并在 surfaceCreated 中将像素格式设置为 RGBA_8888。

此时,顶层布局的背景就可见了。

I copied the solution from the same question: how to make surfaceview transparent and got it to work with a setup similar to yours.

The critical piece for me was the 'setZOrderOnTop(true)' setting, which I foolishly ignored on the first pass. I put that in the constructor, and set my pixel format to RGBA_8888 inside surfaceCreated.

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

无悔心 2024-12-09 13:33:54

试试这个:

getHolder().setFormat(PixelFormat.TRANSLUCENT);

Try this:

getHolder().setFormat(PixelFormat.TRANSLUCENT);
花海 2024-12-09 13:33:54

在使用关键字 SurfaceView 而不是 Canvas 进行搜索后,我发现这是不可能的。有关详细信息,请参阅:如何使 SurfaceView 透明

因为画布的背景是静态的我给了它完全相同的背景。现在看起来是透明的:)

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

After searching with keyword surfaceview instead of canvas iI found out that it isn't possible. For more information see: how to make surfaceview transparent

Because the background of the canvas is static I've gave it the exact same background. Now it looks like it is transparent :)

    Bitmap bg = BitmapFactory.decodeResource(getResources(), R.drawable.background_panel_800_480);
    canvas.drawBitmap(bg, 0, 0, null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文