将图像文件绘制到SurfaceView返回空白

时间:2013-12-02 09:28:37

标签: java android eclipse adt surfaceview

我想将图像文件绘制到SurfaceView,但是当我运行代码时,SurfaceView完全空白

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit);
    displayPicture();
}

private void displayPicture(){
    SurfaceView sv = (SurfaceView) findViewById(R.id.svEditPic);
    Bitmap bmp = BitmapFactory.decodeFile(path);
    Canvas c = new Canvas();
    c.drawBitmap(bmp, 0, 0, null);
    sv.draw(c);
}

这似乎不起作用。任何解决方案?

3 个答案:

答案 0 :(得分:3)

如果您的绘图类扩展了View,则需要实现:

@Override
protected void onDraw(Canvas canvas) {}

并在那里做你的绘画。

检查android sdk文件夹中的BitmapDecode.java示例。另外,请阅读docs

答案 1 :(得分:0)

试试这个,  对于屏幕X使用一些值,分别为screenPts.x, screenPts.y使用Y pos

SurfaceView sv = (SurfaceView) findViewById(R.id.svEditPic);
    Bitmap bmp = BitmapFactory.decodeFile(path);
    Canvas c = new Canvas();
    c.drawBitmap(bmp, screenPts.x, screenPts.y, null);
    bmp.recycle();
    sv.draw(c);

答案 2 :(得分:0)

    iv = (ImageView) findViewById(R.id.ivEdit);
    Bitmap bmp = BitmapFactory.decodeFile(path);
    Bitmap temp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Config.RGB_565);
    c = new Canvas(temp);
    c.drawBitmap(bmp, 0, 0, null);
    iv.setImageBitmap(temp);

经过一番广泛的搜索,我发现SurfaceView.draw(canvas)不会自己绘制。事实上,它恰恰相反。它画到画布上。我用SurfaceView替换了SurfaceView,这对我正在做的事情更加实用。