【发布时间】:2012-07-04 23:31:52
【问题描述】:
我有多个可绘制对象,并希望将其组合成一个可绘制对象(例如,4 个正方形来创建一个大正方形,如 Windows 徽标 :))。我该怎么做?
【问题讨论】:
-
这是Drawable Resources 上的开发人员指南。它通过图片和可运行代码讨论了这一点。
我有多个可绘制对象,并希望将其组合成一个可绘制对象(例如,4 个正方形来创建一个大正方形,如 Windows 徽标 :))。我该怎么做?
【问题讨论】:
您可以使用TableLayout 或一些LinearLayouts 来做到这一点。但是,如果您想要创建单个图像以在 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())。
【讨论】:
Drawable 类实例?如果是这样,您可以使用BitmapDrawable。请尝试更具体。
BitmapDrawable。
您可以使用LayerDrawable 来做这件事。
【讨论】: