Insertar imágenes en un proyecto de Android

Por 9.99€ al mes tendrás acceso completo a todos los cursos. Sin matrícula ni permanencia.

Tamaños de imágenes y layouts en función de la pantalla dónde serán reproducidos

res/drawable-mdpi/my_icon.png   //bitmap for medium density
res/drawable-hdpi/my_icon.png   //bitmap for high density
res/drawable-xhdpi/my_icon.png   //bitmap for extra high density

res/layout/my_layout.xml    //layout for normal screen size (“default”)
res/layout-small/my_layout.xml   //layout for small screen size
res/layout-large/my_layout.xml   //layout for large screen size
res/layout-xlarge/my_layout.xml   //layout for extra large screen size
res/layout-xlarge-land/my_layout.xml  //layout for extra large in landscape orientation

Ponemos los recursos en estas carpetas y será el sistema el que decida cuál utilizar.

Ejercicio: Cargar una imagen

Debemos copiar la imagen en la carpeta drawable-hdpi.

Cargar la imagen en un ImageView:
Introducimos un objeto de tipo ImageView en el xml y lo vinculamos a la imagen guardada en drawable.

Cargar la imagen en un ImageButton:
Introducimos un elemento de tipo ImageButton en el xml y lo vinculamos a la imagen guardada en drawable.

Cambiar dinámicamente la foto del ImageView anterior:

ImageView iv = findViewById(R.id.imageView1);
iv.setImageResource(R.drawable.cara);

Cargar imagen con esquinas redondeadas

ImageView iv = findViewById(R.id.miFoto);

Bitmap batmapBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aperturaabductores);
RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), batmapBitmap);

// option 1 h/t [Chris Banes](https://chris.banes.me/)
circularBitmapDrawable.setCornerRadius(batmapBitmap.getWidth());

// option 2 h/t @csorgod in the comments
circularBitmapDrawable.setCircular(true);


iv.setImageDrawable(circularBitmapDrawable);
Insertar imágenes en un proyecto de Android 1

Background repeat

Por defecto una imagen se deformará para ocupar las dimensiones del ImageView, en el caso de que la imagen sea mas pequeña. Si lo que queremos es que la imagen se repita:

activity_main.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/background_repeat"
...
background_repeat.xml<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
   android:src="@drawable/icon"
   android:tileMode="repeat"
    />

Por 9.99€ al mes tendrás acceso completo a todos los cursos. Sin matrícula ni permanencia.