2011-11-17 12 views
9

Android:ImageButtonの画像の高さ/幅(背景のsrc画像)を設定する方法は?Android:ImageButtonのsrcイメージの高さ/幅を設定する方法は?

Iは、画像のサイズはボタン自動的

+0

が重複する可能性:http://stackoverflow.com/questions/6899088/adjusting-the-size-of-an-imagebutton-in-android –

+2

私はの高さ/幅を設定したいです(背景ではない)画像の場合、ボタンを自動的にフルフィルにするのではなく、画像のサイズをカスタマイズする必要があります。 –

答えて

2

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コード)

// 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

ボタン全体の幅と高さを調整するだけで、src画像を調整したい。 –

+0

これはJavaコードで実行できます。 – damson

+0

ありがとう、それは私のために少し複雑です。私は別のアイコンを使って歩き回った。 –

6

あなたはBitmap.crを使用することができますeateScaledBitmapを使用してイメージを必要なサイズに拡大します。

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

[docs](https://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap)によると、サイズを逆にしたので、次のようにする必要があります: 'image = Bitmap.createScaledBitmap image、desiredWidth、desiredHeight、true); ' –

19

あなただけImageButtonscaleType属性を使用することができます。 fitXYまたはfitCenterまたは必要に応じてその他の値に設定します。

この

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

これは受け入れられる回答でなければなりません。 – MeetTitan

+1

これは質問に答えません。問題は 'ImageButton'の代わりに' android:src'のサイズを設定する方法です。例えば。 'ImageButton'は100dp x 100dp、srcイメージは35dp x 35dpです – Weizhi

3

ように私は、画像ボタン(ボタンのない大きさだけで画像のみ)の「SRC」の画像のサイズを変更...同じ問題を抱えていました。私は

をdid--何

私は、 '描画可能-hdpi' フォルダに '描画可能' フォルダからそれらの '.PNG' ファイルを移動しました。

変だけど...うまくいった。

おかげで、

関連する問題