在 SurfaceView 中设置图像背景,出现黑屏 [英] Setting image background in SurfaceView, getting black screen

查看:64
本文介绍了在 SurfaceView 中设置图像背景,出现黑屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Okay so Im trying to set the background of a SurfaceView to a JPG file. But it doesn't seem to want to draw the image, and all I get is a black screen.

这是我的代码:

    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 文件夹中的平面图"图像.

Not sure why it won't draw the "floorplan" image I have saved in the drawable-mdpi folder.

有人有什么建议吗?

谢谢.

使用断点进行一些调试后,似乎缩放"变量由于某种原因变为无穷大",因此 newWidth 和 newHeight 变量变得小于 0 并且应用程序崩溃.

After doing some debugging with breakpoints, it seems like the "scaled" variable becomes "Infinity" for some reason and as such the newWidth and newHeight variables become less than 0 and the application crashes.

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

This is only if I move the entire surfaceCreated into the contstructor, if I leave the code as is here then it doesn't do anything beyind displaying a black screen.

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

No idea what's causing it to do that though...

推荐答案

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

First, you should implement SurfaceHolder.Callback interface with your MapView class and set it as a callback for its SurfaceHolder to make the app call your onSurfaceCreated() metod. Second, if you want your onDraw() method to be called, then call setWillNotDraw(false) in your MapView's constructor.

我已经这样做了:

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 文件.更新监视重复复制移动

And it works well. NOTE: Moved MapView class to a separate *.java file. Update Watch Duplicate Copy Move

这篇关于在 SurfaceView 中设置图像背景,出现黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆