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 告知用戶(hù),防止遮擋視線,需要對(duì) view 做可滑動(dòng)處理
已知會(huì)遇到的問(wèn)題
1.view 的依賴(lài)的布局類(lèi)型未知【為了后續(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)文章
100 行代碼實(shí)現(xiàn)Flutter自定義TabBar的示例代碼
這篇文章主要介紹了100 行代碼實(shí)現(xiàn)Flutter自定義TabBar的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Android開(kāi)發(fā)新手必須知道的10大嚴(yán)重錯(cuò)誤
這篇文章主要介紹了Android開(kāi)發(fā)新手必須知道的10大嚴(yán)重錯(cuò)誤,總結(jié)分析了Android開(kāi)發(fā)中幫助文件、開(kāi)發(fā)工具、社區(qū)等的重要性以及重要的開(kāi)發(fā)原則,需要的朋友可以參考下2016-01-01
Android viewpage實(shí)現(xiàn)可控制的禁止滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android viewpage實(shí)現(xiàn)可控制的禁止滑動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
Android?RecyclerView使用ListAdapter高效刷新數(shù)據(jù)的操作方法
這篇文章主要介紹了Android?RecyclerView使用ListAdapter高效刷新數(shù)據(jù),本次也是介紹了用另外一種方法來(lái)實(shí)現(xiàn)RecyclerView高效刷新數(shù)據(jù)的功能,需要的朋友可以參考下2022-10-10
Android :okhttp+Springmvc文件解析器實(shí)現(xiàn)android向服務(wù)器上傳照片
這篇文章主要介紹了Android :okhttp+Springmvc文件解析器實(shí)現(xiàn)android向服務(wù)器上傳照片,需要的朋友可以參考下2020-05-05
Android LayoutInflater深入分析及應(yīng)用
這篇文章主要介紹了Android LayoutInflater分析的相關(guān)資料,需要的朋友可以參考下2017-02-02
android 拷貝sqlite數(shù)據(jù)庫(kù)到本地sd卡的方法
下面小編就為大家?guī)?lái)一篇android 拷貝sqlite數(shù)據(jù)庫(kù)到本地sd卡的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
Android ListView和Adapter數(shù)據(jù)適配器的簡(jiǎn)單介紹
這篇文章主要介紹了Android ListView和Adapter數(shù)據(jù)適配器的簡(jiǎn)單介紹,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04

