Getting the Dimensions of a Home Screen Widget's Element - android

I am working on a widget, where I need to have a chart displayed in addition to some text fields. I have figured out how to generate a visualization of the graph and then convert it to a bitmap, which gets placed in the ImageView. The problem is the following:
In a normal Android app, I would get the width and height of the ImageView and pass these as parameters for the bitmap generation. This way, the image will be fitting perfectly within its container. Since I am using a home screen widget, this is not possible, as far as I am aware, though. As a result of this, I am trying to derive the size of the ImageView by trying to figure out what size would the text fields have, but I also have to calculate those and so far have had no luck with getting it right. What I end up with is always having excess space above and beneath the image, or on its sides and the size of this is really varying according to the screen the widget is presented on. You can see some examples in the two links below.
Example A This is the default widget size. For this screen, only a small gap can be seen on the sides of the graph.
Example B Resizing the widget and recalculating the size leads to having bigger gaps on the two sides, though. For other screens the issue can be even more jarring.
My question is:
How could I get/calculate the necessary resolution for my bitmap?

Related

Titanium/ Alloy/ Appcelerator: Cover entire screen with image

I have a JPEG photo which I want to use to as a background for one of the screens.
I want the behaviour to be simlar to the css backgroundSize: cover property. (Play with the following tool to see what I mean: https://davidwalsh.name/demo/background-size.html)
For iPhone, this issue is fairly easy, as you can literally have several different versions of the same image depending which iPhone the app is being installed on.
For Android, this is much more difficult as they have various different aspect ratios and resolutions.
Therefore my question is, how can I specify an image that covers the entire screen.
You can always fake this by adding the imageview inside a View and calculate the proper height/width you need to cover the screen (get the shortest side and scale it up and multiply the other side by the original ratio).
You could also use https://github.com/AndreaVitale/imageview that supports a cover mode
For Android, My suggestion is to use 9-patch Image instead of normal image. It will stretch it self to entire Screen, if you placed image as background
of Window it self as well as worked for ImageView perfectly.
Thank you.

Is there a good way to consistently position a TextView on an ImageView?

I have an ImageView, which is intended to take up the whole screen if it matters, and I intend to put several labels on it with TextViews. I need these labels to correspond to a precise position on the image.
I tried setting the margins in xml, but that won't work because the dp won't convert between devices. I am aware that I can set positions as a percentage of the screen size programatically but this poses a few problems:
I don't want to solve this problem programatically.
The aspect ratio of the image is being maintained, so on most screens there will be a little bit of white space given that the screen is a different shape from the image. Also, the app is not full screen, so the notification bar takes up space. This white space and the notification bar's space makes a percentage of the screen different from a percentage of the image.
How can i position TextView's on an ImageView in such a way that they will be in exactly the same spot (relative to the image) on every device?
I ended up figuring it out myself:
text.setX((int) (img.getX()+(img.getWidth()*pct)/100)
and the same for y, where text is the TextView, img is the ImageView, and pct is the location as a percentage of the image. Another problem is that this can't be called in onCreate(). I called it in onWindowFocusChanged().
To prevent your ImageView from being a different shape from the Image, which would break this solution, you can do:
android:adjustViewBounds="true"
and be sure to set at least one of the dimensions as wrap_content.

loading an image, is the width/height pixel count maintained

I have read all about supporting different screens and I understand the concept of Density Pixels (DP). However one thing I am not sure about. Lets say I have an image file which is 200 by 200 pixels and I loaded it using xml with wrap_content attributes at the top left of the screen (0,0). Now let's say I want to place a textview programmatically beside it.
If I put the text view at coordinates (250,0). Does that mean that the text view will never overlap the image and will always be to the right no matter what the device density/screen size is.
I understand the distance ( gap) will be different but I am hoping my theory of not overlapping will hold.
I tried it on 2 device and no issues but I am not sure if this is coincendence
Thanks
If you are using px unit it will always remain the same. But if you are using dp unit instead of pixel you cannot definitely be sure.
By the way you should use toRightOf property instead of this approach if you only want this.

Is there a way to allow an image to extend past the boundary of the window?

I'm trying (and not finding) a way to have an image that acts as a background, which is larger than the the screen, to not squish and instead to just flow off the right and bottom of the screen.
In other words, I have a background image that is, say, 3000x3000 pixels.
I want it to originate at 0x0 and then rather than being squished into whatever the resolution of the device is, to just get naturally cropped by said resolution, allowing the excess to flow off the sides.
I've tried every combination of wrapping content and scaleType(ing) that I can think of and none of them produce the intended result. Is there a way to do this? Or am I just out of luck?
setScaleType to MATRIX and call setClipChildren with false on the parent ViewGroup container

How to design ImageViews that stretch according to the screen-size

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.

Categories

Resources