I'm new to android programming and I was wondering about how to get a background image (ImageView) show a drawable with a correct size.
Let me explain well, let's imagine we have this image:
I want this image to get shown with this aspect ratio on all screens.
Since I don't want the image to get stretched, I would like to set manually how my image gets shown, maybe by modifying its scale with thirdy part software.
the problem with this image is that, in portrait mode, it's not guaranteed to see the ball, and so I want to crop the part of my image so that the ball gets shown on the bottom-right edge.
I know that I have to import my drawable for all resolutions, but simply importing does not help... Any suggestions?
EDIT:
I'm facing this problem with dynamic background ImageView, so I'm not going to user xml layout files.
Using scaleType attribute doesn't help for this, or at least not just by setting its value.
Related
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.
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 using a potrait photo as the background image of my application. but when the orientation changes, the background image is streched and ugly. I dont want the image to fit XY .. instead of that, I want it to be centerCropped .. remember , its not a background image of any linear layout. its the background of application(theme) .
I think this is possible, I've seen contact+ to do this in their app.. But I don't know how do they do it. Can anybody help me out please?
NOTE: I have used and gravity but it doesn't help.. The image is still fitting XY.not getting cropped to keep its aspect ratio with filling the window as well .
Is the background image of your app being set either by android:background="..." in your layout xml,
or programmatically by calling setBackgroundResource()?
If so, this sets the background resource of a View.
A View's background always stretches to fill up all of the View
width and height, losing its aspect ratio when layout dimensions change
such as when device orientation moves or
when the application runs in devices
with other screen aspect ratios. There is no way of configuring this
to happen otherwise.
Workarounds:
Convert your background to a Nine-Patch file, in order to the
stretching is
applied in selected areas of the image only. This may or may not fit well
to the design of your app.
Make your application detect changes in your layout. When it happens,
build a new background image on-the-fly keeping the aspect ratio of the original image, and set the View's background to the new image.
I've posted
a description on how to keep aspect ratio of View background image
using this technique; hopefully it can serve as a starting point.
I am working on an application in which I am using a frame layout. In the frame layout there are three classes as follows:
creating image
checking the pixel of the image the user touches
drawing the image
But when I am touching on the same color, it is giving me a different color value. If anyone wants to see my code then it is there. If you want to check which image is used by me then the images are as follows:
the second image which is used for getting the pixel:
Can any one tell me why it is not giving me correct value of the color on touch on the image
?
Edited1
my edited code of the imagein class is as follow [code after editing4
My guess is that you are making assumptions based on the the pixel coordinates of the image, and not translating the coordinates as they exist once Android resizes your art. You should see what Android thinks the image size is, after it's drawn to your View and make sure that the spot you're touching is the same spot that you're expecting to be touching.
I'm using PNG images with transparency in my game and no matter what I do the edges of them get all grainy looking.
I have tried setting the following in my activity with no luck:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_DITHER, WindowManager.LayoutParams.FLAG_DITHER);
Is there anything I can do in Photoshop or in my game to overcome this. Please see the image here:
It looks like your image is too low a resolution and the edges are not tidy, there is some white left over from when you deleted the background.
Open the image in photoshop, zoom right in and fix your edges. Probably best to redraw and resize again for the screen you are using.