设置图像背景SurfaceView,得到黑屏 [英] Setting image background in SurfaceView, getting black screen

查看:1253
本文介绍了设置图像背景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.

下面:我的code:

    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
    }

不知道为什么它不会画我都保存在绘制-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到contstructor,如果我离开了code因为是在这里那么它不beyind显示黑屏做任何事情。

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...

推荐答案

首先,你应该实现SurfaceHolder.Callback接口与你的MapView类,并将其设置为一个回调的SurfaceHolder,使应用程序调用你的onSurfaceCreated()梅托德。 其次,如果你希望你的OnDraw()方法被调用,然后调用setWillNotDraw(假)在图形页面的构造。

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
    }
}

和它工作得很好。 注意:移至图形页面类到一个单独的* .java文件。 更新表复印件移动

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

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

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