-
-
Save Zhuinden/f95727df8f444075706f36c98f5a72d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.davidmedenjak.overlappinginvertview | |
import android.content.Context | |
import android.graphics.* | |
import android.util.AttributeSet | |
import android.view.View | |
import android.widget.FrameLayout | |
class InvertViewLayout(context: Context, attrs: AttributeSet? = null) : FrameLayout(context, attrs) { | |
lateinit var backgroundView: View | |
lateinit var overlay: View | |
lateinit var bitmap: Bitmap | |
val inversePaint: Paint = Paint() | |
val rectPaint: Paint = Paint().apply { | |
isAntiAlias = true | |
color = Color.RED | |
} | |
init { | |
// https://stackoverflow.com/q/5941926/1837367 | |
val mx = floatArrayOf(-1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f) | |
val cm = ColorMatrix(mx) | |
inversePaint.colorFilter = ColorMatrixColorFilter(cm) | |
} | |
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { | |
super.onLayout(changed, left, top, right, bottom) | |
backgroundView = getChildAt(0) | |
overlay = getChildAt(1) | |
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) | |
} | |
override fun drawChild(canvas: Canvas, child: View, drawingTime: Long): Boolean { | |
if (child !== overlay) { | |
return super.drawChild(canvas, child, drawingTime) | |
} | |
val overlayCanvas = Canvas(bitmap) | |
val result = super.drawChild(overlayCanvas, child, drawingTime) | |
canvas.save() | |
canvas.clipRect(backgroundView.left, backgroundView.top, backgroundView.right, backgroundView.bottom) | |
canvas.drawBitmap(bitmap, 0F, 0F, inversePaint) | |
canvas.restore() | |
canvas.save() | |
canvas.clipOutRect(backgroundView.left, backgroundView.top, backgroundView.right, backgroundView.bottom) | |
canvas.drawBitmap(bitmap, 0F, 0F, null) | |
canvas.restore() | |
return result | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.constraint.ConstraintLayout | |
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="com.davidmedenjak.overlappinginvertview.MainActivity"> | |
<com.davidmedenjak.overlappinginvertview.InvertViewLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="#999999"> | |
<View | |
android:layout_width="100dp" | |
android:layout_height="100dp" | |
android:layout_marginStart="50dp" | |
android:layout_marginTop="50dp" | |
android:background="@android:color/black"/> | |
<TextView | |
style="@style/TextAppearance.AppCompat.Display1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="0sp" | |
android:layout_marginTop="100dp" | |
android:text="Hello World!" | |
android:textColor="@android:color/black" | |
android:textSize="50sp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent"/> | |
</com.davidmedenjak.overlappinginvertview.InvertViewLayout> | |
</android.support.constraint.ConstraintLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment