views:

62

answers:

1

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

A: 

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);
sadboy
How can I draw the image using view and then move its co ordinates and then draw another view on top of it. PLease help me, I am new to Android graphics. I know the following. ImageView i = new ImageView(this); i.setImageResource(R.drawable.my_image); i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Sorry, i'm not sure on that one. I'm not the greatest with android layouts either. I just recently dealt with some canvas and bitmap code so I thought I'd try to help. You might try posting that comment as another question to get more responses. Also, look around the api demos's project for ideas.
sadboy
The [shelves](http://code.google.com/p/shelves/) project has some great drawing/layout code in it also.
sadboy
http://stackoverflow.com/questions/3442331/draw-view-on-image-within-xml