2012-09-26 11 views
5

2つのImageViewがあります。 ImageView1は背景画像であり、ImageView2はより小さな画像である。 ImageView2の位置は、アプリケーションの途中です。Android:2つのオーバーレイ画像ビューを正しい位置のbmpに結合

ImageView2がImageView1の上にくるように、2つのImageViewをビットマップに結合したいと思います。

結合プロセスは正常に機能しますが、ImageView2は常にbmpファイルの左上隅にあります。

以下

は、私は、BMPを生成するために使用される私のコードです:ImageViewsはそれと同じ位置になるよう

ImageView iv = (ImageView)findViewById(R.id.imageView1); 
    ImageView iv2 = (ImageView)findViewById(R.id.imageView2); 

    File rootPath = new File(Environment.getExternalStorageDirectory(), "testmerge"); 

    if (!rootPath.exists()) { 
     rootPath.mkdirs(); 
    } 

    Toast.makeText(this, rootPath.getPath(), Toast.LENGTH_LONG).show(); 
    File dataFile = new File(rootPath, "picture.png"); 

    iv.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
    iv.layout(0, 0, iv.getMeasuredWidth(), iv.getMeasuredHeight()); 

    iv.setDrawingCacheEnabled(true); 
    Bitmap b1 = Bitmap.createBitmap(iv.getDrawingCache()); 
    iv.setDrawingCacheEnabled(false); 

    iv2.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
    iv2.layout(0, 0, iv2.getMeasuredWidth(), iv2.getMeasuredHeight()); 

    iv2.setDrawingCacheEnabled(true); 
    Bitmap b2 = Bitmap.createBitmap(iv2.getDrawingCache()); 
    iv2.setDrawingCacheEnabled(false);   

    Bitmap bmOverlay = Bitmap.createBitmap(b1.getWidth(), b1.getHeight(), b1.getConfig()); 
    Canvas canvas = new Canvas(bmOverlay); 
    Paint paint = new Paint(); 
    paint.setAntiAlias(true); 
    paint.setFilterBitmap(true); 
    paint.setDither(true); 

    canvas.drawBitmap(b1, 0, 0, null); 
    canvas.drawBitmap(b2, 0, 0, null); 

    try { 
     FileOutputStream out = new FileOutputStream(dataFile, false); 
     bmOverlay.compress(CompressFormat.PNG, 95, out); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

あなたはどのように私は、最終的なビットマップファイルの位置を調整することができます教えてもらえますアプリに表示しますか?

ありがとうございました。

+0

@Kintaroはこの回答をどのように達成しましたか? – Erum

答えて

3

FrameLayoutを作成し、その内に2つのImageViewを含めます。当然、第1の画像を第2の画像と重ね合わせることになる。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:id="@+id/main_image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/main" /> 

    <ImageView 
     android:id="@+id/overlay_image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/overlay" /> 

</FrameLayout> 

画像を中央に配置したり、他の方法で配置したりすることができます。

+0

返事をありがとう。私の質問は、最終ビットマップ(bmOverlay)には両方のimageViewsが含まれていますが、上部のimageViewsは常にビットマップの左上隅にあります。 framelayoutが私のために自動的に行うことを意味しますか(framelayoutからビットマップを作成するとき)? –

+0

フレームレイアウトに2人の子供を配置すると、コンテナmatch_parentを作成し、各子供に異なる重力を設定するなど、レイアウトアトリビュートを追加しない限り、それらは正確にオーバーレイされます。 –

+0

ありがとうございました!!!! –

関連する問題