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

标签 android android-maps

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

图像确实按比例移动,所以我认为问题出在我在 ACTION_DOWN 部分中定义的 offset_xoffset_y 上。然后,在 ACTION_UP 中,我需要在手指下的正确坐标上 createMarker(x,y),但这也有错误的偏移。

我尝试了各种方法让它居中,有些方法比其他方法好。到目前为止,我一直无法在不使用魔数(Magic Number)的情况下使其工作。

实际上,我使用的是点击位置并减去图片尺寸的一半。这对我来说很有意义。如果我减去整个图片的大小,它会更接近正确。我尝试了网络上的各种示例,所有示例都存在 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;
    }

});

最佳答案

您已在 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);

尝试将它写在 if 条件之外,以确保它应用于所有触摸事件。

关于Android 2.2 在触摸下点击并拖动图像居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8381285/

相关文章:

java - 无法将 map 加载到 Android(三星)手机中

android - android map 是否支持地面覆盖?

fragment 内的 Android map

android - Cocos2d-x 与 Visual Studio 2015 跨平台工具

android - 尝试调用虚拟方法 'null object reference'

android - 在 Android 中创建上下文菜单

android - 禁用布局宽度和高度自动导入功能和建议

Android M Google API 的使用谷歌地图和其他权限

android - 在 Mac 上编译 ffmpeg for android

Android 为谷歌地图上的标记设置监听器