Java SurfaceView背景在Draw上减慢20-30毫秒
我使用以下代码在surfaceCreated()中设置背景图像:Java SurfaceView背景在Draw上减慢20-30毫秒,java,android,surfaceview,android-canvas,Java,Android,Surfaceview,Android Canvas,我使用以下代码在surfaceCreated()中设置背景图像: Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.untitled); float heightScale = ((float)background.getHeight())/(float)this.getHeight(); float widthScale = (float)background.getW
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.untitled);
float heightScale = ((float)background.getHeight())/(float)this.getHeight();
float widthScale = (float)background.getWidth()/(float)this.getWidth();
float scale = heightScale > widthScale ? widthScale : heightScale;
int newWidth = Math.round((float)background.getWidth()/scale);
int newHeight = Math.round((float)background.getHeight()/scale);
scaled = Bitmap.createScaledBitmap(background, newWidth, newHeight, true);
缩放为受保护的位图。在onDraw()函数中,我有以下内容:
canvas.drawBitmap(scaled, 0, 0, null); // draw the background
与使用此选项相比,这会使我的绘图速度降低20-30毫秒:
canvas.drawColor(Color.Black);
这有什么办法吗?加速后台onDraw功能?我注意到,如果我不设置背景,应用程序将无法清除我绘制的精灵。好吧。。。以下是您的操作方法: 创建布局并将布局设置为内容视图。然后将surfaceview添加到布局,然后将surfaceview像素格式设置为透明,如下所示:
panel = new Panel(this, getNewHandler());
((LinearLayout)findViewById(R.id.panellinear)).addView(panel);
SurfaceHolder p;
panel.setZOrderOnTop(true); // necessary
p = panel.getHolder();
p.setFormat(PixelFormat.TRANSPARENT)
(对于我来说,面板是扩展SurfaceView的东西)