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 draggable floating view by android

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to achieve draggable floating view 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 to achieve draggable floating view by android".

Business source

After the page is minimized, a floating view needs to appear to inform the user that it is necessary to slide the view to prevent the line of sight from being obscured.

Problems known to be encountered

The dependent layout type of 1.view is unknown [for subsequent expansion]

ViewGroup itself inherits LinearLayout [or other ViewGroup]

Class FloatChannelView (var mContext: Context? Var viewGroup: ViewGroup): LinearLayout (mContext) {private var mIcon: ImageView = ImageView (context) private var mName: TextView = TextView (context) private var mClose: ImageView = ImageView (context) private var iconWH = dip2Px (38) private var groupPadding = dip2Px (3) private var mViewGroupH = dip2Px (44) private var mViewGroupW = dip2Px (152) private var mBoundaryLeft: Float private var mBoundaryTop: Float private var mBoundaryRight: Float private var mBoundaryBottom: Float private var mScreenWidth = getScreenWidth ( ) / / get the screen width and height private var mScreenHeight = getScreenHeight () private var mDownEventX: Float = 0f / / relative control x private var mDownEventY: Float = 0f private var mDownX: Float = 0f / / relative screen x private var mDownY: Float = 0f private var mListener: OnClickListener? = null private var mIsStartAnimation: Boolean = false privateval mDefaultMargin = dip2Px (12) private var mMarginLeft = mDefaultMargin private var mMarginTop = mDefaultMargin private var mMarginRight = MDefaultMargin private var mMarginBottom = mDefaultMargin init {layoutParams = LayoutParams (mViewGroupW MViewGroupH) setPadding (groupPadding, groupPadding) setBackgroundResource (R.drawable.backage) / / suggest adding some transparent orientation = HORIZONTAL gravity = Gravity.CENTER_VERTICAL mBoundaryLeft = mMarginLeft.toFloat () mBoundaryTop = mMarginTop.toFloat () mBoundaryRight = mScreenWidth-mMarginRight.toFloat () mBoundaryBottom = (mScreenHeight-mMarginBottom-dip2Px (85)). ToFloat () setView ()}}

two。 Drag events affect clicks, event distribution processing.

Override fun onTouchEvent (event: MotionEvent?): Boolean {if (mIsStartAnimation) {/ / Animation is in progress without processing onTouch return true} if (event = = null) {return super.onTouchEvent (event)} mIsOnTouch = true / / the upper left coordinates of the suspension zone val x = x val y = y / / val width = mViewGroupW val height = mViewGroupH when (event.actionMasked) {ACTION_DOWN-> {/ / Click the location coordinates mDownEventX = event.x mDownEventY = event.y mDownX = x mDownY = y} ACTION_UP-> {mUpTime = System.currentTimeMillis () if (mIsMove & & abs (mDownX-x) {/ / x right overflow l = mBoundaryRight-mViewGroupW r = mBoundaryRight} else-> no overflow in {/ / x direction Out}} when {mBoundaryTop > top-> {/ / y Top overflow t = mBoundaryTop b = mBoundaryTop + mViewGroupH} mBoundaryBottom

< bottom ->

{/ / y bottom overflow t = mBoundaryBottom-mViewGroupH b = mBoundaryBottom} else-> {/ / y direction does not overflow}} / / only offset, the actual position has not changed, the offset needs to be reset And redraw this@FloatChannelView.translationX = 0f this@FloatChannelView.translationY = 0f layout (l.toInt (), t.toInt (), r.toInt ()) B.toInt () mMarginLeft = l.toInt () mMarginTop = t.toInt () mIsStartAnimation = false} override fun onAnimationCancel (animation: Animator?) {} override fun onAnimationRepeat (animation: Animator?) {}) animatorSet.start () so far I believe you have a deeper understanding of "android how to achieve draggable floating view". 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