有一个布局,其中包含两个视图。第一个视图与哪种类型无关紧要,而另一个自定义视图则部分覆盖第一个视图。 我试图获取第二个视图所覆盖的空间的一部分(作为可绘制的位图?)并将其绘制到第二个视图的另一部分中。 到目前为止没有运气。
示例活动xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/covered_view"
android:layout_width="200dp"
android:layout_height="match_parent"
android:src="@drawable/testimg"
android:scaleType="centerCrop" />
<com.example.coveringview.CloningView
android:id="@+id/covering_view"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginStart="100dp"
android:layout_marginBottom="100dp"
android:background="@color/colorPrimary"
/>
</FrameLayout>
然后使用onDraw方法执行CloningView.kt的代码:
import android.content.Context
import android.graphics.Canvas
import android.graphics.RectF
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
class CloningView : View {
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
// Grab the rectangular segment which has been hidden below the instance of CloningView
val srcRect: RectF = RectF(0.0f, 0.0f, 50.0f, 50.0f)
val drawable: Drawable = ???
// Draw the taken segment at another coordinates
canvas.translate(50.0f, 200.0f)
drawable.draw(canvas)
}
}
我希望onDraw中的注释能够自我解释。
是否有可能实现这一目标?我什么都找不到。