Reputation: 21
I have an example which I collect from Internet.
public class MprojectActivity extends Activity {
/** Called when the activity is first created. */
ImageView itan2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
itan2=(ImageView)findViewById(R.id.imageView18);
itan2.setOnTouchListener( new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()==MotionEvent.ACTION_MOVE){
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) itan2.getLayoutParams();
int x =(int) event.getRawX();
int y =(int) event.getRawY();
mParams.leftMargin = x-236;
mParams.topMargin = y-565;
itan2.setLayoutParams(mParams);
}
return true;
}
});
}
}
In this example I subtract 236 and 565 with x and y.What are these values actually.how to find them with dynamic coding?
Upvotes: 1
Views: 12782
Reputation: 415
It is used to create the relative layout, there is nothing complex about it, it's really simple.
x-236 means you are setting a point on x-axis 236 unit away from the left top of the screen. y-565 means you are setting a point on y-axis 565 unit away from the left top of the screen.
by using those two values and left top value (0,0) you are creating a layout to show your image ( which is itan2 in this example)
Upvotes: 1
Reputation: 4081
I am not sure if I understand your question, please edit it. If you want to learn something more about getRawX and getRawY, please have a look at http://developer.android.com/reference/android/view/MotionEvent.html which says:
public final float getRawX ()
Added in API level 1 Returns the original raw X coordinate of this event. For touch events on the screen, this is the original location of the event on the screen, before it had been adjusted for the containing window and views.
Same for getRawY()
Upvotes: 0