Created
April 29, 2019 20:28
-
-
Save Ronelg/640b7c15630496c3eb45c047279fe69d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import android.content.Context | |
import android.util.AttributeSet | |
import android.view.View | |
import android.graphics.* | |
import android.graphics.PorterDuff | |
import android.graphics.PorterDuffXfermode | |
class BarcodeView @JvmOverloads constructor( | |
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 | |
) : View(context, attrs, defStyleAttr) { | |
private val backgroundColor = 0xA4000000 | |
private val edgeColor = 0xA400FF00 | |
private val rectSize = 300 | |
private var centerX = 0 | |
private var centerY = 0 | |
override fun onDraw(canvas: Canvas) { | |
super.onDraw(canvas) | |
centerX = width / 2 | |
centerY = height / 2 | |
setLayerType(LAYER_TYPE_SOFTWARE, null) | |
drawBackgroud(canvas) | |
drawTransparentRect(canvas) | |
drawEdges(canvas) | |
} | |
private fun drawBackgroud(canvas: Canvas) { | |
val paint = Paint(Paint.ANTI_ALIAS_FLAG) | |
paint.style = Paint.Style.FILL | |
paint.color = backgroundColor.toInt() | |
canvas.drawPaint(paint) | |
} | |
private fun drawTransparentRect(canvas: Canvas) { | |
val rect = RectF() | |
rect.bottom = (centerY - rectSize).toFloat() | |
rect.top = (centerY + rectSize).toFloat() | |
rect.right = (centerX + rectSize).toFloat() | |
rect.left = (centerX - rectSize).toFloat() | |
val paint = Paint(Paint.ANTI_ALIAS_FLAG) | |
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR) | |
paint.alpha = 0; | |
paint.isAntiAlias = true | |
canvas.drawRect(rect, paint) | |
} | |
private fun drawEdges(canvas: Canvas) { | |
val paint = Paint(Paint.ANTI_ALIAS_FLAG) | |
paint.style = Paint.Style.STROKE | |
paint.strokeWidth = 10f | |
paint.color = edgeColor.toInt() | |
val left = (centerX - rectSize).toFloat() | |
val right = (centerX + rectSize).toFloat() | |
val bottom = (centerY + rectSize).toFloat() | |
val top = (centerY - rectSize).toFloat() | |
val edgeSize = left/2 | |
canvas.drawLine(left, bottom, left + edgeSize, bottom, paint)// left horizontal top | |
canvas.drawLine(left, top, left + edgeSize, top, paint)// left horizontal bottom | |
canvas.drawLine(right, bottom, right - edgeSize, bottom, paint)// right horizontal bottom | |
canvas.drawLine(right, top, right - edgeSize, top, paint)// right horizontal bottom | |
canvas.drawLine(left, bottom, left , bottom - edgeSize, paint)// left horizontal bottom | |
canvas.drawLine(left, top, left , top + edgeSize, paint)// left horizontal bottom | |
canvas.drawLine(right, bottom, right , bottom - edgeSize, paint )// left horizontal bottom | |
canvas.drawLine(right, top, right, top + edgeSize, paint)// left horizontal bottom | |
} | |
} |
This file contains hidden or 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"?> | |
<FrameLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<ImageView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:src="@drawable/bg" | |
android:scaleType="centerCrop"/> | |
<com.android.example.BarcodeView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
</FrameLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment