java 设置 SurfaceView 的背景图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5981098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set the Background Image of a SurfaceView
提问by Hani Honey
Is there a way to set the background image of a SurfaceView? Does it have to be done in xml or can I do it all in Java - I've got something that looks like this in my constructor:
有没有办法设置 SurfaceView 的背景图像?它必须在 xml 中完成还是我可以在 Java 中完成 - 我在我的构造函数中有一些看起来像这样的东西:
Drawable sky = (getResources().getDrawable(R.drawable.sky));
this.setBackgroundDrawable(sky);
But it still doesn't show anything.
但它仍然没有显示任何东西。
采纳答案by Romain Guy
You cannot set a background drawable on a SurfaceView. You'll have to draw the background onto the surface yourself.
您不能在 SurfaceView 上设置可绘制的背景。您必须自己将背景绘制到表面上。
回答by xav
While you can't directly set a background image to a SurfaceView
, you can overlap an ImageView
(displaying your background image) and your SurfaceView
on top of this, making it transparent.
虽然您不能直接将背景图像设置为SurfaceView
,但您可以重叠一个ImageView
(显示您的背景图像)和您SurfaceView
在此之上,使其透明。
I had performances issues when drawing a 1920x1080 bitmap as a background image for each SurfaceView repaint: the only solution I found (thanks to this answer) was using an ImageView
displaying this 1920x1080 (fixed) bitmap, and using my SurfaceView
on top of it, making it transparent, to avoid painting the big background image for each SurfaceView
repaint. Now my app is much smoother, thanks to this code:
在为每个 SurfaceView 重绘绘制 1920x1080 位图作为背景图像时,我遇到了性能问题:我找到的唯一解决方案(感谢这个答案)是使用ImageView
显示这个 1920x1080(固定)位图,并SurfaceView
在它上面使用我的透明,以避免为每次SurfaceView
重绘绘制大背景图像。多亏了这段代码,现在我的应用程序更流畅了:
// Setup your SurfaceView
SurfaceView surfaceView = ...; // use any SurfaceView you want
surfaceView.setZOrderOnTop(true);
surfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT);
// Setup your ImageView
ImageView bgImagePanel = new ImageView(context);
bgImagePanel.setBackgroundResource(...); // use any Bitmap or BitmapDrawable you want
// Use a RelativeLayout to overlap both SurfaceView and ImageView
RelativeLayout.LayoutParams fillParentLayout = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
RelativeLayout rootPanel = new RelativeLayout(context);
rootPanel.setLayoutParams(fillParentLayout);
rootPanel.addView(surfaceView, fillParentLayout);
rootPanel.addView(bgImagePanel, fillParentLayout);
Then you shall start your SurfaceView
's paint method with this: (in order to "flush" the previous drawn image in SurfaceView
's buffer)
然后你应该SurfaceView
用这个开始你的paint方法:(为了“刷新”SurfaceView
缓冲区中先前绘制的图像)
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
回答by Manikandan
Try this
试试这个
public void surfaceCreated(SurfaceHolder arg0) {
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.background);
float scale = (float)background.getHeight()/(float)getHeight();
int newWidth = Math.round(background.getWidth()/scale);
int newHeight = Math.round(background.getHeight()/scale);
Bitmap scaled = Bitmap.createScaledBitmap(background, newWidth, newHeight, true);
}
public void onDraw(Canvas canvas) {
canvas.drawBitmap(scaled, 0, 0, null); // draw the background
}
回答by Serj Ardovic
A small addition to the answer by xav. You'd want to set the content view as rootPanel afterwards:
xav 对答案的一个小补充。之后您想将内容视图设置为 rootPanel:
setContentView(rootPanel);
Also, since FILL_PARENT is deprecated, consider using MATCH_PARENT in both places.
此外,由于 FILL_PARENT 已被弃用,请考虑在两个地方都使用 MATCH_PARENT。
回答by mohammad
your surfaceView.setBackground(getResources().getDrawable(R.drawable.?));