我有一个带有文本的按钮,左侧有一个可绘制的按钮。有什么方法可以使可绘制比例达到适当的尺寸(填充按钮的高度),同时保持其纵横比?
我的布局的相关摘录:
<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"/>
您可以根据按钮的尺寸调整图像大小,使用getHeight()
and 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;
}
现在使用这个调整大小的图像作为您的按钮。参考 http://thinkandroid.wordpress.com/2009/12/25/resizing-a-bitmap/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)