android - 防止 ImageView 在父布局范围之外绘制

标签 android android-layout android-xml

我有一个具有缩放功能的 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 进行修复(但在某些设备上失败,我只需要顶角半径)。

布局背景如何

How the layout background looks like

PhotoView 与图像的外观。
How the PhotoView looks like with an image.

如您所见,它超出了父项的曲线边界。

最佳答案

对你来说可能为时已晚,但有人可能仍然需要这个解决方案:)
我创建了一个 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/

相关文章:

java - 从 for 循环中初始化 View

android - 删除系列指示器/绘图图例 AndroidPlot

java - 我可以对 Android 组件(例如 Activity 和服务)使用控制反转吗?

java - ClassNotFoundException:config_inputEventCompatProcessorOverrideClassName androidx,在 andoridx 上运行崩溃

android - 在选择器 StateListDrawable 中使用 attr 引用

android - 单个 XML、多个 Activity 以及性能

java - 为什么类型转换对象是多余的

android - 我应该为 ConstraintLayout 元素使用 constraints 还是 size 属性?

android - 如何在 Android Studio 2.2 上刷新预览?

android - 布局错误 :106 width:0 ellipsizedWidth:0