【问题标题】:Setting image background in SurfaceView, getting black screen在 SurfaceView 中设置图像背景,出现黑屏
【发布时间】:2013-02-11 22:38:26
【问题描述】:

好的,我尝试将 SurfaceView 的背景设置为 JPG 文件。但它似乎不想绘制图像,我得到的只是黑屏。

这是我的代码:

    public class FloorplanActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MapView mapView = new MapView(getApplicationContext());
    setContentView(mapView);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.floorplan, menu);
    return true;
}

class MapView extends SurfaceView{

    Rect testRectangle1 = new Rect(0, 0, 50, 50);
    Bitmap scaled;
    int x;
    int y;

    public MapView(Context context) {
        super(context);

    }

    public void surfaceCreated(SurfaceHolder arg0){
        Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.floorplan);
        float scale = (float)background.getHeight()/(float)getHeight();
        int newWidth = Math.round(background.getWidth()/scale);
        int newHeight = Math.round(background.getHeight()/scale);
        scaled = Bitmap.createScaledBitmap(background, newWidth, newHeight, true);
    }

 public void onDraw(Canvas canvas) {
        canvas.drawBitmap(scaled, 0, 0, null); // draw the background
    }

不知道为什么它不会绘制我保存在 drawable-mdpi 文件夹中的“平面图”图像。

有人有什么建议吗?

谢谢。

编辑:在使用断点进行一些调试后,似乎“缩放”变量由于某种原因变成了“无穷大”,因此 newWidth 和 newHeight 变量变得小于 0 并且应用程序崩溃。

仅当我将整个 surfaceCreated 移动到构造函数中时,如果我将代码保持原样,那么除了显示黑屏之外它不会做任何事情。

不知道是什么导致它这样做......

【问题讨论】:

    标签: android image background surfaceview


    【解决方案1】:

    首先,您应该使用 MapView 类实现 SurfaceHolder.Callback 接口,并将其设置为其 SurfaceHolder 的回调,以使应用程序调用您的 onSurfaceCreated() 方法。 其次,如果您希望调用 onDraw() 方法,请在 MapView 的构造函数中调用 setWillNotDraw(false)。

    我已经这样做了:

    public class MapView extends SurfaceView implements SurfaceHolder.Callback {
    
        private Bitmap scaled;
    
        public MapView(Context context, AttributeSet attrs) {
            super(context, attrs);
            setWillNotDraw(false);
            getHolder().addCallback(this);
        }
    
        public void onDraw(Canvas canvas) {
            canvas.drawBitmap(scaled, 0, 0, null); // draw the background
        }
    
        @Override
        public void surfaceCreated(SurfaceHolder arg0) {
            Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.dr);
            float scale = (float) background.getHeight() / (float) getHeight();
            int newWidth = Math.round(background.getWidth() / scale);
            int newHeight = Math.round(background.getHeight() / scale);
            scaled = Bitmap.createScaledBitmap(background, newWidth, newHeight, true);
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            // TODO Callback method contents
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Callback method contents
        }
    }
    

    而且效果很好。 注意: 将 MapView 类移至单独的 *.java 文件。 更新监视重复复制移动

    【讨论】:

    • 虽然这是旧的,但谢谢。我已经在这个完全相同的问题上绞尽脑汁太久了。
    • 如果你想在这个带有背景的表面视图上绘制一些东西,你可以删除对 setWillNotDraw(false) 的调用,否则无论你画什么,都不会可见
    • 请记住,SurfaceView 有两个部分,Surface 和 View。 Surface 是一个单独的层,它与所有 View 复合在该层后面,因此通常 SurfaceView 的 View 只是一个透明窗口,布局代码使用它来确定 Surface 应该从哪里窥视。如果您覆盖onDraw(),您将在视图上进行渲染;如果你用不透明的像素填充视图,那么根本没有理由使用 SurfaceView(只需创建一个自定义视图)。
    猜你喜欢
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 2015-09-19
    • 2014-05-18
    • 2018-06-25
    相关资源
    最近更新 更多