Android: how to set the height/width of the src image of a ImageButton?

前端 未结 5 1304
野的像风
野的像风 2021-02-13 02:14

Android: how to set the height/width of the image(src image intead of the background) of a ImageButton?

I want to set the height/width of the top level image(not the bac

相关标签:
5条回答
  • 2021-02-13 02:34

    I had same problem... change the size of 'src' image of image button(not the size of button just image only).

    what i did--

    I moved those '.png' file from 'drawable' folder to 'drawable-hdpi' folder.

    weird... but it worked.

    thanks,

    0 讨论(0)
  • 2021-02-13 02:41

    set padding and scaletype to centerInside

    Padding will help you customize the source image to provide the required height and width.

     <ImageButton
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/location"
                android:padding="10dp"
                android:scaleType="centerInside"
                android:background="@drawable/blue_circle_border"/>
    
    0 讨论(0)
  • 2021-02-13 02:42

    You can use Bitmap.createScaledBitmap to scale the image to the size you want.

    Bitmap image = ... //load image from your source
    image = Bitmap.createScaledBitmap(image, desiredHeight, desiredWidth, true);
    
    0 讨论(0)
  • 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: (java code)

    // 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
                )
        );
    
    0 讨论(0)
  • 2021-02-13 02:50

    You can just Use the scaleType attribute of ImageButton. Set it to fitXY or fitCenter or anything else according to your need.

    like this

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:scaleType="fitCenter"
        android:src="@drawable/some_image">
    </ImageButton>
    
    0 讨论(0)
提交回复
热议问题