Android透明画布(surfaceview)
我有一个面板,通过相对布局放置在另一个视图的顶部。
我想给这个面板一个透明的背景,但在搜索了几个小时后没有找到正确的方法。当我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在构造函数中:
在
holder.addCallback(this)
之后:绘图开始时:
In the constructor:
After
holder.addCallback(this)
:At the beginning of drawing:
我复制了同一问题的解决方案: 如何使 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.
试试这个:
Try this:
在使用关键字 SurfaceView 而不是 Canvas 进行搜索后,我发现这是不可能的。有关详细信息,请参阅:如何使 SurfaceView 透明
因为画布的背景是静态的我给了它完全相同的背景。现在看起来是透明的:)
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 :)