Android 2.2点击并拖动触摸下居中的图像

Lea*_*Lea 9 android android-maps

我试图将十字准线拖放到一个MapView.我ImageView屏幕上有一个按钮.当我触摸它时,按钮消失并ImageView出现一个新的按钮.新ImageView的应该坐在我的手指中心,直到我在某处释放,但由于某种原因,偏移是不正确的.十字线出现在我触摸的右下方.

所以我相信这个问题是与图像不按比例移动offset_x和offset_y我定义的ACTION_DOWN部分.然后,ACTION_UP我需要createMarker(x,y)在我的手指下的正确坐标,但也错误地偏移.

我尝试了各种方法使其居中,有些方法比其他方法更好.到目前为止,我还没有使用魔术数字使其无法正常工作.

实际上,我使用点击位置并减去图片大小的一半.这对我来说很有意义.如果我减去整个图片大小,它就更接近正确.我已经尝试过网上的各种例子,所有这些例子都受到View位置不准确的影响.

你能给我一些建议吗?

crosshair = (ImageView)findViewById(R.id.crosshair);
frameLayout = (FrameLayout)findViewById(R.id.mapframe);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);
crosshairImage = BitmapFactory.decodeResource(getResources(), R.drawable.crosshair);
crosshair.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            dragStatus = START_DRAGGING;

            // hide the button and grab a mobile crosshair
            v.setVisibility(View.INVISIBLE);
            image = new ImageView(getBaseContext());
            image.setImageBitmap(crosshairImage);

            // set the image offsets to center the crosshair under my touch
            offset_x = (int)event.getRawX() - (int)(crosshairImage.getWidth()/2)  ;
            offset_y = (int)event.getRawY() - (int)(crosshairImage.getHeight()/2) ;

            // set the image location on the screen using padding
            image.setPadding(offset_x, offset_y, 0, 0);

            // add it to the screen so it shows up
            frameLayout.addView(image, params);
            frameLayout.invalidate();

            if(LOG_V) Log.v(TAG, "Pressed Crosshair");
            return true;
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if(dragStatus == START_DRAGGING) {
                dragStatus = STOP_DRAGGING;

                // place a marker on this spot
                makeMarker((int)event.getX() + offset_x, (int)event.getY() + offset_y); 

                // make the button visible again
                v.setVisibility(View.VISIBLE);

                // remove the mobile crosshair
                frameLayout.removeView(image);

                if(LOG_V) Log.v(TAG, "Dropped Crosshair");
                return true;
            }
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            if (dragStatus == START_DRAGGING) {
                // compute how far it has moved from 'start' offset
                int x =  (int)event.getX() + offset_x;
                int y = (int)event.getY() + offset_y; 

                // check that it's in bounds
                int w = getWindowManager().getDefaultDisplay().getWidth();
                int h = getWindowManager().getDefaultDisplay().getHeight();
                if(x > w)
                    x = w;
                if(y > h)
                    y = h;

                // set the padding to change the image loc
                image.setPadding(x, y, 0 , 0);

                // draw it
                image.invalidate();
                frameLayout.requestLayout();

                if(LOG_V) Log.v(TAG, "(" + offset_x + ", " + offset_y + ")");

                return true;
            }
        }               
        return false;
    }

});
Run Code Online (Sandbox Code Playgroud)

May*_*ore 0

您已经在touch_Down.

offset_x = (int)event.getRawX() - (int)(crosshairImage.getWidth()/2)  ;
offset_y = (int)event.getRawY() - (int)(crosshairImage.getHeight()/2) ;

// set the image location on the screen using padding
image.setPadding(offset_x, offset_y, 0, 0);
Run Code Online (Sandbox Code Playgroud)

尝试将其写入 if 条件之外,只是为了确保它适用于所有触摸事件。