如何设置新创建的图像的位置

时间:2014-03-05 07:16:53

标签: android imageview ontouchevent ontouchlistener

在我的应用程序中,我有多个图像并排。我可以在触摸特定图像时创建新图像,但新创建的图像始终仅在第一个位置添加。如何在触摸位置添加新创建的图像。以下是我的代码

@Override
    public boolean onTouch(View view, MotionEvent me) {
        params = new RelativeLayout.LayoutParams(new ViewGroup.MarginLayoutParams(100,100));
        if (me.getAction() == MotionEvent.ACTION_DOWN) {
            status = START_DRAGGING;
            m_lastTouchX = me.getX();
            m_lastTouchY = me.getY();
            imageView = new ImageView(this);
            imageView.setImageBitmap(view.getDrawingCache());

            layout.addView(imageView,params);
        }
        if (me.getAction() == MotionEvent.ACTION_UP) {
            status = STOP_DRAGGING;
            Log.i("Drag", "Stopped Dragging");
        } else if (me.getAction() == MotionEvent.ACTION_MOVE) {
            if (status == START_DRAGGING) {
                System.out.println("Dragging");
                m_dx = me.getX() - m_lastTouchX;
                m_dy = me.getY() - m_lastTouchY;
                m_posX = m_prevX + m_dx;
                m_posY = m_prevY + m_dy;
                params.leftMargin = (int) m_posX;
                params.topMargin=(int) m_posY;
                imageView.bringToFront();
                imageView.setLayoutParams(params);
                imageView.invalidate();
            }
        }
        return false;
    }

0 个答案:

没有答案