【问题标题】:Combine multiple drawables组合多个drawable
【发布时间】:2012-07-04 23:31:52
【问题描述】:

我有多个可绘制对象,并希望将其组合成一个可绘制对象(例如,4 个正方形来创建一个大正方形,如 Windows 徽标 :))。我该怎么做?

【问题讨论】:

  • 这是Drawable Resources 上的开发人员指南。它通过图片和可运行代码讨论了这一点。

标签: android drawable


【解决方案1】:

您可以使用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 是什么意思? Drawable 类实例?如果是这样,您可以使用BitmapDrawable。请尝试更具体。
  • 对不起,当然是 Drawable 类。 4 个输入 Drawable 实例和一个输出。
  • 是的,那么你可以使用BitmapDrawable
【解决方案2】:

您可以使用LayerDrawable 来做这件事。

【讨论】:

    猜你喜欢
    • 2014-11-12
    • 2015-11-07
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2016-10-28
    相关资源
    最近更新 更多