我有一个具有缩放功能的 ImageView
<com.github.chrisbanes.photoview.PhotoView
android:id="@+id/helfie_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="center" />
它位于具有顶角半径的可绘制对象的父级中:
<LinearLayout
android:outlineProvider="background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/sheet_bg"
android:orientation="vertical">
<!-- including the photoview -->
<include
android:id="@+id/preview_layout_frame"
layout="@layout/preview_snaped_helfie"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<android.support.v7.widget.RecyclerView
android:id="@+id/helfie_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="20dp" />
</LinearLayout>
这是
sheet_bg
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/dark" />
<!--<stroke-->
<!--android:width="5dp"-->
<!--android:color="@color/white" />-->
<corners
android:topLeftRadius="35dp"
android:topRightRadius="35dp"
/>
</shape>
我已经尝试了
clipChildren
的所有内容至clipPadding
为了确保 ImageView 不会在线性布局的范围之外绘制,到目前为止还没有任何效果。使用 cardview 进行修复(但在某些设备上失败,我只需要顶角半径)。布局背景如何
PhotoView 与图像的外观。
如您所见,它超出了父项的曲线边界。
最佳答案
对你来说可能为时已晚,但有人可能仍然需要这个解决方案:)
我创建了一个 CustomConstraintLayout 来防止它的 child 超出 parent 的界限。
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import androidx.constraintlayout.widget.ConstraintLayout
/**
* Created by Payam Monsef
* At: 2022/Jun/27
*/
class CustomConstraintLayout @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {
private var topRadius = 64F
private var mPath: Path? = null
private var mPaint: Paint? = null
init {
mPath = Path()
mPaint = Paint()
mPaint?.style = Paint.Style.FILL_AND_STROKE
mPaint?.color = Color.GRAY
setBackgroundColor(Color.TRANSPARENT)
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
mPath?.apply {
reset()
val radiusArr = floatArrayOf(
topRadius, topRadius,
topRadius, topRadius,
0f, 0f,
0f, 0f
)
addRoundRect(
RectF(0f, 0f, width.toFloat(), height.toFloat()),
radiusArr,
Path.Direction.CW
)
}
}
override fun onDraw(canvas: Canvas) {
mPath?.let {
mPaint?.let { paint ->
canvas.drawPath(it, paint)
}
canvas.clipPath(it)
}
}
}
关于android - 防止 ImageView 在父布局范围之外绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48247602/