我有多个drawable,想将其组合成一个drawable(例如,将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()
)。
BitmapDrawable
。 - Cristian