0

我在线性布局中有两个图像视图水平。现在我想一次移动一个图像视图。但是当我移动一个图像视图时,第二个图像视图的位置也在变化。我希望一次只有一个图像应该移动。

private void addViewDynamically() {
        for (int i = 0; i < mTxtViewArray.length; i++) {
            mTxtViewArray[i] = new TextView(getApplicationContext());
            mViewGrp[i] = new LinearLayout(getApplicationContext());
            mTxtViewArray[i].setLayoutParams(new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            mTxtViewArray[i].setText("text " + i);

            mTxtViewArray[i].setOnTouchListener(this);
            mViewGrp[i].addView(mTxtViewArray[i]);



             mainLayout.addView(mViewGrp[i]);
        }
    }

    private void initialize() {     
        mTxtViewArray = new TextView[5];
        mViewGrp = new LinearLayout[5];
        width = getWindowManager().getDefaultDisplay().getWidth();
        height = getWindowManager().getDefaultDisplay().getHeight();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mBeforeMoveParams = (LinearLayout.LayoutParams) v.getLayoutParams();

        switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:
            x_offset = (int) event.getX();
            y_offset = (int) event.getY();

            break;
        case MotionEvent.ACTION_MOVE:

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

            if (x > width) {
                x = width;
            }
            if (y > height) {
                y = height;
            }

            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) v
                    .getLayoutParams();
            int left = lp.leftMargin + (x - x_offset);
            int top = lp.topMargin + (y - y_offset);

            lp.leftMargin = left;
            lp.topMargin = top;

            v.setLayoutParams(lp);

            break;
        case MotionEvent.ACTION_UP:

            break;

        default:
            break;
        }
        return true;
    }
4

0 回答 0