Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the View effect that can be dragged freely by Android

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

The main content of this article is to explain "how to achieve the View effect that can be dragged freely by Android". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how Android can achieve the View effect that can be dragged at will."

Customize and drag the View at will:

Package com.dragdemo;import android.annotation.SuppressLint;import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.widget.ImageView;/** * view * / @ SuppressLint ("AppCompatCustomView") public class DragView extends ImageView {private int width; private int height; private int screenWidth; private int screenHeight; private Context context; / / whether to drag private boolean isDrag=false; public boolean isDrag () {return isDrag } public DragView (Context context, AttributeSet attrs) {super (context, attrs); this.context=context;} @ Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure (widthMeasureSpec, heightMeasureSpec); width=getMeasuredWidth (); height=getMeasuredHeight (); screenWidth= ScreenUtil.getScreenWidth (context); screenHeight=ScreenUtil.getScreenHeight (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) {super.onTouchEvent (event); if (this.isEnabled ()) {switch (event.getAction ()) {case MotionEvent.ACTION_DOWN: isDrag=false; downX = event.getX (); downY = event.getY (); break Case MotionEvent.ACTION_MOVE: Log.e ("kid", "ACTION_MOVE"); final float xDistance = event.getX ()-downX; final float yDistance = event.getY ()-downY; int / / drag event if (Math.abs (xDistance) > 10 | | Math.abs (yDistance) > 10) {Log.e ("kid", "Drag"); isDrag=true; l = (int) (getLeft () + xDistance); r = lumped widths; t = (int) (getTop () + yDistance) when the horizontal or vertical sliding distance is greater than 10 B = tweak height; / / if the boundary is not drawn, it should be judged according to the actual situation of the project, because the location that the project needs to move is the full screen of the phone, / / so it can be written this way. If it is a fixed area, you need to get the width and height of the parent control before processing if (lscreenWidth) {ringing screen width; l=r-width. } if (tscreenHeight) {baked screen height;} this.layout (l, t, r, b);} break; case MotionEvent.ACTION_UP: setPressed (false); break; case MotionEvent.ACTION_CANCEL: setPressed (false) Break;} return true;} return false;}}

Tool classes used:

Package com.dragdemo;import android.content.Context;import android.util.DisplayMetrics;import android.view.Display;import android.view.View;import android.view.WindowManager;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 = manager.getDefaultDisplay (); width = display.getWidth ();} return width;} public static int getScreenHeight (Context context) {if (height = = 0) {WindowManager manager = (WindowManager) context .getSystemService (Context.WINDOW_SERVICE); Display display = manager.getDefaultDisplay (); height = display.getHeight ();} 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; java.lang.reflect.Field field = null; int x = 0 Try {c = Class.forName ("com.android.internal.R$dimen"); obj = c.newInstance (); field = c.getField ("status_bar_height"); x = Integer.parseInt (field.get (obj). ToString ()); statusHeight = context.getResources (). GetDimensionPixelSize (x); return statusHeight;} catch (Throwable e) {e.printStackTrace ();} return statusHeight } public static float getScreenDensity (Context context) {if (density = = 0) {try {DisplayMetrics dm = new DisplayMetrics (); WindowManager manager = (WindowManager) context .getSystemService (Context.WINDOW_SERVICE); manager.getDefaultDisplay (). GetMetrics (dm); density = dm.density;} catch (Exception ex) {ex.printStackTrace (); density = 1.0f }} return density;} public static float getScreentMinLength (Context context) {return getScreenHeight (context) > getScreenWidth (context)? GetScreenWidth (context): getScreenHeight (context);} / * obtain the maximum length and width of the screen in the max range according to the coefficient of k specified. The default width is 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) {this.width = width; this.height = height }} public static int dip2px (Context context, float dipValue) {return (int) (dipValue * getScreenDensity (context) + 0.5f) } / * convert sp value to PX value to keep text size unchanged * * @ param context * @ param spValue * (attribute in DisplayMetrics class scaledDensity) * @ return * / public static int sp2px (Context context, float spValue) {final float fontScale = context.getResources (). GetDisplayMetrics (). ScaledDensity; return (int) (spValue * fontScale + 0.5f) } / * change from px (pixel) unit to dp * / public static int px2dip (Context context, float pxValue) {final float scale = context.getResources (). GetDisplayMetrics (). Density; return (int) (pxValue / scale + 0.5f) according to the resolution of the phone. } / * get the height of the top position of the control on the screen-that is, the Y point * * @ return * / public static int getScreenViewTopHeight (View view) {return view.getTop ();} / * get the height of the bottom position of the control on the screen-that is, the Y point * * @ return * / public static int getScreenViewBottomHeight (View view) {return view.getBottom () } / * get the position on the left side of the control on the screen-that is, the X point on the left side of the control * * @ return * / public static int getScreenViewLeftHeight (View view) {return view.getLeft ();} / * get the position on the right side of the control on the screen-that is, the X point on the right side of the control * * @ return * / public static int getScreenViewRightHeight (View view) {return view.getRight () } / * * get control width * / public static int getWidth (View view) {int w = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED); view.measure (w, h); return (view.getMeasuredWidth ()) } / * * get control height * / public static int getHeight (View view) {int w = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED); view.measure (w, h); return (view.getMeasuredHeight ());}}

XML file:

MainActivity:package com.dragdemo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Toast;public class MainActivity extends AppCompatActivity {DragView iv_drag; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); iv_drag= (DragView) findViewById (R.id.iv_drag) Iv_drag.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {if (! iv_drag.isDrag ()) {Toast.makeText (MainActivity.this, "response Click", Toast.LENGTH_SHORT) .show ();}) }} at this point, I believe you have a deeper understanding of "Android how to achieve the View effect that can be dragged at will", so you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report