How do I press and drag and leave imageView where i unpressed my finger?[android]

62 views Asked by At
ImageView currentImage = (ImageView) findViewById(R.id.image) ;  
onTouch(View v, MotionEvent event){
 if(event.getAction() == MotionEvent.ACTION_DOWN){
    float x = event.getX() ;
    float y = event.getY();
    currentImage.setX(x) ;
    currentImage.setY(y) ;
    //the 2 lines above must be dragging the image across the screen
 }


 if( event.getAction() == MotionEvent.ACTION_UP){
     currentImage.setX(x) ;
     currentImage.setY(y) ;
 }

}

However, the code does not work, this is the closest i could come up with.

1

There are 1 answers

1
NehaK On

For anything below Honeycomb (API Level 11) you will have to use setLayoutParams():-

If superLayout is RelativeLayout-

 RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(int width, int height);
    layoutParams.setMargins(int left, int top, int right, int bottom);
    imageView.setLayoutParams(layoutParams);

If superLayout is Linearlayout-

 Linearlayout.LayoutParams layoutParams=new Linearlayout.LayoutParams(int width, int height);
    layoutParams.setMargins(int left, int top, int right, int bottom);
    imageView.setLayoutParams(layoutParams);

And If you limit your support to Honeycomb and up you can use the setX(), setY(), setLeft(), setTop()