How do I add standard scroll bars to a SurfaceView? - android

I got a SurfaceView, in which I display a bitmap that is much larger than the actual area of the SurfaceView, so I have implemented a way for the user to slide their finger on the bitmap and move it up and down. When doing so, I would like to be able to display a vertical scroll bar, preferably the standard Android scroll bars, instead of drawing something custom.
The thing is, I would like my SurfaceView to stay the size of the screen, that is I don't want to scroll the SurfaceView itself, I just want to create the illusion that the user is scrolling the contents, and therefore display the scrollbars.
I tried setting android:scrollbars = "vertical" in the layout's XML, I tried mSurfaceView.setVerticalScrollBarEnabled(true); and I tried awakenScrollBars(); whenever the user touches the SurfaceView, however none of those displays the scrollbars.
So, is it possible to display the standard Android scrollbar on a SurfaceView?

short answer is: no!
everything that the SurfaceView ever draws on the screen is whatever you directly call to be drawn using the whole lockCanvas and unlockCanvasAndPost that you know about.
Putting those parameters in the XML make it possible for you to read them in Java via the AttributeSet in the constructor, but that's only for configuration, it won't drawn anything.

Related

Which Android layout and view to use for simple image-based game?

I would like to make a simple Android game where a large background image is displayed and some other images are displayed in specific locations over it, where the other images may be clickable.
Here's a quick sample image of what I'm talking about:
The user should be able to tap the soccer player or the moose (ah, the classic "soccer player moose problem"!)
How should I render this screen (which layouts and views?) so the user can interact with it and it will scale properly on different devices?
I would use a RelativeLayout.
You can set the you background image to the layout (fill_parent for height and width).
You can then put your ImageViews, containing your moose and soccer player down on the layout relative to the top or sides of the sceen, or relative to each other (making sure to specify "dp" units for everything). Set the backgrounds of your ImageViews to be transparent, and there won't be a bounding box problem (and/or you can also set your ImageViews alignment to be relative to each other, ensuring they don't overlap).
I think this is the simplest way to do this - it is then super easy to attach onClickListener to your ImageViews in your Activity, and you are done.
This type of layout will work the same on all devices and screen sizes.
There are some small gotcha's with RelativeLayouts, but they are pretty simple once you get into them, and provide fast rendering (since the view hierarchy is usually shallow). Good Luck.
ImageView for the clickable elements seems like a fine choice to me. For the background I would just set your image as the background of the parent layout i.e. RelativeLayout
SurfaceView for the whole thing (with your field as a background) and regular *ImageView*s for added elements. You can easily recover the click coordinates from the SurfaceView and thus know what element has been touched.
SurfaceView might offer you additional possibilities anyway.
For most images, I'd use an ImageView for each one, like FoamyGuy said.
If they're close enough for overlapping bounding boxes to be an issue, you can still use an ImageView for each, but with a variation of this answer, testing alpha for each ImageView in range.
I would agree with both FoamyGuy and Booger that if your only goal is to place static images onto the screen that do something when you click them, RelativeLayout and ImageViews all the way.
But...
If you are looking to randomly spawn multiple images onto the screen in intervals and have them move around for the player to interact with while explosions are going off and maidens are being kidnapped, you should look into SurfaceView, Canvas, Drawable, TouchEvents, and FrameBuffers.

Android game programming big layouts

I wanna start a new project. A little "Jump n Run", but I have questions:
The levels will be bigger than the screen. How can I make a big Layout and edit it?
And how do the view "follows" the player, if he moves.
I dont want code samples. I only want basic ideas how to do it.
I suggest you have a look at the SurfaceView. It allows you to render bitmaps to the screen. In your case you could create a bitmap that is larger than the screen and have the user navigate it with the use of their finger (so you'll also have to also use your touchscreen). Basically what you will be changing when the user wants to go to a new location would be the src rect parameter in the drawBitmap method.
I hope this gets you started.
You could specify the layout to whatever pixel size you want (even if it's larger than any screen size would be), and then use scroll views to allow the user to scroll to other areas of the screen. Check out the Android developer docs on horizontal scroll view.

Android: Draw image over button

Is it possible to draw an image over another view (like a button) and still have the view work normally? The user would still be able to click on the button even though there is another image on top of it. I want to draw some mostly transparent pngs over my layout for lighting effects, but I want to make sure everything will still work correctly.
Try using a RelativeLayout with an ImageView on top of the widget(s) in your app. See this question for a quick example.

large, long images scrolling across screen at different speeds, and off of the left of the screen

So I have multiple very long images (like 2000x100) that i want to have scrolling across the screen at different speeds. and my original solution is not working out. Originally, I placed them all in a HorizontalScrollView, and made a runnable that used setPadding to adjust the position of the ImageViews within the scrollView. This works, however, the background that is supposed to remain mostly stationary on the screen, shakes back and forth as you scroll, and lags behind the scroll location significantly. It's choppy and it looks bad.
Is there any other way that I can move images across the screen to achieve the desired effect?
(could I perhaps use a frame layout, and set an ontouchlistener? then how would you move the layout across the screen?)
I set up a layout so that the images protrude off of the right of the screen correctly, but I can't seem to move them off of the left of the screen (setpadding(-50,0,0,0) does not work)
Thanks in advance!
I solved this by using relative layouts within a frame layout. I set an ontouchlistener for the frame layout and used the scrollTo method for each image with a modifying coefficient. I had not previously noticed the scrollTo and scrollBy methods of the layout.

Draw ontop of all views

Is there a way to draw ontop of every view (like a watermark that shows everywhere for example)? sort of how the Dock4Droid app draws a a dockbar ontop of every screen.
Your best bet is to have a relative layout as your top level container. The very last item in your markup should be an imageview containing your watermark which you can then position on the screen anywhere you like.
As an alternative, you can do your own drawing (onDraw) and set the opacity value such that it gives the presentation of being translucent. Works for me with several of my requirements.
I don't think this is possible, as android app drawing is effectively sandboxed.
Even if you could, why would you want to? It seems like a major annoyance for your users. I know I would uninstall an app that did that.

Categories

Resources