網站首頁 編程語言 正文
本文實例為大家分享了android實現可拖動的浮動view,供大家參考,具體內容如下
業務來源
頁面最小化后,需要出現一個浮動 view 告知用戶,防止遮擋視線,需要對 view 做可滑動處理
已知會遇到的問題
1.view 的依賴的布局類型未知【為了后續方便擴展】
外界傳遞 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 ?// 相對控件的x
? ? private var mDownEventY: Float = 0f
? ? private var mDownX: Float = 0f ?// 相對屏幕所在的 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.拖動事件影響點擊,事件分發處理。
override fun onTouchEvent(event: MotionEvent?): Boolean {
? ? ? ? if (mIsStartAnimation) { ?// 動畫正在進行無需處理 onTouch
? ? ? ? ? ? return true
? ? ? ? }
? ? ? ? if (event == null) {
? ? ? ? ? ? return super.onTouchEvent(event)
? ? ? ? }
? ? ? ? mIsOnTouch = true
? ? ? ? //懸浮區域左上角坐標
? ? ? ? val x = x
? ? ? ? val y = y
? ? ? ? //懸浮區域寬高
? ? ? ? val width = mViewGroupW
? ? ? ? val height = mViewGroupH
? ? ? ? when (event.actionMasked) {
? ? ? ? ? ? ACTION_DOWN -> {
? ? ? ? ? ? ? ? //點擊位置坐標
? ? ? ? ? ? ? ? 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
? ? ? ? ? ? ? ? // 抬起后處理邊界溢出問題
? ? ? ? ? ? ? ? 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.拖到邊界問題。
拖出邊界后做了回彈處理
/**
? ? ?* ?超出邊界回彈
? ? ?* ?@param left 當前 x 方向位置
? ? ?* ?@param right 當前 y 方向位置
*/
? ? private fun resilienceAnimation(left: Float, top: Float, right: Float, bottom: Float) {
? ? ? ?
? ? ? ? var startX = 0f
? ? ? ? var resilienceX = 0f
? ? ? ? if (mBoundaryLeft <= left && right <= mBoundaryRight) { ?// x 方向在范圍內
? ? ? ? ? ? // 不處理
? ? ? ? } 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 方向在范圍內
? ? ? ? ? ? // 不處理
? ? ? ? } else if (mBoundaryTop > top) { ?// top 溢出
? ? ? ? ? ? startY = 0f
? ? ? ? ? ? resilienceY = mBoundaryTop - top
? ? ? ? } else { ?// bottom 溢出
? ? ? ? ? ? startY = 0f
? ? ? ? ? ? resilienceY = mBoundaryBottom - bottom
? ? ? ? }
? ? ? ? if (resilienceX == 0f && resilienceY == 0f) { ?// 在范圍內無需回彈
? ? ? ? ? ? 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方向未溢出
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? // 只進行偏移,實際位置未變化,需要重置偏移量,并重繪
? ? ? ? ? ? ? ? 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()
原文鏈接:https://blog.csdn.net/ff_hh/article/details/120047276
相關推薦
- 2022-10-14 查看pip安裝的python包的位置等詳細信息
- 2022-05-18 C語言自定義類型超詳細梳理之結構體?枚舉?聯合體_C 語言
- 2022-08-03 python數據類型可變與不可變深入分析_python
- 2022-07-19 react Context的基本使用( react跨組件傳遞數據 )
- 2023-11-22 Docker常用命令、指令
- 2022-07-12 使用docker搭建Redis-cluster偽集群
- 2023-03-03 詳解C++?STL模擬實現forward_list_C 語言
- 2022-09-27 詳解adb工具的基本使用_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支