0

Canvas- および Bitmap クラスを使用して画像を作成しています。ユーザーの背景として設定したい。次に、その上にさらに画像を追加したいと思います。

これは、背景として想定されている画像のコードです。

ImageView imgMap1 = (ImageView) findViewById(R.id.imgMap1);
imgMap1.setImageDrawable(new BitmapDrawable(Bitmap.createBitmap(bmp, 0, 0, 500, 500)));

これは、背景として作成するコードです。

LinearLayout ll = new LinearLayout(this);
ll.setBackgroundResource(R.drawable.nn);
this.setContentView(ll);

ここでの問題は次のとおりです。背景として設定すると、他の写真が表示されなくなります。これどうやってするの?前もって感謝します。

他の画像がレイアウトに追加されます。それらは指のタッチで移動できます。ユーザーは指でそれらを再配置できます。

ImageView i1 = (ImageView) findViewById(R.id.Image1);
    ImageView i2 = (ImageView) findViewById(R.id.Image2);
4

2 に答える 2

0

Androidリファレンスを参照して、コンテンツBitmapとして直接設定できることに気づきました。ImageViewsetImageBitmap(Bitmap bm)

次に、あなたの質問について話しましょう。

まず、独自のクラスを作成してView;を拡張します。次に、ビットマップを使用して背景画像とオーバーレイ画像を読み込みます。3番目に、onTouchイベントを呼び出します。これにより、onDrawメソッドは、によって返された座標を使用してオーバーレイ画像を自動的に再描画します。onTouch

何かのようなもの:

public class dragndrop extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {

    // using this to load your background image
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inPreferredConfig = Bitmap.Config.RGB_565;  // this is not a must       
    bmBackground = BitmapFactory.decodeFile(filePath, opt);
    super.onCreate(savedInstanceState);
    DrawView dv = new DrawView(dragndrop.this);
    setContentView(dv);
}


public class DrawView extends View {

    Point coordinate;

    public DrawView(Context context) {
        super(context);
        setFocusable(true); //necessary for getting the touch events
    }

    @Override 
    protected void onDraw(Canvas canvas) {

        // assume you have already load your background image as bitmap
        canvas.drawBitmap(bmBackground, 0, 0, null);

    // assume bm is the overlay image you need to put on top,
        // the method here will draw the object with the coordinate you give
    canvas.drawBitmap(bm, coordinate.x, coordinate.y, null);
    }



   // events when touching the screen
    public boolean onTouchEvent(MotionEvent event) {
        int eventaction = event.getAction(); 

        int x = (int)event.getX(); 
        int y = (int)event.getY(); 

        switch (eventaction ) { 

        case MotionEvent.ACTION_DOWN: 
        // add code here to determine the point user touched is within the object or not

                    break;
                }
              }

             break; 


        case MotionEvent.ACTION_MOVE:   // touch 'n' drag 

            // pass the touch point to your object
            coordinate.x = x;
            coordinate.y = y;

            break; 

        case MotionEvent.ACTION_UP: 
            // touch drop - just do things here after dropping

             break; 
        } 
        // redraw the canvas
        invalidate(); 
        return true; 

    }


}


}

これは私自身のスニペットです。使用する前に編集してください。質問が解決したらお知らせください。

于 2012-09-26T20:17:46.453 に答える
0

レイアウトは、XML ファイルで上から下に構築されるか、コードに要素を追加した順序で構築されます。XMLファイルの上位要素として、またはコードの前に、他の画像が最初にレイアウトに追加されているようです。完全な回答を得るには、他のコードをコンテキストとして追加する必要があります。

于 2012-09-26T18:41:07.143 に答える