我有多个可绘制对象,并希望将其组合到一个可绘制对象中(例如,用4个正方形创建一个大正方形,例如Windows徽标:))。我怎样才能做到这一点?

最佳答案

您可以使用TableLayout或一些LinearLayout来实现。但是,如果要在ImageView中创建要使用的单个图像,则必须手动创建Bitmap。这并不难:

Bitmap square1 = BitmapFactory.decodeResource(getResources(), R.drawable.square1);
Bitmap square2 = BitmapFactory.decodeResource(getResources(), R.drawable.square2);
Bitmap square3 = BitmapFactory.decodeResource(getResources(), R.drawable.square3);
Bitmap square4 = BitmapFactory.decodeResource(getResources(), R.drawable.square4);

Bitmap big = Bitmap.createBitmap(square1.getWidth() * 2, square1.getHeight() * 2, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(big);
canvas.drawBitmap(square1, 0, 0, null);
canvas.drawBitmap(square2, square1.getWidth(), 0, null);
canvas.drawBitmap(square3, 0, square1.getHeight(), null);
canvas.drawBitmap(square4, square1.getWidth(), square1.getHeight(), null);

我什至没有编译上面的代码。我只是向您展示如何做到这一点。我还假设您有所有具有相同尺寸的正方形可绘制对象。请注意,可以在任何需要的地方使用名为big的位图(例如ImageView.setImageBitmap())。

10-06 13:41