7

I need to move an ImageView (or anything else, for that matter) around inside a RelativeLayout. Does any one know the proper way to do this?

Joren
  • 9,623
  • 19
  • 63
  • 104
  • 1
    Could you be a bit more specific? What does "move around" mean? – CommonsWare Nov 02 '09 at 13:47
  • Like, move its x\y coordinates. For example, if you had an image which was larger than the screen resolution and you wanted to be able to drag it around to look at the parts which were off screen. – Joren Nov 02 '09 at 19:34

4 Answers4

10

Makesure ImageView's layout_height and layout_width is set to fill_parent. Then do:

To move a view, use Matrix. It is very handy.

Matrix matrix = new Matrix();

matrix.reset();

matrix.postTranslate(x, y);

imageview.setScaleType(ScaleType.MATRIX);
imageview.setImageMatrix(matrix);
ademar111190
  • 14,215
  • 14
  • 85
  • 114
whogiawho
  • 466
  • 9
  • 21
  • remember to set the ImageView android:scaleType="matrix" in xml if you plan to use this and return the view to the center/start position (as with a joystick handle), else it will get offset if its not set. – TouchBoarder Oct 20 '12 at 20:07
2

this I found worked:

imageview.layout(l,t,r,b);
Udo Held
  • 12,314
  • 11
  • 67
  • 93
Redsmurf
  • 21
  • 1
1

mImageView.scrollBy(xOffset, yOffset);

Dave
  • 11
  • 3
0

It is very easy. You can implement onTouchListener event of any control you want and set its x, y position like make a new LayoutParams set its x, y and assign to view as view.setLayoutParam(layoutParams); it will drag the View.

user229044
  • 232,980
  • 40
  • 330
  • 338
Adnan
  • 1