How to make an image fit to various screen sizes on Android?

Asked

Viewed 3,188 times

0

I’m using a button with background image

                   <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/btn"
                    android:background="@drawable/myimage" />

I wanted this image to fit several screen sizes, an example, would it be possible to do the same unity where one creates a canvas and everything on canvas will automatically adjust from resolution passed by reference?

  • You need to create various image sizes according to the main screen densities, there is no way to put a single image and wait for android to do the rest. See on documentation the table of densities and additional tips.

  • friend if you are using android studio to create your app just you use instead of this

3 answers

1

If you’re using Android Studio to create your app, replace android:background="@drawable/myimage" />

for

android:background="@mipmap/myimage" />

That folder mipmap Android Studio maps to multiple folders on your project file system.

Where you saved the image on drawable, you have to save the image in mipmap:

inserir a descrição da imagem aqui

or save directly to your app folder:

inserir a descrição da imagem aqui

Do one of these two steps, and Android will take care to adjust your image automatically.

0

I think you may be looking for the scaleType attribute of imageview, it is possible to set how the image will behave within the canvas itself, whether it will be cropped or not.

if this is so and it is not possible to use the imageview instead of your button, you can generate a drawable with the lib Glide and set as background on your button.

Glide is a famous lib for treating and caching images on android. there are also some auxiliary libs for it, such as Glide Transformations that allows you to apply a number of transformations to any image.

https://github.com/bumptech/glide

https://github.com/wasabeef/glide-transformations

0

creates a photo with the same name for each resolution. create a folder inside the drawable folder for each resolution ( name the folders of: hdpi or ldpi or mdpi or xhdpi or xxhdpi ). place the photos inside those folders -- that are already in drawawable. android will take care of change at the time of execution as mobile model.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.