First view results
In essence, it is a bit inappropriate to say that it should be the processing of gestures, not to mention the code. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature + Signature = "brush: java;"> public class SimpleDragSample extends Activity {protected int contentTop; @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. layout_simpledrag); DisplayMetrics dm = getResources (). getDisplayMetrics (); final int screenWidth = dm. widthPixels; final int screenHeight = dm. heightPixels; Log. e ("width, height", screenWidth + "," + screenHeight); final Button _ Button = (Button) findViewById (R. id. buttonDrag); _ Button. setOnTouchListener (new OnTouchListener () {int lastX, lastY; boolean isDraging = false; @ Overridepublic boolean onTouch (View v, MotionEvent event) {int ea = event. getAction (); switch (ea) {case MotionEvent. ACTION_DOWN: isDraging = true; lastX = (int) event. getRawX (); lastY = (int) event. getRawY (); if (contentTop = 0) {contentTop = getWindow (). findViewById (Window. ID_ANDROID_CONTENT ). getTop (); // obtain the total height of the status bar and title bar} break; case MotionEvent. ACTION_MOVE: if (isDraging) {int dx = (int) event. getRawX ()-lastX; int dy = (int) event. getRawY ()-lastY; int l = v. getLeft () + dx; int B = v. getBottom () + dy; int r = v. getRight () + dx; int t = v. getTop () + dy; // determine if (l <0) {l = 0; r = v. getWidth () ;}if (t <0) {t = 0; B = v. getHeight () ;}if (r> screenWidth) {r = screenWidth; l = r-v. getWidth () ;}if (B> screenHeight-contentTop) {B = screenHeight-contentTop; t = B-v. getHeight ();} v. layout (l, t, r, B); lastX = (int) event. getRawX (); lastY = (int) event. getRawY (); v. postInvalidate ();} break; case MotionEvent. ACTION_UP: isDraging = false; break; default: break;} return false ;}});}}Layout_simpledrag.xml
The code is not too complex, so I will not explain it more here. I will only discuss the problems encountered during the development process.
1. view. getLeft, view. getRight, view. getTop, view. getBottom is the position relative to the parent view. Here, the "RelativeLayout" defined by the parent view of the button does not include the top Status Bar and the title bar.
This article is very detailed.
Http://blog.csdn.net/androiddevelop/article/details/8373782
2. Use the following code to test my Xiaomi mobile phone (480*854). The obtained value is 320*569.
DisplayMetrics dm = getResources().getDisplayMetrics() ;final int screenWidth = dm.widthPixels ;final int screenHeight = dm.heightPixels ;
Solution:
NOTE: If density is not equal to 1, you must set targetSdkVersion to 4-9. For example:
Add the supports-screens node to the AndroidManifest. xml file of the project. The specific content is as follows:
After the study, we found that if multi-resolution support is not set, the Android system will convert the low density (120) of 160 x320 to the high density () of X or above to the corresponding size.
3. Three methods for obtaining screen resolution
(1) Direct int screenWidth = getWindowManager (). getdefadisplay display (). getWidth (); int screenHeight = getWindowManager (). getdefadisplay display (). getHeight (); (2) Get the DisplayMetrics object through getResources (), DisplayMetrics dm = new DisplayMetrics (); dm = getResources (). getDisplayMetrics (); screenWidth = dm. widthPixels; screenHeight = dm. heightPixels; (3) Get dm = new DisplayMetrics () through getWindowManager (); getWindowManager (). getdefadisplay display (). getMetrics (dm); screenWidth = dm. widthPixels; screenHeight = dm. heightPixels;