0

I've created a simple custom control in android and on it I place a background image. I'm having problems when the control is placed on a layout at different sizes (i.e. when it is stretched), specifically:

  1. I wish to overlay a rectangle at a specific position and size, which I know the pixel position for the original image. How can I do this with my control. I suspect something like this is impossible given it's a 9-patch. Is my best bet to work out the percentage from the top/left on the original or is that pointless given some parts stretch and some don't?

  2. In the custom control I set the image like this in the constructor:

    setBackgroundResource(R.drawable.buttonbt);

Which is working just fine, however I wanted to originally draw it in the onDraw event as I might want to change it depending on property changes, e.g.

Bitmap b=BitmapFactory.decodeResource(getResources(), R.drawable.buttonbt);
canvas.drawBitmap(b, 0, 0, null);

But this does not resize according to the size of its bounding box, it is simply trying to show it at it's original size without scaling to fit. How would you do this (whether the former method is better or not).

thanks.

Neil Walker
  • 6,400
  • 14
  • 57
  • 86
  • Ad 1 dont know what you mean – pskink Sep 02 '13 at 15:00
  • ad 2 use drawBitmap with Matrix param, set Matrix using Matrix.rectToRect(- – pskink Sep 02 '13 at 15:02
  • Regarding 1. Yes, it's hard to describe, maybe I should have put a picture in :) essentially a control can be stretched depending on it's width/height and the density of the display. I wish to overlay the view at a specific relative offset with say a rectangle - how to calculate this position so it's the same no matter what the size. Any better? – Neil Walker Sep 02 '13 at 16:36
  • you can see my answer fot this question http://stackoverflow.com/questions/16729169/how-to-maintain-multi-layers-of-imageviews-and-keep-their-aspect-ratio-based-on but basically when you draw Bitmap using. matrix you can use the same matrix to map points rectangles etc – pskink Sep 02 '13 at 16:44

2 Answers2

1

You can create a scaled bitmap as below

Bitmap bitmap = Bitmap.createScaledBitmap(b, width, height, true);

Hope it will work for you. Please let me know!

  • Thanks. I take it there's an easy way to get size of view that is required? I think the onMeasure method has thrown me into confusion and whether I need to use that or not. – Neil Walker Sep 02 '13 at 16:34
  • I don't think you really need to use onMeasure function. –  Sep 02 '13 at 19:33
0

ok when your View is say 100x100 px and your Bitmap is 300x300 you can try the following (pseudo code here) in inDraw method:

# src is a Bitmap 300x300
# dst is a View 100x100
mMatrix.setRectToRect(RectF src, RectF dst, Matrix.ScaleToFit stf)
canvas.save()
canvas.concat(mMatrix)
canvas.drawBitmap(mBitmap, 0, 0, null)
// actually it will draw a rect with left/top edge at (10, 10) and right/bottom at (20, 20)
canvas.drawRect(30, 30, 60, 60, paint)
canvas.restore()
pskink
  • 23,874
  • 6
  • 66
  • 77