0

我有图像视图。ImageView 有图像。当我移动 imageView 时,图像没有正确移动。我参考了其他一些教程,但没有帮助。当我将图像向左移动时,它会向下移动

代码:

imageView.setOnTouchListener(new View.OnTouchListener() {



        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();

              switch(event.getAction())                 
                 {
                   case MotionEvent.ACTION_DOWN:  

                         break;   

                   case MotionEvent.ACTION_MOVE:

                         int x_cord = (int) event.getRawX();
                         int y_cord = (int) event.getRawY();

                   System.out.println("value of x" +x_cord);
                   System.out.println("value of y" +y_cord);         


                         if (x_cord > windowwidth) {
                             x_cord = windowwidth;
                            }
                         if (y_cord > windowheight) {
                             y_cord = windowheight;
                            }


                  layoutParams.leftMargin = x_cord+25;
                  layoutParams.rightMargin = y_cord+25; 

                 imageView.setLayoutParams(layoutParams);
                          break;
                    default: break;
                   } 
                    return true;
        }
    });
4

1 回答 1

1
@Override
public boolean onTouch(View v, MotionEvent event){
        switch(event.getAction())
        {
            case MotionEvent.ACTION_DOWN :
            {
                x = event.getX();
                                    y = event.getY();
                dx = x-myView.getX();
                dy = y-myView.getY();
            }
            break;
            case MotionEvent.ACTION_MOVE :
            {
                myView.setX(event.getX()-dx);
                myView.setY(event.getY()-dy);
            }
            break;
            case MotionEvent.ACTION_UP :
            {
                //your stuff
            }
        return true;
}

对于 API 8:

RelativeLayout.LayoutParams parms;
LinearLayout.LayoutParams par;
float dx=0,dy=0,x=0,y=0;

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch(event.getAction())
    {
        case MotionEvent.ACTION_DOWN :
        {
            parms = (LayoutParams) myView.getLayoutParams();
            par = (LinearLayout.LayoutParams) getWindow().findViewById(Window.ID_ANDROID_CONTENT).getLayoutParams();
            dx = event.getRawX() - parms.leftMargin;
            dy = event.getRawY() - parms.topMargin;
        }
        break;
        case MotionEvent.ACTION_MOVE :
        {
            x = event.getRawX();
            y = event.getRawY();
            parms.leftMargin = (int) (x-dx);
            parms.topMargin = (int) (y - dy);
            myView.setLayoutParams(parms);
        }
        break;
        case MotionEvent.ACTION_UP :
        {

        }
        break;
    }
    return true;
}
于 2013-06-18T17:35:25.447 回答