I'm working on a project where I have a static image as background and I need to put some Views on specific locations relatively to some elements that appears on the background picture.
When I'm using FrameLayout or RelativeLayout and trying to measure the exact dp from the corners I get different results for different screens sizes and the relation with the background image is lost.
I try thinking for solutions. Maybe I need to measure the image background size on the fly and then try to calculate the exact position? Or maybe put some "water marks" in the background images and get the correct positions by scanning the bitmap on the fly.
What is the best approach?
Related
I am working on listView item in Unity and stuck at fitting the image properly in UI Image. I tried multiple things using Preserver Aspect Ratop, using Filled image type almost every thing but could not make it look fine. In android we call it center crop what i am trying to achieve. First image is with Preserve Aspect while the second one is without it.
What should i do to achieve it.
Thank you.
This is one way to do it that I can think of, using 2 images. The outer image is set at your desired size and have a Mask component:
The inner image is set at center of the outer image and scaled to fit the outer image (either programatically or by hand) It will be cropped by the outer image's mask and only display the area you want.
I'm new on the Android development and I have some problems to place views in a precise position, indipendently from the resolution or the screen size.
I have a background image and I would like to put TextViews/Buttons in a specific position without use dip coordinates.
I use a Frame Layout to put the background image and the views on different layers but the problem arise when I change screen resolution or screen size, the elements change position.
Do you know how I can solve this issue?
Thanks
I am attempting to make an 8x8 matrix of coloured squares on a black background in a layout that is limited to portrait only. I need the squares to all be equal in size, and the overall matrix to be 60% of the screen's width and of equal height. I also need to control the colour of each square dynamically as a result of my code, call it a pattern setting code if you will. Here is an example of what I need the final product to look like:
Screen example
I have been researching different ways of doing this and was hoping for some advice on the best approach. Here are some ideas I had:
1) Use GridLayout (not GridView) and set the background of each cell to the required colour. This shouldn't be difficult to control with java, but how do I enforce the correct dimensions? Would I need to fill each grid cell with something of a certain size, or can I use layout_weight attributes to control the dimensions?
2) Use a combination of RelativeLayout and LinearLayout with ImageView and display locally saved images of coloured squares. Can I use layout_weight to control the size of these images, and if these images were too small would they be stretched to fill their required dimensions? (I'm thinking of larger tablet screens.)
What I really need from someone is some sample code to implement a very simple version of what I need (for example one square in the centre of the screen that I can colour-control dynamically). Obviously I can then expand it to my specific requirements. If more details or code are required I will happily post them.
Perhaps you could just create a custom View object, and render the appropriate bitmap in the onDraw method? Seems like overkill to use layout objects, unless you're planning to do more than just render squares of colour?
Edit: Alternatively, I once used the info in the following posting to programmatically build a layout grid. It's not exactly what you need (it generates as many horizontal 'cells' as are necessary), but should make for insightful reading on manipulating layout dimensions:
Line-breaking widget layout for Android
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
I have a requirement where few of my image views form border areas of an application, and the application will be running on many screen sizes.
I'll be creating a uniform image for the smallest size possible, which can be repeated as many times as needed, and still presents the same image.
I want the image view to replicate the image contained whenever it is increased.
Is anything like this possible?
I tried 9-patch images, but I couldn't find nice articles on it which could explain how to create useful images with it which could suit my need.
I think you have to write a custom view to do this. Overide onDraw method in it to copy your bitmap as many times as needed.
Get the width and height of the device dynamically and set the imageview width and height in program.