简体   繁体   中英

How to make an image adjust to its screen size

How do I adjust my player image to fit any screen? Like if its on a smaller screen it shrinks the image or if its on a larger screen the image gets larger.

Here's the code I have and which doesn't work:

player.setScaleX( player.getContentSize().width / winSize.width );
player.setScaleY( player.getContentSize().height / winSize.height);

Assuming the dimensions of the parent view are density independent, you can use imageView.setScaleType to either CENTER_CROP or CENTER_INSIDE to have the images scaled.

http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

EDIT** If you just want the image to fit the whole screen, then make the imageview fill_parent for both width and height and then set the image of that imageview to use the appropriate scale type. Not sure why you need your own algorithms..

Edit***

That's a standard issue; supporting multiple screen sizes and scaling appropriately. The basic idea is to avoid specifying actual pixels and, instead, to use density independent pixels. See this: http://developer.android.com/guide/practices/screens_support.html and this: http://developer.android.com/guide/practices/tablets-and-handsets.html

Generally it comes down to using "dp" units in xml and in code to specify the units (for text sizes and widths/heights, etc) as whatever your number is TIMES getResources().getDisplayMetrics().density which will apply the appropriate scaling factor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM