最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android實(shí)現(xiàn)可拖動(dòng)的浮動(dòng)view

 更新時(shí)間:2022年04月19日 09:28:19   作者:為今天而努力  
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)可拖動(dòng)的浮動(dòng)view,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了android實(shí)現(xiàn)可拖動(dòng)的浮動(dòng)view,供大家參考,具體內(nèi)容如下

業(yè)務(wù)來源

頁面最小化后,需要出現(xiàn)一個(gè)浮動(dòng) view 告知用戶,防止遮擋視線,需要對(duì) view 做可滑動(dòng)處理

已知會(huì)遇到的問題

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)畫正在進(jìn)行無需處理 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
? ? ? ? ? ? ? ? // 抬起后處理邊界溢出問題
? ? ? ? ? ? ? ? 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 當(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)無需回彈
? ? ? ? ? ? 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()

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

新巴尔虎左旗| 平昌县| 常宁市| 巩留县| 海淀区| 敦化市| 平武县| 赤水市| 遂溪县| 章丘市| 牡丹江市| 宜兰县| 新兴县| 福安市| 两当县| 枞阳县| 威宁| 余江县| 汽车| 临颍县| 疏附县| 安图县| 九江县| 治多县| 枞阳县| 新邵县| 长沙市| 大方县| 容城县| 宜兰县| 纳雍县| 龙里县| 且末县| 新龙县| 邛崃市| 石棉县| 祁东县| 微山县| 丹阳市| 察隅县| 荥阳市|