画布onDraw不显示任何内容

时间:2015-03-27 20:35:08

标签: android canvas android-view

我想要一个画布来编辑照片。我从画廊中获取照片并将其发送到我的自定义View.but我不会如何在画布中设置照片: 继承我的代码:

public class CanvasView extends View {
    ImageView img;
    Bitmap canvasBitmap;
    Canvas drawCanvas;

    public CanvasView(Context context) {
        super(context);
    }
    public void setCanvasPath(String bitmap_path) {
        BitmapFactory.Options decode_options = new BitmapFactory.Options();
        decode_options.inMutable = true;
        canvasBitmap  = BitmapFactory.decodeFile(bitmap_path,decode_options);

        drawCanvas = new Canvas(canvasBitmap);

        drawCanvas.drawBitmap(canvasBitmap, 0, 0, null);
        invalidate();
    }
    @Override

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}

这是xml文件:

    <!--?xml version="1.0" encoding="utf-8"?-->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent">
    </LinearLayout>

1 个答案:

答案 0 :(得分:0)

您正在错误的画布上绘制位图

  protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (canvasBitmap != null) {
                canvas.drawBitmap(canvasBitmap, 0, 0, null);
        }
  }