合并多个可绘制对象

10

我有多个drawable,想将其组合成一个drawable(例如,将4个正方形组合成一个大正方形,就像Windows徽标一样 :))。我该如何实现?


1
这是有关可绘制资源的开发人员指南。它详细讨论了此内容,并提供了图片和可运行代码。 - Sam
2个回答

16

您可以使用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())。


谢谢,我该如何使用这个画布创建可绘制对象? - artem
“drawable”是什么意思?是指“Drawable”类的实例吗?如果是,您可以使用“BitmapDrawable”。请尽量更具体。 - Cristian
对不起,当然是Drawable类。4个输入的Drawable实例和一个输出。 - artem
是的,那么您可以使用BitmapDrawable - Cristian

8

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接