![](/img/trans.png)
[英]How to set property “android:drawableTop” of a button at runtime
[英]How to set drawable(with changed size) in button like android:drawableTop at Runtime
我从服务器获取与其相关的图像和文本的数量现在我想在LinearLayout中设置每个图像的文本(在底部)
我从这里得到了问题第二部分的答案
button.setCompoundDrawables(left, top, right, bottom);
但问题是我得到了不同大小的图像,并希望调整它们的大小
我成功通过使用布局参数调整按钮的大小,但是
setCompoundDrawable(left,top,right,bottom);
图像没有调整大小
我怎么能实现这个?
我希望下面的代码对你有用因为它的工作正常
Bitmap bitmap = ImageResizeUtility.resizeBitmap(bitmap, 100, 110);
使用此类可以调整图像大小
public class ImageResizeUtility
{
public static Bitmap resizeBitmap(final Bitmap bitmap, final int width, final int height)
{
final int oldWidth = bitmap.getWidth();
final int oldHeight = bitmap.getHeight();
final int newWidth = width;
final int newHeight = height;
// calculate the scale
final float scaleWidth = ((float) newWidth) / oldWidth;
final float scaleHeight = ((float) newHeight) / oldHeight;
// create a matrix for the manipulation
final Matrix matrix = new Matrix();
// resize the Bitmap
matrix.postScale(scaleWidth, scaleHeight);
// if you want to rotate the Bitmap
// recreate the new Bitmap
final Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, oldWidth, oldHeight, matrix, true);
return resizedBitmap;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.