android實(shí)現(xiàn)可拖動(dòng)的浮動(dòng)view
本文實(shí)例為大家分享了android實(shí)現(xiàn)可拖動(dòng)的浮動(dòng)view,供大家參考,具體內(nèi)容如下
業(yè)務(wù)來(lái)源
頁(yè)面最小化后,需要出現(xiàn)一個(gè)浮動(dòng) view 告知用戶,防止遮擋視線,需要對(duì) view 做可滑動(dòng)處理
已知會(huì)遇到的問(wèn)題
1.view 的依賴的布局類型未知【為了后續(xù)方便擴(kuò)展】
外界傳遞 ViewGroup 自己本身繼承 LinearLayout【或者其他 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() // 獲取屏幕寬高
? ? private var mScreenHeight = getScreenHeight()
?
? ? private var mDownEventX: Float = 0f ?// 相對(duì)控件的x
? ? private var mDownEventY: Float = 0f
? ? private var mDownX: Float = 0f ?// 相對(duì)屏幕所在的 x
? ? private var mDownY: Float = 0f
?
? ? private var mListener: OnClickListener? = null
? ? private var mIsStartAnimation: Boolean = false
?
? ? private val 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, groupPadding, groupPadding)
? ? ? ? setBackgroundResource(R.drawable.backage) // 建議加一些透明
? ? ? ? orientation = HORIZONTAL
? ? ? ? gravity = Gravity.CENTER_VERTICAL
? ? ? ? mBoundaryLeft = mMarginLeft.toFloat()
? ? ? ? mBoundaryTop = mMarginTop.toFloat()
? ? ? ? mBoundaryRight = mScreenWidth - mMarginRight.toFloat()
? ? ? ? mBoundaryBottom = (mScreenHeight - mMarginBottom - dip2Px(85)).toFloat()
? ? ? ? setView()
? ? }
}2.拖動(dòng)事件影響點(diǎn)擊,事件分發(fā)處理。
override fun onTouchEvent(event: MotionEvent?): Boolean {
? ? ? ? if (mIsStartAnimation) { ?// 動(dòng)畫(huà)正在進(jìn)行無(wú)需處理 onTouch
? ? ? ? ? ? return true
? ? ? ? }
? ? ? ? if (event == null) {
? ? ? ? ? ? return super.onTouchEvent(event)
? ? ? ? }
? ? ? ? mIsOnTouch = true
? ? ? ? //懸浮區(qū)域左上角坐標(biāo)
? ? ? ? val x = x
? ? ? ? val y = y
? ? ? ? //懸浮區(qū)域?qū)捀?
? ? ? ? val width = mViewGroupW
? ? ? ? val height = mViewGroupH
? ? ? ? when (event.actionMasked) {
? ? ? ? ? ? ACTION_DOWN -> {
? ? ? ? ? ? ? ? //點(diǎn)擊位置坐標(biāo)
? ? ? ? ? ? ? ? mDownEventX = event.x
? ? ? ? ? ? ? ? mDownEventY = event.y
? ? ? ? ? ? ? ? mDownX = x
? ? ? ? ? ? ? ? mDownY = y
? ? ? ? ? ? }
? ? ? ? ? ? ACTION_UP -> {
? ? ? ? ? ? ? ? mUpTime = System.currentTimeMillis()
? ? ? ? ? ??
? ? ? ? ? ? ? ? if (mIsMove && abs(mDownX - x) <= 8f && abs(mDownY - y) <= 8f) {
? ? ? ? ? ? ? ? ? ? mListener?.onClick(this)
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? mIsMove = false
? ? ? ? ? ? ? ? // 抬起后處理邊界溢出問(wèn)題
? ? ? ? ? ? ? ? resilienceAnimation(x, y, x + mViewGroupW, y + mViewGroupH)
? ? ? ? ? ? }
? ? ? ? ? ? ACTION_MOVE -> {
? ? ? ? ? ? ? ? val changeX = event.x.toInt() - mDownEventX
? ? ? ? ? ? ? ? val changeY = event.y.toInt() - mDownEventY
? ? ? ? ? ? ? ? mIsMove = true
? ? ? ? ? ? ? ? if (changeX == 0f && changeY == 0f) {
? ? ? ? ? ? ? ? ? ? return super.onTouchEvent(event)
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? val left = (x + changeX).toInt()
? ? ? ? ? ? ? ? val top = (y + changeY).toInt()
? ? ? ? ? ? ? ? val right = left + mViewGroupW
? ? ? ? ? ? ? ? val bottom = top + mViewGroupH
? ? ? ? ? ? ? ? layout(left, top, right, bottom)
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return true
? ? }3.拖到邊界問(wèn)題。
拖出邊界后做了回彈處理
/**
? ? ?* ?超出邊界回彈
? ? ?* ?@param left 當(dāng)前 x 方向位置
? ? ?* ?@param right 當(dāng)前 y 方向位置
*/
? ? private fun resilienceAnimation(left: Float, top: Float, right: Float, bottom: Float) {
? ? ? ?
? ? ? ? var startX = 0f
? ? ? ? var resilienceX = 0f
? ? ? ? if (mBoundaryLeft <= left && right <= mBoundaryRight) { ?// x 方向在范圍內(nèi)
? ? ? ? ? ? // 不處理
? ? ? ? } else if (mBoundaryLeft > left) { ?// left 溢出
? ? ? ? ? ? startX = 0f
? ? ? ? ? ? resilienceX = mBoundaryLeft - left
? ? ? ? } else { ? // right 方向底部溢出
? ? ? ? ? ? startX = 0f
? ? ? ? ? ? resilienceX = mBoundaryRight - right
? ? ? ? }
? ? ? ? var startY = 0f
? ? ? ? var resilienceY = 0f
? ? ? ? if (mBoundaryTop <= top && bottom <= mBoundaryBottom) { ?// y 方向在范圍內(nèi)
? ? ? ? ? ? // 不處理
? ? ? ? } else if (mBoundaryTop > top) { ?// top 溢出
? ? ? ? ? ? startY = 0f
? ? ? ? ? ? resilienceY = mBoundaryTop - top
? ? ? ? } else { ?// bottom 溢出
? ? ? ? ? ? startY = 0f
? ? ? ? ? ? resilienceY = mBoundaryBottom - bottom
? ? ? ? }
? ? ? ? if (resilienceX == 0f && resilienceY == 0f) { ?// 在范圍內(nèi)無(wú)需回彈
? ? ? ? ? ? return
? ? ? ? }
?
? ? ? ? // 超出邊界回彈
? ? ? ? val phaseFirstDuration: Long = 400
? ? ? ? var oAnimPhaseFirstTUpX: ObjectAnimator? = null
? ? ? ? if (resilienceX != 0f) {
? ? ? ? ? ? oAnimPhaseFirstTUpX = ObjectAnimator.ofFloat(this, "translationX", startX, resilienceX)
? ? ? ? ? ? ? ? ? ? .setDuration(phaseFirstDuration)
? ? ? ? }
? ? ? ? var oAnimPhaseFirstTUpY: ObjectAnimator? = null
? ? ? ? if (resilienceY != 0f) {
? ? ? ? ? ? oAnimPhaseFirstTUpY = ObjectAnimator.ofFloat(this, "translationY", startY, resilienceY)
? ? ? ? ? ? ? ? ? ? .setDuration(phaseFirstDuration)
? ? ? ? }
? ? ? ? val animatorSet = AnimatorSet()
? ? ? ? if (oAnimPhaseFirstTUpX != null && oAnimPhaseFirstTUpY != null) {
? ? ? ? ? ? animatorSet.play(oAnimPhaseFirstTUpX).with(oAnimPhaseFirstTUpY)
? ? ? ? } else if (oAnimPhaseFirstTUpX != null) {
? ? ? ? ? ? animatorSet.play(oAnimPhaseFirstTUpX)
? ? ? ? } else {
? ? ? ? ? ? animatorSet.play(oAnimPhaseFirstTUpY)
? ? ? ? }
? ? ? ? animatorSet.childAnimations[animatorSet.childAnimations.size - 1].addListener(object : Animator.AnimatorListener {
?
? ? ? ? ? ? override fun onAnimationStart(animation: Animator?) {
? ? ? ? ? ? ? ? mIsStartAnimation = true
? ? ? ? ? ? }
?
? ? ? ? ? ? override fun onAnimationEnd(animation: Animator?) {
? ? ? ? ? ? ? ? var l = left
? ? ? ? ? ? ? ? var t = top
? ? ? ? ? ? ? ? var r = right
? ? ? ? ? ? ? ? var b = bottom
? ? ? ? ? ? ? ? when {
? ? ? ? ? ? ? ? ? ? mBoundaryLeft > left -> { ?// x左邊溢出
? ? ? ? ? ? ? ? ? ? ? ? l = mBoundaryLeft
? ? ? ? ? ? ? ? ? ? ? ? r = mBoundaryLeft + mViewGroupW
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? mBoundaryRight < right -> { ?// x右邊溢出
? ? ? ? ? ? ? ? ? ? ? ? l = mBoundaryRight - mViewGroupW
? ? ? ? ? ? ? ? ? ? ? ? r = mBoundaryRight
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else -> { ?// x方向未溢出
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? when {
? ? ? ? ? ? ? ? ? ? mBoundaryTop > top -> { ?// y 頂部溢出
? ? ? ? ? ? ? ? ? ? ? ? t = mBoundaryTop
? ? ? ? ? ? ? ? ? ? ? ? b = mBoundaryTop + mViewGroupH
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? mBoundaryBottom < bottom -> { ?// y 底部溢出
? ? ? ? ? ? ? ? ? ? ? ? t = mBoundaryBottom - mViewGroupH
? ? ? ? ? ? ? ? ? ? ? ? b = mBoundaryBottom
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else -> { ?// y方向未溢出
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? // 只進(jìn)行偏移,實(shí)際位置未變化,需要重置偏移量,并重繪
? ? ? ? ? ? ? ? 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()以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)ImageView圖片縮放和拖動(dòng)
- Android實(shí)現(xiàn)跟隨手指拖動(dòng)并自動(dòng)貼邊的View樣式(實(shí)例demo)
- Android自定義View實(shí)現(xiàn)拖動(dòng)選擇按鈕
- Android實(shí)現(xiàn)單頁(yè)面浮層可拖動(dòng)view的一種方法
- Android通過(guò)自定義ImageView控件實(shí)現(xiàn)圖片的縮放和拖動(dòng)的實(shí)現(xiàn)代碼
- Android開(kāi)發(fā)實(shí)現(xiàn)可拖動(dòng)排序的ListView功能【附源碼下載】
- Android DragImageView實(shí)現(xiàn)下拉拖動(dòng)圖片放大效果
- Android RecyclerView滑動(dòng)刪除和拖動(dòng)排序
- Android ViewDragHelper仿淘寶拖動(dòng)加載效果
- Android自定義View圓形和拖動(dòng)圓、跟隨手指拖動(dòng)效果
相關(guān)文章
Android UI體驗(yàn)之全屏沉浸式透明狀態(tài)欄樣式
這篇文章主要介紹了Android UI體驗(yàn)之全屏沉浸式透明狀態(tài)欄效果的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android四大組件之Service服務(wù)詳細(xì)講解
Android的服務(wù)是開(kāi)發(fā)Android應(yīng)用程序的重要組成部分。不同于活動(dòng)Activity,服務(wù)是在后臺(tái)運(yùn)行,服務(wù)沒(méi)有接口,生命周期也與活動(dòng)Activity非常不同。通過(guò)使用服務(wù)我們可以實(shí)現(xiàn)一些后臺(tái)操作,比如想從遠(yuǎn)程服務(wù)器加載一個(gè)網(wǎng)頁(yè)等,下面來(lái)看看詳細(xì)內(nèi)容,需要的朋友可以參考下2022-07-07
Android中URLEncoder空格被轉(zhuǎn)碼為"+"號(hào)的處理辦法
當(dāng)上傳文件的文件名中間有空格,用URLEncoder.encode方法會(huì)把空格變成加號(hào)(+)在前臺(tái)頁(yè)面顯示的時(shí)候會(huì)多出加號(hào),下面這篇文章主要給大家介紹了關(guān)于Android中URLEncoder空格被轉(zhuǎn)碼為"+"號(hào)的處理辦法,需要的朋友可以參考下2023-01-01
Android使用Pull解析器解析xml文件的實(shí)現(xiàn)代碼
Android使用Pull解析器解析xml文件的實(shí)現(xiàn)代碼,需要的朋友可以參考一下2013-02-02
Android實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)斷點(diǎn)續(xù)傳功能,能在上次的斷點(diǎn)處繼續(xù)上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
android中soap協(xié)議使用(ksoap調(diào)用webservice)
kSOAP是如何調(diào)用ebservice的呢,首先要使用SoapObject,這是一個(gè)高度抽象化的類,完成SOAP調(diào)用??梢哉{(diào)用它的addProperty方法填寫(xiě)要調(diào)用的webservice方法的參數(shù)2014-02-02
Android檢測(cè)url地址是否可達(dá)的兩種方法
今天小編就為大家分享一篇Android檢測(cè)url地址是否可達(dá)的兩種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01

