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 suspended and draggable Button by Android

2025-04-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is about Android's implementation of floating draggable Buttons. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

profile

Recently, because of the project needs, need to make a drag button on the interface, there are also many examples on the Internet, read most of the examples are incomplete or not clear explanation, the effect map is not obvious, take this to record their own implementation scheme, in case of emergency, but also for the majority of scholars can directly copy the way to complete the project needs.

Core code implementation

1DraggingButton implementation

public class DraggingButton extends android.support.v7.widget.AppCompatButton { private int lastX = 0; private int lastY = 0; private int beginX = 0; private int beginY = 0; private int screenWidth = 720; private int screenHeight = 1280; public DraggingButton(Context context) { this(context, null); } public DraggingButton(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public DraggingButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initData(context); } private void initData(Context context){ WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(dm); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = (int) event.getRawX(); //Distance from touch point to left of screen lastY = (int) event.getRawY(); //Distance between touch point and top of screen beginX = lastX; beginY = lastY; break; case MotionEvent.ACTION_MOVE: int dx =(int)event.getRawX() - lastX; //Absolute distance of x-axis drag int dy =(int)event.getRawY() - lastY; //absolute distance of y-axis drag // getLeft(): distance from left boundary of child View to left boundary of parent View, getRight(): distance from right boundary of child View to left boundary of parent View //The following data indicates where the view should be in the layout int left = getLeft() + dx; int top = getTop() + dy; int right = getRight() + dx; int bottom = getBottom() + dy; if(left

< 0){ left = 0; right = left + getWidth(); } if(right >

screenWidth){ right = screenWidth; left = right - getWidth(); } if(top

< 0){ top = 0; bottom = top + getHeight(); } if(bottom>

screenHeight){ bottom = screenHeight; top = bottom - getHeight(); } layout(left, top, right, bottom); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: //Release when solving drag and click event trigger if (Math.abs(lastX - beginX) < 10 && Math.abs(lastY - beginY) < 10){ return super.onTouchEvent(event); }else{ setPressed(false); return true; } default: break; } return super.onTouchEvent(event); }}

The core code has been dedicated, and the drag-and-drop function can be realized through the custom DraggingButton. The specific principle mainly lies in the use of onTouchEvent and layout functions. The specific details are not described, and the code comments are relatively clear.

give a chestnut

Layout in activity

Style drag_button_bg.xml

Code in activity

private DraggingButton mDraggintView;mDraggintView = (DraggingButton) findViewById(R.id.tv_dragging); mDraggintView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(FloatingActionBtnTestActivity.this, "click", Toast.LENGTH_SHORT).show(); Thank you for reading! About "Android how to achieve floating can drag Button" this article is shared here, I hope the above content can have some help for everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

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