making UI layout with custom shape buttons - android

I want to make a layout like this. This app will be for my own use only i don't have much experience in graphics with android. I have draw these images and saved as PNG separately. I tried to put them in a layout, but i face to many difficulties with spacing and correct placing. But PNG is a rectangle and it clicks outside the image. So i think about preparing all possible situation and load all image as background. Touchable area rectangle(invisible) would be drawn inside button.
Is it a good practice to load image as a background when touch? I would have 16x4=64 backgrounds
What difficulties i can face in the future?
Should i stick to the black background and load separate buttons?
If above "Yes", then to draw background and image is better to resize image in pixels in a drawing SW like Corel? In my case 1280x720
1:
And the touch area would be something like this so i don't think it is possible to misclick it?
2:

With classic View, you won't be able to do non-rectangular view (you can draw want ever you want but your touch area is still rectangular).
The easiest way is to handle the onTouchEvent on a large View (fillin the screen with your image as background) with a View.OnTouchListener and calculate above which (fake) button you are.
For highlights you can use SurfaceView (could be the tricks #Frank.N.Stein talks about).
The hardest way is to use OpenGL graphics ... good luck for a someone who doesn't "have much experience [...]".

Related

How to change transparency of an imageview in certain area?

I'm trying to make a page of cropping profile picture. I have a crop frame which you can drag along. The image is transparent, or blurred, but within the frame, it is clear. The area within the frame will be your profile picture. I know how to set alpha, but I haven't come up with a way to set alpha within the crop frame. So this is the effect I want:
Any help would be great. Thanks!
You should probably think about having two ImageViews. One rectangular for the blurred photo and one circular placed on top of the first one for the crop preview.
When user selects a photo, you would create the blurred copy. It seems you already know how to move the photo within the ImageWiew, and so doing couple of calculations with the widths and heights of the ImageViews and the photo, should give you the correct positioning of the photos in both of the ImageViews to make them align.
Lastly when dragging on either of the ImageViews, adjust the position of the photo in both ImageViews.
This looks promising for circular ImageViews
ImageView in circular through xml
This might have something for efficient image blurring How to blur background images in Android
If you want a different approach, using OpenGL ES, you might be able to achieve realtime blurring outside of the circle with a pixel shader Shader for Android OpenGL ES. Possibly a more elegant solution, but probably not as simple.
I'm not exactly sure if you want blurring or transparency, but the solution will be very similar.

How to fix android lag for grid with a lot of elements

So what i have is a RelativeLayout that is a grid with 25x25 elements what i am trying to do is make a crossword puzzle. I use the https://github.com/natario1/ZoomLayout so i can zoom in and out of the puzzle. The cells are pretty simple they are only a TextView with background a png image that is set in the xml. There is no lag if this stays like this but if i try to use CardView or try to do a elevation of the cell so there is a shadow it starts to lag(interestingly it lags only on a real device not on emulator).
So i was wondering am i approaching the problem in a wrong way?
Should i just remove the grid and try to draw on the canvas?
Is there a way to optimize the shadows so they don't cause lag?
Should i cache the background image of the TextView in a bitmap and pass it in the TextView so i maybe get a faster loading (i don't know how this works since the image is loaded in the xml so i assume it uses a cached image)?
Should i just use some other way to do the game?
Any suggestion will be appreciated
My recommendation would be to draw the grid on the canvas and when the user taps the grid, calculate the cell at that location and add a new View (CardView with whatever properties you want) at that point. Your performance will be drastically better but it does require you do do some Math.

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.

How to implement multi-images on screen, each can be selected, moved, rotated, zoomed and bordered?

User can select some images to the screen, and be able to select/drag/move/zoom/rotate each image. When an image is selected, it will have a blue border. And user can also bring an image to the front or background.
Here is sample picture in the following: there are two images(girls) on a star-background. The top one has been selected(it has a blue border).
I don't know have to implement it. For several days of learning, I can now drag/move/zoom/rotate a single image(inside an imageview, and scaled by matrix). But I don't know how to handle multi-images.
I want to know:
Is it still a good idea to use ImageView(for each image), or what classes should I use?
How to implement the orders? (so I can bring them to front or background)
How to determine which image has been selected
How to find and draw the border of selected image?
For now, I just know how to use ImageView, but I'm not sure if I can use it to implement this task. Please give me some advices, thanks !
Drawing everything directly onto a Canvas is one way of doing it. There is an article that covers the basics of using a Canvas. You can read it HERE
There is one major downside in doing this - you have to handle all the input events by yourself. The API won't tell you which image has been clicked, or dragged. You'll have to implement that by yourself.

Android LIVE Wallpaper approach?

I'm entirely new to Android development, and I'm interested in making a live wallpaper. I was thinking about looping a set of pictures instead of drawing the animation. Is this a possible approach? If possible is this a suitable or ideal way of doing it, does it eat up memory and would i need images with different resolutions because of the fragmentation?(hundreds of different devices)
Thanks in advance :)
Sure, it should be fairly simple.
The main part of the Wallpaper engine is the Drawing of course. You need to handle the drawing manually, there is no easy way to loop through images... you need to code it. You will have to continuously monitor the time which has passed and adjust the displayed image based on that. You are given a Canvas object to draw to and it is all done during run-time in code.
The Wallpaper engine class has a event called onSurfaceChanged which gives you the width and height of the Surface which you will be drawing to. It will be called everytime the screen dimensions change (like if the phone is put into Landscape mode for instance). You need to have code that will handle any combination of width or height for all the device types out there. You will need to decide if you are going to stretch, center, tile or crop (Or a combination of those) the source images to fit any particular screen size. I would recommend scale to fit so that the image is either taller or wider than the screen and center the image either vertically or horizontally (cropping the extra bits) once that is done. I personally just use one source size and resize it to fit. You can opt to use several source files if you want which is the recommended approach I believe, but it's a little confusing.
I would start by creating a Live Wallpaper which just draws something basic like a shape to the screen. Then work out how to display an custom image and take it from there.
Good luck
You will need to be very careful about memory if taking a frame approach to animation--probably will not be possible to animate full frames--much more practical to move sprites on a background. See, fr'instance: http://davidjhinson.wordpress.com/2010/05/19/scarce-commodities-google-android-memory-and-bitmaps/
what if you have a png frame animation that is set at a certain x and y position that sits in a spot on the background image, both would have to be rescaled

Categories

Resources