在Android中的单个布局中拖动多个ImageView无法正常工作。

时间:2017-02-15 07:12:43

标签: android

我正在开发一个Android应用程序,在我的应用程序中,我动态创建一些ImageViews并将ImageViews设置为LinearLayout.So我想独立地拖动布局中的每个图像。我写了一个代码来拖动图像。但它不能正常工作,当我拖动一个图像时,布局中的所有其他图像也会被移动。我想独立移动每个图像。所以任何人都知道请帮助我。

这是我的 setCanvasImage()方法

public void setCanvasImage() {

    final int imgCount = dbobj.getFromTemp();
    canvasImage = new ImageView[imgCount];
    imglayout = (LinearLayout) findViewById(R.id.canvas);

    final String[] strImage = dbobj.getdImage();

    imglayout.removeAllViews();


    for (int i = 0; i < imgCount; i++) {

        int posLeft = 50;
        int posTop = 50;

        canvasImage[i] = new ImageView(this);
        boolean tabletSize = getResources().getBoolean(R.bool.isTablet);

        if (tabletSize) {

            int imgWidth = 150;
            int imgHeight = 150;

            LinearLayout.LayoutParams paramss = new LinearLayout.LayoutParams(imgWidth, imgHeight);
            canvasImage[i].setLayoutParams(paramss);

            canvasImage[i].setScaleType(ScaleType.FIT_XY);
            canvasImage[i].setPadding(posLeft, posTop, 0, 0);
            canvasImage[i].setTag(strImage[i]);
            setImage(strImage[i], canvasImage[i]);
            imglayout.addView(canvasImage[i]);


        } else {

            int imgWidth = 100;
            int imgHeight = 100;
            LinearLayout.LayoutParams paramss = new LinearLayout.LayoutParams(imgWidth, imgHeight);
            canvasImage[i].setLayoutParams(paramss);

            canvasImage[i].setPadding(posLeft, posTop, 0, 0);
            canvasImage[i].setTag(strImage[i]);
            setImage(strImage[i], canvasImage[i]);
            imglayout.addView(canvasImage[i]);
        }

    }
    //}
    for (int i = 0; i < imgCount; i++) {

        canvasImage[i].setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {



                final int x = (int) motionEvent.getRawX();
                final int y = (int) motionEvent.getRawY();

                switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {

                    case MotionEvent.ACTION_DOWN:

                        LinearLayout.LayoutParams lparams = (LinearLayout.LayoutParams) view.getLayoutParams();
                        xDelta = x - lparams.leftMargin;
                        yDelta = y - lparams.topMargin;
                        break;
                    case MotionEvent.ACTION_UP:

                        break;
                    case MotionEvent.ACTION_MOVE:
                        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
                        layoutParams.leftMargin = x - xDelta;
                        layoutParams.topMargin = y - yDelta;
                        layoutParams.rightMargin = 0;
                        layoutParams.bottomMargin = 0;
                        view.setLayoutParams(layoutParams);
                        break;
                }
                mainLayout.invalidate();
                return true;
            }
        });
    }
}

0 个答案:

没有答案