我对android开发很陌生,请给我启发
如何通过拖放在图形屏幕中的特定位置移动按钮,文本视图等小部件。

提前致谢

阿克沙特

最佳答案

尝试以下代码:-

int x_cord, y_cord;
RelativeLayout.LayoutParams layoutParams1;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        if (v == mImageViewEye1) {
            System.out.println("Eye1 is Clicked...");
            layoutParams1 = (RelativeLayout.LayoutParams) mImageViewEye1
                    .getLayoutParams();
            switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                x_cord = (int) event.getRawX();
                y_cord = (int) event.getRawY();
                scaleimage(layoutParams1);
                layoutParams1.topMargin = y_cord - 75;
                mImageViewEye1.setLayoutParams(layoutParams1);
                break;
            default:
                break;
            }
        }
        return true;
    }

    public void scaleimage(RelativeLayout.LayoutParams layoutParams) {
        if (x_cord > windowwidth) {
            x_cord = windowwidth;
        }
        if (y_cord > windowheight) {
            y_cord = windowheight;
        }
        layoutParams.leftMargin = x_cord - 25;
        layoutParams.topMargin = y_cord - 75;
    }


如果您有任何查询,请告诉我。

10-08 18:52