During the project process, you need to drag the ball at will on the page. At the beginning, the implementation was implemented in the form of a floating ball. Because I used it in the project before, I found that the user has permission restrictions after each installation. Some users even don’t know how to turn on the floating ball permission on their mobile phone after turning off the floating ball permission. This way, the user experience is very bad, so I re-defined the implementation of dragging on the page without requiring permission.
Customize the random drag view:
package ; import ; import ; import ; import ; import ; import ; /** *Drag arbitrarily view */ @SuppressLint("AppCompatCustomView") public class DragView extends ImageView { private int width; private int height; private int screenWidth; private int screenHeight; private Context context; //Drag or not private boolean isDrag=false; public boolean isDrag() { return isDrag; } public DragView(Context context, AttributeSet attrs) { super(context, attrs); =context; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { (widthMeasureSpec, heightMeasureSpec); width=getMeasuredWidth(); height=getMeasuredHeight(); screenWidth= (context); screenHeight=(context)-getStatusBarHeight(); } public int getStatusBarHeight(){ int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); return getResources().getDimensionPixelSize(resourceId); } private float downX; private float downY; @Override public boolean onTouchEvent(MotionEvent event) { (event); if (()) { switch (()) { case MotionEvent.ACTION_DOWN: isDrag=false; downX = (); downY = (); break; case MotionEvent.ACTION_MOVE: ("kid","ACTION_MOVE"); final float xDistance = () - downX; final float yDistance = () - downY; int l,r,t,b; // When the horizontal or vertical sliding distance is greater than 10, it is considered a drag event if ((xDistance) >10 ||(yDistance)>10) { ("kid","Drag"); isDrag=true; l = (int) (getLeft() + xDistance); r = l+width; t = (int) (getTop() + yDistance); b = t+height; //No boundary judgment is drawn, the actual situation of the project should be based on the actual situation of the project, because the position required to move the mobile phone is full screen. // So I can write this way. If it is a fixed area, I need to get the width and height position of the parent control before processing it if(l<0){ l=0; r=l+width; }else if(r>screenWidth){ r=screenWidth; l=r-width; } if(t<0){ t=0; b=t+height; }else if(b>screenHeight){ b=screenHeight; t=b-height; } (l, t, r, b); } break; case MotionEvent.ACTION_UP: setPressed(false); break; case MotionEvent.ACTION_CANCEL: setPressed(false); break; } return true; } return false; } }
Tools used:
package ; import ; import ; import ; import ; import ; public class ScreenUtil { private static int width = 0; private static int height = 0; private static int showHeight = 0; private static int statusHeight = 0; private static float density = 0; public static int getScreenWidth(Context context) { if (width == 0) { WindowManager manager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); Display display = (); width = (); } return width; } public static int getScreenHeight(Context context) { if (height == 0) { WindowManager manager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); Display display = (); height = (); } return height; } public static int getScreenShowHeight(Context context) { if (showHeight == 0) { showHeight = getScreenHeight(context) - getStatusBarHeight(context); } return showHeight; } public static int getStatusBarHeight(Context context) { if (statusHeight > 0) { return statusHeight; } Class<?> c = null; Object obj = null; field = null; int x = 0; try { c = ("$dimen"); obj = (); field = ("status_bar_height"); x = ((obj).toString()); statusHeight = ().getDimensionPixelSize(x); return statusHeight; } catch (Throwable e) { (); } return statusHeight; } public static float getScreenDensity(Context context) { if (density == 0) { try { DisplayMetrics dm = new DisplayMetrics(); WindowManager manager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); ().getMetrics(dm); density = ; } catch (Exception ex) { (); density = 1.0f; } } return density; } public static float getScreentMinLength(Context context) { return getScreenHeight(context) > getScreenWidth(context) ? getScreenWidth(context) : getScreenHeight(context); } /** * Get the maximum length and width of the screen within the max range according to the coefficient of specified k, and the default width is relatively small. * * @param context * @param k * @return */ public static DrawWrap getCutWrap(Context context, float k, float max) { float tWidth = getScreenWidth(context); float tHeight = getScreenHeight(context); if (tWidth * max * k > tHeight) { return new DrawWrap(tHeight * max / k, tHeight * max); } else { return new DrawWrap(tWidth * max, tWidth * max * k); } } public static class DrawWrap { public float width; public float height; public DrawWrap(float width, float height) { = width; = height; } } public static int dip2px(Context context, float dipValue) { return (int) (dipValue * getScreenDensity(context) + 0.5f); } /** * Convert sp value to px value to ensure that the text size remains unchanged * * @param context * @param spValue * (Property scaledDensity in DisplayMetrics class) * @return */ public static int sp2px(Context context, float spValue) { final float fontScale = ().getDisplayMetrics().scaledDensity; return (int) (spValue * fontScale + 0.5f); } /** * Convert from px (pixel) unit to dp according to the resolution of the phone */ public static int px2dip(Context context, float pxValue) { final float scale = ().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f); } /** * Get the height of the top position of the control in the screen - that is, the Y point at the top of the control * * @return */ public static int getScreenViewTopHeight(View view) { return (); } /** * Get the height of the bottom position of the control in the screen - that is, the Y point at the bottom of the control * * @return */ public static int getScreenViewBottomHeight(View view) { return (); } /** * Get the position on the left side of the control in the screen--that is, the X point on the left side of the control * * @return */ public static int getScreenViewLeftHeight(View view) { return (); } /** * Get the position on the right side of the control in the screen--that is, the X point on the right side of the control * * @return */ public static int getScreenViewRightHeight(View view) { return (); } /* * Get control width */ public static int getWidth(View view) { int w = (0, ); int h = (0, ); (w, h); return (()); } /* * Get control height */ public static int getHeight(View view) { int w = (0, ); int h = (0, ); (w, h); return (()); } }
XML file:
< android: android:layout_gravity="center" android:src="@drawable/function_night_open" android:layout_width="80dp" android:layout_height="80dp" />
MainActivity:
package ; import .; import ; import ; import ; public class MainActivity extends AppCompatActivity { DragView iv_drag; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); iv_drag= (DragView) findViewById(.iv_drag); iv_drag.setOnClickListener(new () { @Override public void onClick(View v) { if(!iv_drag.isDrag()){ (, "Response to click", Toast.LENGTH_SHORT).show(); } } }); } }
Summarize
The above is the example code for Android to implement the random drag view effect introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!