fix:修复PK-头像缩放适配问题
This commit is contained in:
@@ -47,6 +47,7 @@
|
||||
android:id="@+id/iv_avatar_red"
|
||||
android:layout_width="@dimen/dp_62"
|
||||
android:layout_height="@dimen/dp_62"
|
||||
android:scaleType="centerCrop"
|
||||
android:src="@drawable/default_avatar"
|
||||
app:layout_constraintBottom_toBottomOf="@id/iv_avatar_border_red"
|
||||
app:layout_constraintEnd_toEndOf="@id/iv_avatar_border_red"
|
||||
@@ -102,6 +103,7 @@
|
||||
android:id="@+id/iv_avatar_blue"
|
||||
android:layout_width="@dimen/dp_62"
|
||||
android:layout_height="@dimen/dp_62"
|
||||
android:scaleType="centerCrop"
|
||||
android:src="@drawable/default_avatar"
|
||||
app:layout_constraintBottom_toBottomOf="@id/iv_avatar_border_blue"
|
||||
app:layout_constraintEnd_toEndOf="@id/iv_avatar_border_blue"
|
||||
@@ -145,6 +147,7 @@
|
||||
android:id="@+id/iv_avatar_contribute"
|
||||
android:layout_width="@dimen/dp_36"
|
||||
android:layout_height="@dimen/dp_36"
|
||||
android:scaleType="centerCrop"
|
||||
android:layout_marginBottom="@dimen/dp_4"
|
||||
android:src="@drawable/default_avatar"
|
||||
app:layout_constraintBottom_toBottomOf="@id/bg_avatar_contribute"
|
||||
@@ -194,6 +197,7 @@
|
||||
android:layout_width="@dimen/dp_36"
|
||||
android:layout_height="@dimen/dp_36"
|
||||
android:layout_marginBottom="@dimen/dp_4"
|
||||
android:scaleType="centerCrop"
|
||||
android:src="@drawable/default_avatar"
|
||||
app:layout_constraintBottom_toBottomOf="@id/bg_avatar_charm"
|
||||
app:layout_constraintEnd_toEndOf="@id/bg_avatar_charm"
|
||||
|
@@ -65,6 +65,7 @@
|
||||
android:id="@+id/iv_avatar_red"
|
||||
android:layout_width="@dimen/dp_62"
|
||||
android:layout_height="@dimen/dp_62"
|
||||
android:scaleType="centerCrop"
|
||||
android:src="@drawable/default_avatar"
|
||||
app:layout_constraintBottom_toBottomOf="@id/iv_avatar_border_red"
|
||||
app:layout_constraintEnd_toEndOf="@id/iv_avatar_border_red"
|
||||
@@ -120,6 +121,7 @@
|
||||
android:id="@+id/iv_avatar_blue"
|
||||
android:layout_width="@dimen/dp_62"
|
||||
android:layout_height="@dimen/dp_62"
|
||||
android:scaleType="centerCrop"
|
||||
android:src="@drawable/default_avatar"
|
||||
app:layout_constraintBottom_toBottomOf="@id/iv_avatar_border_blue"
|
||||
app:layout_constraintEnd_toEndOf="@id/iv_avatar_border_blue"
|
||||
|
@@ -93,6 +93,7 @@
|
||||
android:layout_height="@dimen/dp_43"
|
||||
android:layout_marginStart="@dimen/dp_10"
|
||||
android:layout_marginTop="@dimen/dp_56"
|
||||
android:scaleType="centerCrop"
|
||||
android:src="@drawable/default_avatar"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@id/iv_bg"
|
||||
@@ -127,6 +128,7 @@
|
||||
android:layout_width="@dimen/dp_43"
|
||||
android:layout_height="@dimen/dp_43"
|
||||
android:layout_marginEnd="@dimen/dp_10"
|
||||
android:scaleType="centerCrop"
|
||||
android:src="@drawable/default_avatar"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintTop_toTopOf="@id/iv_avatar_left"
|
||||
|
@@ -21,6 +21,7 @@
|
||||
android:id="@+id/iv_avatar_1"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:scaleType="centerCrop"
|
||||
android:layout_margin="@dimen/dp_0_5"
|
||||
app:shapeAppearance="@style/shape_circle" />
|
||||
|
||||
@@ -61,6 +62,7 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_margin="@dimen/dp_0_5"
|
||||
android:scaleType="centerCrop"
|
||||
app:shapeAppearance="@style/shape_circle" />
|
||||
|
||||
<TextView
|
||||
@@ -99,6 +101,7 @@
|
||||
android:id="@+id/iv_avatar_3"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:scaleType="centerCrop"
|
||||
android:layout_margin="@dimen/dp_0_5"
|
||||
app:shapeAppearance="@style/shape_circle" />
|
||||
|
||||
|
@@ -18,6 +18,7 @@
|
||||
|
||||
<com.google.android.material.imageview.ShapeableImageView
|
||||
android:id="@+id/iv_avatar_1"
|
||||
android:scaleType="centerCrop"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_margin="@dimen/dp_2"
|
||||
@@ -38,6 +39,7 @@
|
||||
|
||||
<com.google.android.material.imageview.ShapeableImageView
|
||||
android:id="@+id/iv_avatar_2"
|
||||
android:scaleType="centerCrop"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_margin="@dimen/dp_2"
|
||||
@@ -58,6 +60,7 @@
|
||||
|
||||
<com.google.android.material.imageview.ShapeableImageView
|
||||
android:id="@+id/iv_avatar_3"
|
||||
android:scaleType="centerCrop"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_margin="@dimen/dp_2"
|
||||
|
@@ -60,6 +60,7 @@
|
||||
android:id="@+id/iv_avatar_red"
|
||||
android:layout_width="@dimen/dp_62"
|
||||
android:layout_height="@dimen/dp_62"
|
||||
android:scaleType="centerCrop"
|
||||
android:src="@drawable/default_avatar"
|
||||
app:layout_constraintBottom_toBottomOf="@id/iv_avatar_border_red"
|
||||
app:layout_constraintEnd_toEndOf="@id/iv_avatar_border_red"
|
||||
@@ -80,6 +81,7 @@
|
||||
android:id="@+id/iv_avatar_blue"
|
||||
android:layout_width="@dimen/dp_62"
|
||||
android:layout_height="@dimen/dp_62"
|
||||
android:scaleType="centerCrop"
|
||||
android:src="@drawable/default_avatar"
|
||||
app:layout_constraintBottom_toBottomOf="@id/iv_avatar_border_blue"
|
||||
app:layout_constraintEnd_toEndOf="@id/iv_avatar_border_blue"
|
||||
|
@@ -21,6 +21,7 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:padding="@dimen/dp_0_5"
|
||||
android:scaleType="centerCrop"
|
||||
android:src="@drawable/default_avatar"
|
||||
app:shapeAppearance="@style/shape_circle"
|
||||
app:strokeColor="@color/white"
|
||||
|
@@ -0,0 +1,134 @@
|
||||
package com.chwl.library.widget.drag
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Point
|
||||
import android.util.AttributeSet
|
||||
import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.FrameLayout
|
||||
import androidx.core.view.contains
|
||||
import androidx.customview.widget.ViewDragHelper
|
||||
import com.example.lib_utils.log.ILog
|
||||
|
||||
|
||||
abstract class FloatDragWindowView : FrameLayout, ILog {
|
||||
|
||||
private var dragHelper: ViewDragHelper? = null
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* 新创建一个同步点
|
||||
*/
|
||||
fun createDragSyncPoint(): Point {
|
||||
return Point(-1, -1)
|
||||
}
|
||||
}
|
||||
|
||||
constructor(context: Context) : this(context, null)
|
||||
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
|
||||
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : this(
|
||||
context,
|
||||
attrs,
|
||||
defStyleAttr,
|
||||
0
|
||||
)
|
||||
|
||||
constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet?,
|
||||
defStyleAttr: Int,
|
||||
defStyleRes: Int
|
||||
) : super(context, attrs, defStyleAttr, defStyleRes) {
|
||||
onInitialize()
|
||||
}
|
||||
|
||||
protected open fun onInitialize() {
|
||||
// initDrag(this)
|
||||
}
|
||||
|
||||
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
|
||||
super.onLayout(changed, left, top, right, bottom)
|
||||
updateDragPosition()
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化拖拽功能
|
||||
*/
|
||||
protected fun initDrag(parent: ViewGroup) {
|
||||
val dragCallback = object : ViewDragCallback(parent) {
|
||||
|
||||
override fun onViewReleased(releasedChild: View, xvel: Float, yvel: Float) {
|
||||
super.onViewReleased(releasedChild, xvel, yvel)
|
||||
getDragSyncPoint()?.set(releasedChild.left, releasedChild.top)
|
||||
}
|
||||
|
||||
override fun tryCaptureView(child: View, pointerId: Int): Boolean {
|
||||
return this@FloatDragWindowView.tryCaptureView(child, pointerId)
|
||||
}
|
||||
}
|
||||
dragHelper = ViewDragHelper.create(parent, 1f, dragCallback)
|
||||
}
|
||||
|
||||
/**
|
||||
* 绑定容器
|
||||
*/
|
||||
internal fun bindLayout(parent: ViewGroup) {
|
||||
if (parent.contains(this)) {
|
||||
return
|
||||
}
|
||||
parent.addView(
|
||||
this,
|
||||
ViewGroup.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.MATCH_PARENT
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新同步之前的位置
|
||||
*/
|
||||
open fun updateDragPosition() {
|
||||
getDragSyncPoint()?.let {
|
||||
if (it.x == -1 && it.y == -1) {
|
||||
// 该同步点是初始状态
|
||||
return@let
|
||||
}
|
||||
getDragView().offsetLeftAndRight(it.x - getDragView().left)
|
||||
getDragView().offsetTopAndBottom(it.y - getDragView().top)
|
||||
}
|
||||
}
|
||||
|
||||
abstract fun getDragView(): View
|
||||
|
||||
/**
|
||||
* 可以拖拽?
|
||||
*/
|
||||
abstract fun tryCaptureView(child: View, pointerId: Int): Boolean
|
||||
|
||||
/**
|
||||
* 拖拽位置同步记录
|
||||
*/
|
||||
abstract fun getDragSyncPoint(): Point?
|
||||
|
||||
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
|
||||
if (ev?.action == MotionEvent.ACTION_DOWN) {
|
||||
dragHelper?.findTopChildUnder(ev.x.toInt(), ev.y.toInt()) ?: return false
|
||||
}
|
||||
return super.dispatchTouchEvent(ev)
|
||||
}
|
||||
|
||||
override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
|
||||
return dragHelper?.shouldInterceptTouchEvent(event) ?: super.onInterceptTouchEvent(event)
|
||||
}
|
||||
|
||||
override fun onTouchEvent(event: MotionEvent): Boolean {
|
||||
return if (dragHelper == null) {
|
||||
super.onTouchEvent(event)
|
||||
} else {
|
||||
dragHelper?.processTouchEvent(event)
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
package com.chwl.library.widget.drag
|
||||
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.customview.widget.ViewDragHelper
|
||||
|
||||
open class ViewDragCallback(private val parent: ViewGroup) : ViewDragHelper.Callback() {
|
||||
|
||||
override fun tryCaptureView(child: View, pointerId: Int): Boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
override fun clampViewPositionVertical(child: View, top: Int, dy: Int): Int {
|
||||
if (isBoundaryLimit(child)) {
|
||||
return (parent.height - child.height - parent.paddingBottom).coerceAtMost(
|
||||
parent.paddingTop.coerceAtLeast(
|
||||
top
|
||||
)
|
||||
)
|
||||
}
|
||||
return top
|
||||
}
|
||||
|
||||
override fun clampViewPositionHorizontal(child: View, left: Int, dx: Int): Int {
|
||||
if (isBoundaryLimit(child)) {
|
||||
return (parent.width - child.width - parent.paddingEnd).coerceAtMost(
|
||||
parent.paddingStart.coerceAtLeast(
|
||||
left
|
||||
)
|
||||
)
|
||||
}
|
||||
return left
|
||||
}
|
||||
|
||||
override fun onViewPositionChanged(
|
||||
changedView: View,
|
||||
left: Int,
|
||||
top: Int,
|
||||
dx: Int,
|
||||
dy: Int
|
||||
) {
|
||||
}
|
||||
|
||||
override fun getOrderedChildIndex(index: Int): Int {
|
||||
return index
|
||||
}
|
||||
|
||||
override fun getViewHorizontalDragRange(child: View): Int {
|
||||
return parent.measuredWidth - child.measuredWidth
|
||||
}
|
||||
|
||||
override fun getViewVerticalDragRange(child: View): Int {
|
||||
return parent.measuredHeight - child.measuredHeight
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否边界限制
|
||||
* @param child 要处理的view
|
||||
*/
|
||||
protected open fun isBoundaryLimit(child: View): Boolean {
|
||||
return true
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user