Drawing an image at the same place on different screens?

Problems with Canvas, OpenGL, etc...

Drawing an image at the same place on different screens?

Postby EightBitSpade » Thu Oct 07, 2010 3:03 am

My question here regards drawing an image at the same place on the screen, regardless of the screen size or resolution. I know that I can use dips in my layout, instead of regular pixels, to help everything be sized and placed more appropriately on different screens, but here I'm talking about doing it in code.

Say, for example, I am using a custom surface view, and in my override of its onDraw function, I'm drawing an image to the screen using canvas.drawBitmap. That function takes in two numbers for coordinates, but only in hard pixels - so the actual location of the drawn image varies depending on the screen you're viewing the app on. My question is how can I draw that image in that onDraw function so that it appears in the same relative place on every screen?
EightBitSpade
Junior Developer
Junior Developer
 
Posts: 15
Joined: Sun Sep 26, 2010 2:07 am

Top

Re: Drawing an image at the same place on different screens?

Postby oxyde » Thu Oct 07, 2010 10:04 am

I don t know if it s a good thing to do but here's what i do:
in onSurfaceCreated() you can call getWidth() and getHeight() which respectively give width and height of the surface in pixels.
If you want to center your bitmap you just have to do
x = (width - bitmapSizeX) / 2 and
y = (height - bitmapSizeY) / 2
if there s a "cleanest" way to do that, plz tell me !
oxyde
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Aug 11, 2010 1:28 pm
Location: paris, france

Re: Drawing an image at the same place on different screens?

Postby EightBitSpade » Fri Oct 08, 2010 3:13 am

Hi! Thanks for the response.

Unfortunately, that doesn't work. I'm assuming your referring to the method SurfaceCreated(). Using getWidth and getHeight there only return zero each, because, as I've found, at that point the actual width and height of the view is not known.

I do think I've found a way to accomplish my goal if I can just figure out how to tell what the current resolution (width and height) of the current screen is inside my Panel (SurfaceView) class. Does anyone know how to do that? I've tried using Display metrics and the getWindowManager method, but it seems like the method getWindowManager only works inside of a class extending Activity.
EightBitSpade
Junior Developer
Junior Developer
 
Posts: 15
Joined: Sun Sep 26, 2010 2:07 am

Re: Drawing an image at the same place on different screens?

Postby oxyde » Fri Oct 08, 2010 9:11 am

yeah sorry it's surfaceCreated() implemented from SurfaceHolder.CallBack
For me it works well this way. Before this call to surfaceCreated() indeed getWidth() returns 0. But in surfaceCreated it returns the good size in pixels.
@Override
public void surfaceCreated(SurfaceHolder holder) {
this.height = getHeight();
this.width = getWidth();
[...]
oxyde
Junior Developer
Junior Developer
 
Posts: 10
Joined: Wed Aug 11, 2010 1:28 pm
Location: paris, france

Re: Drawing an image at the same place on different screens?

Postby mark@project8games.com » Fri Oct 08, 2010 4:15 pm

You should use DisplayMetrics to get the screen size/density etc.
User avatar
mark@project8games.com
Developer
Developer
 
Posts: 41
Joined: Tue Mar 02, 2010 8:33 pm

Re: Drawing an image at the same place on different screens?

Postby EightBitSpade » Fri Oct 08, 2010 6:17 pm

@oxyde:

Hmm. That's really odd. When i do it that same way, I get 0 for both my width and height. I'm not sure why that would be.

@mark

As I said, I tried setting up DisplayMetrics, and was able to just fine, but when I then went about the getWindowManager process, I am told that the method getWindowManager() is not defined. I think it's important to note that I'm trying to do all of this in a file separate from my activity, where I define my Panel class.
EightBitSpade
Junior Developer
Junior Developer
 
Posts: 15
Joined: Sun Sep 26, 2010 2:07 am

Top

Re: Drawing an image at the same place on different screens?

Postby mark@project8games.com » Fri Oct 08, 2010 8:19 pm

I have a singleton DisplayMetrics wrapper that I use. It contains the width/height of the screen and other hardware values (such as if a keyboard is present). I initialize this object in my onCreate() method and then I reference it whenever I need by doing Configuration.get().getXXXX().
User avatar
mark@project8games.com
Developer
Developer
 
Posts: 41
Joined: Tue Mar 02, 2010 8:33 pm

Re: Drawing an image at the same place on different screens?

Postby EightBitSpade » Fri Oct 08, 2010 9:22 pm

That sounds like what I'll have to be doing. I'll read up and see if I can't figure out how to do that as well. Thanks a ton for the help! :)
EightBitSpade
Junior Developer
Junior Developer
 
Posts: 15
Joined: Sun Sep 26, 2010 2:07 am

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 1 guest