10

複数のドローアブルがあり、それを1つのドローアブルに結合したいと思います(たとえば、Windowsロゴのように4つの正方形で1つの大きな正方形を作成します:))。これどうやってするの?

4

2 に答える 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())。

于 2012-07-04T23:47:02.947 に答える
8

LayerDrawableを使用して、これを正確に行うことができます。

于 2016-03-30T23:57:23.493 に答える