在带有文本的按钮中缩放drawableLeft

MBo*_*ber 6 scaling android button drawable

我有一个带文字的按钮和左边的抽屉.有没有什么方法可以在保持纵横比的同时将可绘制比例调整到合适的尺寸(填充按钮的高度)?

我的布局相关摘录:

        <Button 
            android:text="@string/add_fav"
            android:id="@+id/event_fav_button"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:drawableLeft="@drawable/star_yellow"/>  
Run Code Online (Sandbox Code Playgroud)

Imr*_*ana 2

您可以根据按钮的尺寸调整图像大小,使用getHeight()getWidth()方法来获取按钮的大小,并使用以下函数调整按钮的图像大小:

调整位图大小:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {

int width = bm.getWidth();

int height = bm.getHeight();

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);

// recreate the new Bitmap

Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);

return resizedBitmap;

}
Run Code Online (Sandbox Code Playgroud)

现在使用这个调整大小的图像作为您的按钮。 参考