Java code
Java code
Package com. twy. test;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. util. DisplayMetrics;
Import android. view. MotionEvent;
Import android. view. View;
Import android. view. View. OnTouchListener;
Import android. widget. ImageView;
Public class TestDemo extends Activity {
Private ImageView img1;
Private ImageView img2;
Private int screenWidth;
Private int screenHeight;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Img1 = (ImageView) findViewById (R. id. imageView1 );
Img2 = (ImageView) findViewById (R. id. imageView2 );
DisplayMetrics dm = getResources (). getDisplayMetrics ();
ScreenWidth = dm. widthPixels;
ScreenHeight = dm. heightPixels-50;
Img1.setOnTouchListener (movingEventListener );
Img2.setOnTouchListener (movingEventListener );
}
Private OnTouchListener movingEventListener = new OnTouchListener (){
Int lastX, lastY;
@ Override
Public boolean onTouch (View v, MotionEvent event ){
Switch (event. getAction ()){
Case MotionEvent. ACTION_DOWN:
LastX = (int) event. getRawX ();
LastY = (int) event. getRawY ();
Break;
Case MotionEvent. ACTION_MOVE:
Int dx = (int) event. getRawX ()-lastX;
Int dy = (int) event. getRawY ()-lastY;
Int left = v. getLeft () + dx;
Int top = v. getTop () + dy;
Int right = v. getRight () + dx;
Int bottom = v. getBottom () + dy;
// The setting cannot be out of bounds
If (left <0 ){
Left = 0;
Right = left + v. getWidth ();
}
If (right> screenWidth ){
Right = screenWidth;
Left = right-v. getWidth ();
}
If (top <0 ){
Top = 0;
Bottom = top + v. getHeight ();
}
If (bottom> screenHeight ){
Bottom = screenHeight;
Top = bottom-v. getHeight ();
}
V. layout (left, top, right, bottom );
LastX = (int) event. getRawX ();
LastY = (int) event. getRawY ();
Break;
Case MotionEvent. ACTION_UP:
Break;
}
Return true;
}
};
}
Xml Code
Java code
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<RelativeLayout android: id = "@ + id/relativeLayout1"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent">
<ImageView android: id = "@ + id/imageView1"
Android: layout_width = "wrap_content" android: background = "@ drawable/icon"
Android: layout_height = "wrap_content" android: layout_alignParentLeft = "true"> </ImageView>
<ImageView android: id = "@ + id/imageView2"
Android: layout_width = "wrap_content" android: background = "@ drawable/icon"
Android: layout_height = "wrap_content" android: layout_toRightOf = "@ + id/imageView1"
Android: layout_alignTop = "@ + id/imageView1" android: layout_alignBottom = "@ + id/imageView1"> </ImageView>
</RelativeLayout>
</LinearLayout>
In the onTouch code, if false is returned, the ACTION_MOVE event cannot be captured.
Returns the value of onTouch in onTouchEvent.
1. if return false indicates that the onTouch event has not been consumed, the onTouch event does not end after the code in onTouch is executed.
2. If return true indicates that the onTouch event has been consumed and the onTouch event has ended.
However, in actual operations, except the ACTION_DOWN event, only the method that returns true can capture other events. Therefore, when false is returned, only the MOVE and UP events after the first DOWN event can be captured.
DisplayMetics class:
The DisplayMetrics class under the Andorid. util package provides general information about the display, such as the display size, resolution, and font.
To obtain DisplayMetrics members, first Initialize an object as follows:
Java code
DisplayMetrics metrics = new DisplayMetrics ();
GetWindowManager (). getDefaultDisplay (). getMetrics;
// GetWindowManager () gets the manager that displays the custom window
// Getdefadisplay Display () Get the default Display object
// GetMetrics (dm) initializes a DisplayMetrics object through the data of the Display object
V. layout (left, top, right, bottom );
Assign a size and position to a view and all of its descendants
This is the second phase of the layout mechanic. (The first is measuring ). in this phase, each parent calllayout on all of its children to position them. this is typically done using the child measurements that were stored in the measure pass (). derived classes with children shoshould override onLayout. in that method, they shocould call layout on each of their children.
Parameters:
L Left position, relative to parent
T Top position, relative to parent
R Right position, relative to parent
B Bottom position, relative to parent
Author "twy"