防止 ImageView 在父布局边界之外绘制

Nos*_*lvi 5 android android-layout android-xml

我有一个具有缩放功能的图像视图

<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" />
Run Code Online (Sandbox Code Playgroud)

它在一个父级中,具有可绘制的顶角半径:

<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>
Run Code Online (Sandbox Code Playgroud)

这是 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>
Run Code Online (Sandbox Code Playgroud)

我已经尝试了从clipChildrenclipPadding到的所有方法,以确保 ImageView 不会在线性布局的边界之外绘制,到目前为止还没有任何效果。使用 cardview 可以解决这个问题(但在某些设备上会失败,我只需要顶角半径)。

布局背景的样子

布局背景的样子

PhotoView 与图像的外观。 PhotoView 与图像的外观。

如您所见,它跨越了父级的曲线边界。

小智 0

对您来说可能为时已晚,但有人可能仍然需要这个解决方案:)

我创建了一个 CustomConstraintLayout 来防止其子级超出父级的边界。

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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)