Algo muy recurrido en nuestras aplicaciones, ya no android, sino en todos los lenguajes que utilicemos siempre es el escalado de imágenes.
En el día a día de un programador se tratan imágenes de todos los tamaños y tenemos que adaptarlas a nuestras necesidades, esta función para nuestras aplicaciones de android nos va a resolver el día en mas de una ocasión.
public static Drawable resizeImage(Context ctx, int resId, int w, int h) {
// cargamos la imagen de origen
Bitmap BitmapOrg = BitmapFactory.decodeResource(ctx.getResources(),
resId);
int width = BitmapOrg.getWidth();
int height = BitmapOrg.getHeight();
int newWidth = w;
int newHeight = h;
// calculamos el escalado de la imagen destino
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// para poder manipular la imagen
// debemos crear una matriz
Matrix matrix = new Matrix();
// resize the Bitmap
matrix.postScale(scaleWidth, scaleHeight);
// volvemos a crear la imagen con los nuevos valores
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0,
width, height, matrix, true);
// si queremos poder mostrar nuestra imagen tenemos que crear un
// objeto drawable y así asignarlo a un botón, imageview...
return new BitmapDrawable(resizedBitmap);
}
VN:F [1.9.20_1166]
Rating: 7.5/10 (12 votes cast)