问题描述
我有一个面板,它通过一个RelativeLayout的放置在另一个视图之上。
我想给这个面板透明背景,但没有发现搜索几个小时后,要做到这一点的正确方法。当我设置阿尔法回到0我结束了一个黑色的背景。
希望这里有人能帮助我。
非常感谢!
面板通过此code得出:
进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Paint;
进口android.util.AttributeSet;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;
公共类面板扩展了SurfaceView实现SurfaceHolder.Callback {
私人ViewThread mThread;
涂料粉刷=新的油漆();
公共面板(上下文的背景下,AttributeSet中的AttributeSet){
超(背景下,AttributeSet中的);
。getHolder()的addCallback(本);
mThread =新ViewThread(本);
}
公共无效doDraw(帆布油画){
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,油漆);
}
公共无效updateDrawing(浮动LB,RB浮动,浮动BD,飘啊,飘AD){
左=磅;
右= RB;
距离= BD;
A高= AH;
ADistance = AD;
}
公共无效surfaceChanged(SurfaceHolder持有人,INT格式,诠释的宽度,高度INT){}
公共无效surfaceCreated(SurfaceHolder持有者){
如果(!mThread.isAlive()){
mThread =新ViewThread(本);
mThread.setRunning(真正的);
mThread.start();
}
}
公共无效surfaceDestroyed(SurfaceHolder持有者){
如果(mThread.isAlive()){
mThread.setRunning(假);
}
}
}
与关键字surfaceview代替帆布II搜索后发现,这是不可能的。欲了解更多信息,请参阅:如何使surfaceview透明
由于画布的背景是静态的,我把它完全一样的背景。现在看来,它是透明的:)
位图BG = BitmapFactory.de codeResource(getResources(),R.drawable.background_panel_800_480);
canvas.drawBitmap(BG,0,0,NULL);
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);
}
}
}
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);
这篇关于Android的透明画布(surfaceview)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!