Question

I am trying to draw rectangle on top of image and I am having trouble placing the art on the image. Only part of Rectangle shows up somewhere in bottom of screen. Here is what I am doing:

sinewave is the image from res>drawable folder

public void onDraw(Canvas canvas) 
{
     Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sinewave);
     canvas.drawBitmap(myBitmap, 0, 0, null);

     Paint myPaint = new Paint();
     myPaint.setColor(Color.GREEN);
     myPaint.setStyle(Paint.Style.STROKE);
     myPaint.setStrokeWidth(3);
     canvas.drawRect(0, 35 ,80 , 20, myPaint);
}

I am not able to go beyond 35, I want to place the rectangle right in the middle of sinewave's image. Can some one help me with their ideas.

Was it helpful?

Solution

I'm not sure if this would work, but there is a getScaledHeight and width method on the bitmap you could try. If this gets the right bounds this would draw a rectangle on the outer border, but you could adjust to get it where you need.

int height = bm.getScaledHeight(canvas);
int width = bm.getScaledWidth(canvas);
Rect r = new Rect(0, 0, width, height);
canvas.drawRect(r, paint);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top