Android ImageView scaleType - android

I have an image to be used a background in activity:
I want this image to fit screen by its height.
It means that for wide-screen smartphones, I want my image to be fit by height and centered:
and for square-screen smartphones, I want my image to be cut:
I use ImageView for the image. What scaleType should I use? Looking at the second figure, I'd say to use android:scaleType="centerInside". But looking at the third, I'd say to use android:scaleType="centerCrop". What is correct?

Your evaluation of the different scaleType's is correct. If you want the whole image to be visible, use "centerInside", or if you want to fill the whole view then use centerCrop.
To use a mix of both, you can set the scaleType in your onCreate() method. Based on the behavior you want to have, you can check the orientation or size of the screen and set the appropriate choice.
imageView.setScaleType(ScaleType.CENTER_INSIDE); // or ScaleType.CENTER_CROP

You can have two layouts, one for each of your configurations. You can then load the proper one at the activity's onCreate() call.

When you fit one side of image to background, you will face with two problems, first one is screen width is bigger than image's width or screen height is bigger than image height.So you will have empty space.
If you do not want to face with resolution problem and you want to fit both side of image to background, you need to use centerCROP.But as i said, if one side of image is not enough to fit background, image gets bigger till it will be filled.

Use centerCrop because centerInside doesn't scale an image in a view and you have to create the image with appropriate height to achieve wide-screened smartphones background filling. Or alternative you could use fitCenter to get uniformly scaled image by both axes which fills the all background.

Related

How to set background image scale type of a theme?

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.

Round shaped image is elongated horizontally or vertically

I have to display a splash image which has a round shaped object (a Ball). The Layout for splash is a simple linear layout with just a single Image view to occupy the full screen.
Image : single image with the size of 1280 x 720.
When my splash screen is shown in the App, The round object is shown in different shape in different screen sizes. I hope the aspect ratio and the resolution is the cause for these elongated images.
Could you please suggest an idea / approach to solve this ?
Do I need to consider the aspect ratio or the resolution or both ?
Finally the ball should look like a ball in all the devices :)
Thanks in Advance.
1) Yes, by default Android will scale your image down to fit the ImageView, maintaining the aspect ratio. However, make sure you're setting the image to the ImageView using android:src="..." rather than android:background="...". src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly to the size of the ImageView. (You can use a background and a source at the same time though, which can be useful for things like displaying a frame around the main image, using just one ImageView.)
2)You should also see android:adjustViewBounds to make the ImageView resize itself to fit the rescaled image. For example, if you have a rectangular image in what would normally be a square ImageView, adjustViewBounds=true will make it resize the ImageView to be rectangular as well. This then affects how other Views are laid out around the ImageView.
You can change the way it default scales images using the android:scaleType parameter. By the way, the easiest way to discover how this works would simply have been to experiment a bit yourself! Just remember to look at the layouts in the emulator itself (or an actual phone) as the preview in Eclipse is usually wrong.
Reference : How to scale an Image in ImageView to keep the aspect ratio
set imageView property
scaleType="centerInside"
Add scaled versions of the image with the same file name under folders 'res->drawable','res->drawable-ldpi','res->drawable-hdpi' and under xhdpi "http://developer.android.com/guide/practices/screens_support.html#DesigningResources"

Android: make ImageButton's image larger

I have an ImageButton:
I want to make the image (circular play image) bigger. I am aware of android:scaleType, but I don't want to stretch the image all the way to the borders of the button. I just want to make it a little bigger, say 120% of the current size.
Is that possible?
Edit your image in photoshope to make it bigger in your desire resolution.
Or
In android put height , weight of your image and set scaleType:FitCenter
In the xml layout set the width and height to be a larger amount of dip.

Tying images to one another and scaling?

I have an application that needs to scale an image in relation to another image due to different screen size, I have an image that is essentially a background that matches the width of the screen and another image in a relative layout that is aligned so it fits on my phone in 2.2 but when seen on 2.1
Or ... i could just use something that scales the image as a percentage. Suggestions?
edit:
I guess all i need is to know how i can keep an image in a spot relative to another using relative layout and scale it through a percentage, different screen size will scale the other image but not this one because the other image fills the parent so when the screen size is smaller it just scales it down, however the one i need to scale does not because it doesn not fill the parent.

Android: scaleType expand to screen

I need to resize several images inside ImageView to screen size.
Currently I use scaleType="centerInside", it works good for bigger images to scale them down to screen size but doesn't expand small images.
All other suggested options crop or deform image. Is there any way to expand images (keeping ratio) via XML or do I have to do it manually in code?
You will be limited in how much you can do from the XML alone. The simplest way to expand a small drawable is to set android:layout_width and android:layout_height properties to what is desired (using density independent pixels dip). android:layout_width="320dip" will expand you to the full width of the screen (in portrait view).
If your images are photos or something with varying aspect ratios, then your only choice may be to inflate the view from code (after determining the correct size).
Just try scaleType="fitXY" . It Will enlarge or shrink the image exactly to the size of image view
Use scaleType="fitCenter" instead.
Try fitStart // Scale the image using START.
android:scaleType
http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType

Categories

Resources