Question

I am having little difficulty drawing on bitmap. I have posted my code below. What I am trying to do is I am drawing a rect on a newly created bitmap and adding that bitmap to canvas of onDraw(). But I am not getting that rect to be displayed on screen but if I copy my code of drawing rect in the onDraw method it is displayed on screen, dont know what I am doing wrong. Can some one correct me. Thanks :)

public class MyView extends View {
Bitmap bmp;
Path path;
Paint paint;

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

    setMinimumHeight(400);
    setMinimumWidth(400);

    setupDrawing();
    square();
}

private void setupDrawing(){
    path = new Path();
    paint  = new Paint();

    paint.setColor(Color.GREEN);

}

public void square(){
    Bitmap.Config conf = Bitmap.Config.ARGB_8888;
    bmp = Bitmap.createBitmap(200, 200, conf);
    Canvas canvas = new Canvas(bmp);

     path.moveTo(0, 0);
     path.lineTo(getWidth()-50,0);
     path.lineTo(getWidth()-50, getHeight()-50);
     path.lineTo(0,getHeight()-50);
     path.lineTo(0, 0);
    canvas.drawPath(path, paint);

    invalidate();
}


@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);

    canvas.drawBitmap(bmp, 0, 0, paint);
}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(getSuggestedMinimumWidth(),    getSuggestedMinimumHeight());
};

}
Was it helpful?

Solution

This is because you are calling the function square() from constructor. At that time the view won't be created and getWidth() and getHeight() inside square() method will be zero and thus the path will not be drawn over the Bitmap. You can get the view width and height from onMeasure method. I have changed your code, please find it below,

public class MyView extends View {

private Bitmap bmp;
private Path path;
private Paint paint;
private int viewWidth, viewHeight;

public MyView(Context context) {
    super(context);
    setMinimumHeight(400);
    setMinimumWidth(400);
    setupDrawing();
}

private void setupDrawing() {
    path = new Path();
    paint = new Paint();
    paint.setColor(Color.GREEN);
}

public void square() {
    Bitmap.Config conf = Bitmap.Config.ARGB_8888;
    bmp = Bitmap.createBitmap(200, 200, conf);
    Canvas canvas = new Canvas(bmp);
    path.moveTo(0, 0);
    path.lineTo(viewWidth - 50, 0);
    path.lineTo(viewWidth - 50, viewHeight - 50);
    path.lineTo(0, viewHeight - 50);
    path.lineTo(0, 0);
    canvas.drawPath(path, paint);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    square();
    if (bmp != null)
        canvas.drawBitmap(bmp, 0, 0, paint);
}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(getSuggestedMinimumWidth(),
            getSuggestedMinimumHeight());
    viewWidth = getSuggestedMinimumWidth();
    viewHeight = getSuggestedMinimumWidth();
};

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top