ImageView无法正常移动

时间:2013-06-18 17:29:56

标签: android image relativelayout pan

我有imageView。 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;
        }
    });

1 个答案:

答案 0 :(得分: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;
}