android中如何使Canvas绘制区域透明?
我想让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我通过这个得到了输出
I got the output by this
如果你想要一个具有透明背景的画布,你必须为画布配置背景图像
使用 Bitmap.Config.ARGB_4444
并使用 0x00000000 等颜色作为透明
希望这会有所帮助!我有一个非常透明的画布:D 耶!
If you want to have a canvas with a transparent background you have to configure the background image for the
with Bitmap.Config.ARGB_4444
And use colors like 0x00000000 as transparent
hope this helps! I have a pretty transparent canvas :D yay!
第一个属性是 alpha,其余属性是 RGB 颜色。
或者
first attribute is alpha and rest are RGB colors.
or
这使画布透明,这对我有用:)
this make canvas tramsparent and it is work for me :)
在您的
onDraw()
方法中添加以下内容:这将使您的
canvas
透明,并且background View
将可见。In your
onDraw()
method add this:This will make your
canvas
transparent andbackground View
will be visible.对我来说效果很好
Works fine for me with
您可以创建与画布大小相同的位图。使用 Color.TRANSPARENCY 擦除位图的所有颜色并将其设置为画布位图:
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:
好吧,我不确定是否绘制透明画布,但在您的情况下,您可以进行调整,使用背景图像本身绘制画布。
然后你可以用手指在上面画画/绘画。
代码示例:
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:
将画布的视图背景颜色设置为#00000000
Set canvas's view background color to #00000000