0

我有 45 个按钮,我在 java 代码中通过循环创建。现在我需要根据用户交互设置和更改背景图像,还需要根据按钮调整图像大小。如果我可以为 java 中的图像按钮做所有事情,这对我很有帮助。

我如何在java代码中调整按钮的背景图像。

4

2 回答 2

0

将包含图像和要用于每个按钮的数据的数组存储起来,并在循环初始化 ImageButtons 时使用它们。

发布一些代码以获得详细帮助。

于 2012-05-20T08:36:40.797 回答
0

如果要根据 Button 的尺寸调整图像大小,请使用getHeight()getWidth()方法获取按钮的大小,并使用以下函数调整 Button 的图像大小:

调整位图大小:

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;

}

现在您可以使用Button 对象上的setBackgroundDrawable()orsetBackgroundResource()方法来更改其背景图像,并且您可以通过setHeight()setWidth()方法调整 Button 的大小。 参考

于 2012-05-20T10:08:01.307 回答