2

サーフェイスビュー上の 2 つの単純なビットマップを指で移動したいと考えています。1 つのビットマップでは問題なく動作しますが、2 つのビットマップを処理できません。imageview onTouchListener で試してみましたが、API レベル 11 である View.setX(float x) を使用する必要がありました。API レベル 8 を超えたくないのですが、どうすれば実装できますか?

    public class ItemClass extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    private SurfaceHolder surface;
    private Paint p = new Paint();
    private Bitmap[] item = new Bitmap[2];
    private int[] coord = new int[2];

    public DrawClass(Context context) {
        super(context);
        getHolder().addCallback(this);
        surface = getHolder();
        item[0] = BitmapFactory.decodeResource(getResources(), R.drawable.img1);
        item[1] = BitmapFactory.decodeResource(getResources(), R.drawable.img2);        
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        Thread drawThread = new Thread(this);
        drawThread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {}

    @Override
    public void run() {

        Canvas c;
        while (true) {
            c = null;
            try {
                c = surface.lockCanvas(null);
                synchronized (surface) {
                    onDraw(c);

                }
            } finally {
                if (c != null) {
                    surface.unlockCanvasAndPost(c);
                }
            }
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        coord[0]=(int)event.getX();
        coord[1]=(int)event.getY();

        return true;
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLACK);

        canvas.drawBitmap(item[0], coord[0], coord[1], null);
        canvas.drawBitmap(item[1], 100, 100, null);
    }

}
4

1 に答える 1

0

わかりました。長方形のビットマップ(透明度なし)を使用していると仮定して、簡単な回答を試みます。

まず、両方のオブジェクトのx、y座標と幅、高さを保存する必要があります。現在のビットマップ配列Bitmap[]アイテムの代わりに、ビットマップのカスタムクラスを作成することをお勧めします。

private class MyImage{
            Bitmap image;
            int x,y,w,h;
        }
private MyImage[] images = new MyImage[2];

それに応じてコードを変更する必要があります。item [0] = BitmapFactory.decodeResource(getResources()、R.drawable.img1); -> images [0] .image = BitmapFactory.decodeResource(getResources()、R.drawable.img1);

coord [0]-> images [0] .x

coord [1]-> images [0] .y

次のような画像から幅と高さのプロパティを取得できます。

images[0].h = images[0].image.getHeight();
images[0].w = images[0].image.getWidth();

好みに応じて、これらの画像に固定のx、y値を指定するか、画面サイズ内でランダムな値を指定することができます。ここでMath.randomの使用方法の例を参照してください。Javaで特定の範囲内のランダムな整数を生成するにはどうすればよいですか?ここでサイズを表示します画面のサイズをピクセル単位で取得します

次に、onTouchEventは、x、yが画像の1つに含まれていて、はいであるかどうかを確認します。その画像のみを移動します。何かのようなもの:

for (int i = 0; i<=images.length; i++){
            //if touch event is inside this image - move it
            if (coord[0] >= (images[i].x) && coord[0] <= (images[i].x+images[i].w) &&
                    coord[1] >= (images[i].y) && coord[1] <= (images[i].y+images[i].h) ){
                //offset the image by the distance of the touch event from the center of the image
                images[i].x += coord[0] - (images[i].x+images[i].w/2);
                images[i].y += coord[1] - (images[i].y+images[i].h/2);
                canvas.drawBitmap(images[i].image, images[i].x, images[i].y, null);
            } else {
                //draw the iamge at its old position
                canvas.drawBitmap(images[i].image, images[i].x, images[i].y, null);
            }
        }

また、オブジェクトがオーバーラップする可能性があるかどうかも検討してください。オーバーラップしない場合は、それに応じて防止する必要があります。

お役に立てれば。

于 2012-10-05T03:12:36.623 に答える