Android customized dragfloatingactionbutton

Floating button floating actionbutton is a new control added by Android 5.0 System. Floating actionbutton inherits from ImageView, so floating actionbutton has all properties of ImageView. This article explains a drag and drop suspension button, and adds the function of adsorption frame similar to QQ. Before that, let's learn about its simple use:

First you have to add its dependencies

Then use it in the layout file.

As shown in the figure:

When the floatingactionbutton is normally displayed, it has a fill color and a shadow; When clicked, there will be a ripplecolor, and the shadow range can be increased. Of which:

1. The default fill color is coloraccent in style.

2. Ripplecolor defaults to colorcontrolhighlight in theme.

3. Elevation and pressedtranslationz, the former allows the user to set the shadow size for normal display; The latter is the shadow size displayed when clicked.

OK, now let's introduce the focus of this article: drag and drop suspension button with adsorption function

Code first.

ScreenUtils.Java

The above code is also very simple. I believe you can see it by looking at the comments in the code. But let's talk about its implementation principle here: for this user-defined floating button, we mainly rewrite its ontouch event, capture the touch event, and then use the setx(), sety() method to move it. The adsorption effect mainly uses the attribute animation. Finally, don't forget whether return is still dragging, so as not to trigger the click event.

PS

Finally, post a pop-up box. It is recommended to use popmenu. Compared with popwindow, it will automatically adjust the display position, which is very useful in dragging the floating button, because if you use the latter, you move the button to the top of the screen, and when your pop-up box is also set above the displayed floating button, it may block the contents of the pop-up box.

Create a new menu folder and add pop in it_ Item.xml file

The above is the Android customized dragfloatingactionbutton that Xiaobian introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>