0

I have a Panel class, which extends SurfaceView. The panel fills my whole activity. Inside the panel I draw a Ball - Bitmap (and some other stuff(lines/squares)).

I add some pseudo depth. Something like 2.5 D, not really 3D, but kind of. But now I am facing a problem, when the Ball goes into depth I don't know how to re size it. Because the Ball is constantly moving back and forth (left/right, up/down too).

I don't know where to read about it, but I think if I add the Bitmap to an ImageView, this would solve all my problems.

My First question, does this even solve my problem? And if not, how else could I solve it.

And my second question is, how do I add an Imageview on a SurfaceView?

I found some good hints here: Implement a ImageView to a SurfaceView but it only says what to do, not how to do it. Because nothing happens in my activity. Everything happens in my Panel.

Community
  • 1
  • 1
Aqua
  • 1

1 Answers1

0

I think the answer to one of my questions may help you: Unable to draw on top of a custom SurfaceView class. It's not possible to add a ImageView into a SurfaceView because SurfaceView doesn't extent the ViewGroup class, but as that answer shows, it is straightforward to draw on top of a SurfaceView.

In that question, I had a custom view called DrawOnTop which inherited directly from View, and I drew resized and rotated bitmaps onto that view's canvass without any problems. My guess is that you'll be able to do the same will your ball object. The code I used was as follows:

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap);
int bitmapOrg_width = bitmapOrg.getWidth();
int bitmapOrg_height = bitmapOrg.getHeight();

float bitmapScaleFactor = 1.5;   // to create a resized bitmap 50% bigger
float angle = 90;                // to rotate by 90 degrees

Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(bitmapScaleFactor, bitmapScaleFactor);
// rotate the Bitmap
matrix.postRotate(angle);
// create the resized bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, bitmapOrg_width, bitmapOrg_height, matrix, true);
canvas.drawBitmap(resizedBitmap, left, top, null);
Community
  • 1
  • 1
Stochastically
  • 7,616
  • 5
  • 30
  • 58
  • I came across the same solution. But this way i would constantly making new Bitmap objects. That is what i tried to avoid. But maybe its not that big of a problem if i always create new objects?! – Aqua Mar 27 '13 at 20:12
  • As far as I know, that's the only way to do it. My app constantly creates new objects, and there don't seem to be any performance issues on the devices that I've seen it run on. However, if you're just scaling and not rotating the objects, it might be sensible to create all the possible scaled objects in advance and put them in an array. That would improve performance as long as it doesn't use up too much memory. – Stochastically Mar 28 '13 at 07:15
  • What if fear is the Garbage Collector. As this is my first android project, I heard the the GC is my worst enemy... – Aqua Mar 28 '13 at 14:26
  • Again, I've never had that problem, [Aqua](http://stackoverflow.com/users/2216199/aqua). Although [this answer](http://stackoverflow.com/questions/3117429/garbage-collector-in-android) suggests that you should force garbage collection before creating bitmaps. But if, as I suggested, you can create all your bitmaps in advance there shouldn't be a problem once they've all been instantiated (as long as it doesn't leave your app short of memory). Note though that in C#, I've seen significant performance reductions by calling GC, because each time it has to trundle through all the objects. – Stochastically Mar 28 '13 at 19:34