Android: cómo configurar la altura / anchura de la imagen src de un ImageButton?

Android: cómo configurar la altura / ancho de la imagen (src image intead del fondo) de un ImageButton?

Quiero establecer la altura / ancho de la imagen de nivel superior (no el fondo), el tamaño de la imagen debe ser personalizado en lugar de rellenar el botón automáticamente

Sólo puede utilizar el atributo ImageButton de ImageButton . Ajústelo en fitXY o en fitCenter o cualquier otra cosa según su necesidad.

Me gusta esto

 <ImageButton android:id="@+id/imageButton" android:layout_width="100dp" android:layout_height="100dp" android:scaleType="fitCenter" android:src="@drawable/some_image"> </ImageButton> 

Puede utilizar Bitmap.createScaledBitmap para escalar la imagen al tamaño que desee.

 Bitmap image = ... //load image from your source image = Bitmap.createScaledBitmap(image, desiredHeight, desiredWidth, true); 

Tuve el mismo problema … cambiar el tamaño de la imagen 'src' del botón de imagen (no el tamaño del botón sólo imagen).

lo que hice–

Moví los archivos '.png' de la carpeta 'drawable' a la carpeta 'drawable-hdpi'.

Extraño … pero funcionó.

Gracias,

Xml

 <ImageButton android:id="@+id/picture_id" android:layout_width="10dp" //here width with unit. 5 dp exemple android:layout_height="3dp" //here height with unit. 3 dp exemple android:src="@drawable/picture_name" /> 

EDIT: (código java)

 // load the origial BitMap (500 x 500 px) Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.android); int width = bitmapOrg.width(); int height = bitmapOrg.height(); int newWidth = 200; int newHeight = 200; // calculate the scale - in this case = 0.4f float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // create a matrix for the manipulation Matrix matrix = new Matrix(); // resize the bit map matrix.postScale(scaleWidth, scaleHeight); // rotate the Bitmap matrix.postRotate(45); // recreate the new Bitmap Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true); // make a Drawable from Bitmap to allow to set the BitMap // to the ImageView, ImageButton or what ever BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); ImageView imageView = new ImageView(this); // set the Drawable on the ImageView imageView.setImageDrawable(bmd); // center the Image imageView.setScaleType(ScaleType.CENTER); // add ImageView to the Layout linLayout.addView(imageView, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT ) ); 
  • ImageButton en Android
  • Imagebutton cambiar de forma programática?
  • Android Consigue las dimensiones de Image en ImageButton
  • ¿Cómo puedo cambiar las imágenes en un ImageButton en Android cuando uso un OnTouchListener?
  • ImageButton en Android con fondo transparente
  • ¿Cómo cambiar la transparencia del botón de imagen mediante programación?
  • Obtener recursos de un fragmento
  • Desactivar un ImageButton?
  • Cómo eliminar la imagen de fondo estándar de ImageButton?
  • Cómo obtener la imagen de un ImageButton?
  • cambiar la imagen del botón de imagen hasta que se presione
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.