android中如何使Canvas绘制区域透明?

发布于 2024-12-23 16:46:10 字数 1385 浏览 0 评论 0原文

我想让 Canvas 区域透明,因为我想在其后面添加一个图像,以便 Canvas 操作发生在图像上方。我的画布代码在这里

public class Panel extends SurfaceView implements SurfaceHolder.Callback {

private ViewThread mThread;
private ArrayList<Element> mElements = new ArrayList<Element>();

public Panel(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    getHolder().addCallback(this); 
    mThread = new ViewThread(this); 
} 


public void doDraw(Canvas canvas) {
    canvas.drawColor(Color.TRANSPARENT);
    synchronized (mElements) {
        for (Element element : mElements) {
            element.doDraw(canvas);
        }
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    // TODO Auto-generated method stub
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if (!mThread.isAlive()) {
        mThread = new ViewThread(this);
        mThread.setRunning(true);
        mThread.start();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    if (mThread.isAlive()) {
        mThread.setRunning(false);
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    synchronized (mElements) {
        mElements.add(new Element(getResources(), (int) event.getX(), (int) event.getY()));
    }
    return super.onTouchEvent(event);
}

}

如何实现它,它上面的任何片段都会非常有帮助。谢谢

I would like to make Canvas area transparent because I would like to add an Image behind it so that Canvas actions happen above the image.My code for the canvas is here

public class Panel extends SurfaceView implements SurfaceHolder.Callback {

private ViewThread mThread;
private ArrayList<Element> mElements = new ArrayList<Element>();

public Panel(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    getHolder().addCallback(this); 
    mThread = new ViewThread(this); 
} 


public void doDraw(Canvas canvas) {
    canvas.drawColor(Color.TRANSPARENT);
    synchronized (mElements) {
        for (Element element : mElements) {
            element.doDraw(canvas);
        }
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    // TODO Auto-generated method stub
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if (!mThread.isAlive()) {
        mThread = new ViewThread(this);
        mThread.setRunning(true);
        mThread.start();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    if (mThread.isAlive()) {
        mThread.setRunning(false);
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    synchronized (mElements) {
        mElements.add(new Element(getResources(), (int) event.getX(), (int) event.getY()));
    }
    return super.onTouchEvent(event);
}

}

How to achieve it, any snippets on it will be very much helpful.Thanks

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

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

发布评论

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

评论(9

遥远的她 2024-12-30 16:46:10

我通过这个得到了输出

 public Panel(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.setBackgroundColor(Color.TRANSPARENT);                 
    this.setZOrderOnTop(true); //necessary                
    getHolder().setFormat(PixelFormat.TRANSPARENT); 
    getHolder().addCallback(this); 
    mThread = new ViewThread(this); 

} 

I got the output by this

 public Panel(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.setBackgroundColor(Color.TRANSPARENT);                 
    this.setZOrderOnTop(true); //necessary                
    getHolder().setFormat(PixelFormat.TRANSPARENT); 
    getHolder().addCallback(this); 
    mThread = new ViewThread(this); 

} 
优雅的叶子 2024-12-30 16:46:10

如果你想要一个具有透明背景的画布,你必须为画布配置背景图像

mCanvas = new Canvas(mBackgroundBitmap);

使用 Bitmap.Config.ARGB_4444

并使用 0x00000000 等颜色作为透明

    Bitmap mBackgroundImage = Bitmap.createBitmap(Size, Size,
                    Bitmap.Config.ARGB_4444);
    mCanvas = new Canvas(mBackgroundImage);

希望这会有所帮助!我有一个非常透明的画布:D 耶!

If you want to have a canvas with a transparent background you have to configure the background image for the

mCanvas = new Canvas(mBackgroundBitmap);

with Bitmap.Config.ARGB_4444

And use colors like 0x00000000 as transparent

    Bitmap mBackgroundImage = Bitmap.createBitmap(Size, Size,
                    Bitmap.Config.ARGB_4444);
    mCanvas = new Canvas(mBackgroundImage);

hope this helps! I have a pretty transparent canvas :D yay!

无边思念无边月 2024-12-30 16:46:10
canvas.drawColor(Color.argb(0, 255, 255, 255));

第一个属性是 alpha,其余属性是 RGB 颜色。

或者

canvas.drawColor(Color.TRANSPARENT);
canvas.drawColor(Color.argb(0, 255, 255, 255));

first attribute is alpha and rest are RGB colors.

or

canvas.drawColor(Color.TRANSPARENT);
飘逸的'云 2024-12-30 16:46:10

这使画布透明,这对我有用:)

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

this make canvas tramsparent and it is work for me :)

canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.OVERLAY);
心在旅行 2024-12-30 16:46:10

在您的 onDraw() 方法中添加以下内容:

canvas.drawColor(0x00AAAAAA);

这将使您的 canvas 透明,并且 background View 将可见。

In your onDraw() method add this:

canvas.drawColor(0x00AAAAAA);

This will make your canvas transparent and background View will be visible.

再见回来 2024-12-30 16:46:10
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)

对我来说效果很好

override fun onFinishInflate() {
    super.onFinishInflate()
    setZOrderOnTop(true)
    holder.setFormat(PixelFormat.TRANSLUCENT)
    isFocusable = true
    holder.addCallback(surfaceCallback)
}
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)

Works fine for me with

override fun onFinishInflate() {
    super.onFinishInflate()
    setZOrderOnTop(true)
    holder.setFormat(PixelFormat.TRANSLUCENT)
    isFocusable = true
    holder.addCallback(surfaceCallback)
}
我的黑色迷你裙 2024-12-30 16:46:10

您可以创建与画布大小相同的位图。使用 Color.TRANSPARENCY 擦除位图的所有颜色并将其设置为画布位图:

Bitmap transparentBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),
Bitmap.Config.ARGB_8888);
transparentBitmap.eraseColor(Color.TRANSPARENT);
canvas.setBitmap(transparentBitmap);

You can create a Bitmap of the same size as the canvas. Erase all the colors of the bitmap using Color.TRANSPARENCY and set it as a canvas bitmap:

Bitmap transparentBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),
Bitmap.Config.ARGB_8888);
transparentBitmap.eraseColor(Color.TRANSPARENT);
canvas.setBitmap(transparentBitmap);
倦话 2024-12-30 16:46:10

好吧,我不确定是否绘制透明画布,但在您的情况下,您可以进行调整,使用背景图像本身绘制画布。

然后你可以用手指在上面画画/绘画。

代码示例:

        BitmapDrawable bd = (BitmapDrawable)<YOUR_ACTIVITY>.this.getResources().getDrawable(R.drawable.<DRAWBLE_ID>);
        Bitmap b = bd.getBitmap();

        mBitmap = Bitmap.createBitmap(b,0,0,100,100); // This line is required only if you wanna some change in the bitmap you created
        mCanvas = new Canvas(mBitmap);

well, I am not sure about drawing the transparent canvas, but in your case you can do a tweak, that draw the canvas using the background image iteself.

And then you can draw/ paint by finger on it.

Code example:

        BitmapDrawable bd = (BitmapDrawable)<YOUR_ACTIVITY>.this.getResources().getDrawable(R.drawable.<DRAWBLE_ID>);
        Bitmap b = bd.getBitmap();

        mBitmap = Bitmap.createBitmap(b,0,0,100,100); // This line is required only if you wanna some change in the bitmap you created
        mCanvas = new Canvas(mBitmap);
小梨窩很甜 2024-12-30 16:46:10

将画布的视图背景颜色设置为#00000000

Set canvas's view background color to #00000000

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文