2012-07-04 39 views
8

我有多个可绘图并希望将其合并为一个可绘制的(例如,4个方格创建一个大方形,如Windows徽标:))。我怎样才能做到这一点?合并多个可绘制的图案

+1

以下是[可绘制资源](http://developer.android.com/guide/topics/resources/drawable-resource.html)上的开发人员指南。它讨论这是与图片和可运行的代码很好的细节。 – Sam

回答

14

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

我甚至没有编译上面的代码;我只是告诉你如何做到这一点。我也假设你有相同尺寸的方形drawable。请注意,名称为big的位图可以在任何需要的地方使用(例如ImageView.setImageBitmap())。

+0

谢谢,我该如何使用这个画布创建drawable? – arts777

+0

你是什么意思的drawable?一个'Drawable'类实例?如果是这样,你可以使用'BitmapDrawable'。请尝试更具体。 – Cristian

+0

对不起,当然可绘制类。 4个输入可绘制实例和一个输出。 – arts777