Created
December 11, 2017 16:38
-
-
Save bakawaii/d642f5d1ecd747175047f73eec27accd 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
package com.bakawaii.roomex | |
import android.content.Context | |
import android.graphics.* | |
import android.util.AttributeSet | |
import android.view.View | |
/** | |
* Created by Bakawaii on 2017/12/11. | |
*/ | |
class ScannerOverlay @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) { | |
private val paint: Paint = Paint().apply { | |
color = Color.RED | |
style = Paint.Style.FILL_AND_STROKE | |
strokeWidth = 20F | |
isAntiAlias = true | |
strokeCap = Paint.Cap.ROUND | |
strokeJoin = Paint.Join.ROUND | |
} | |
private val path: Path = Path() | |
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec) | |
val lengthRefs = if (measuredHeight > measuredWidth) measuredWidth else measuredHeight | |
val strokeWidth = lengthRefs.toFloat() / 25 | |
val strokeLength = lengthRefs.toFloat() / 5 | |
paint.strokeWidth = strokeWidth | |
val offset = strokeWidth / 2 | |
path.reset() | |
//Top-left | |
path.moveTo(0F + offset, 0F + offset + strokeLength) | |
path.lineTo(0F + offset, 0F + offset) | |
path.moveTo(0F + offset, 0F + offset) | |
path.lineTo(0F + offset + strokeLength, 0F + offset) | |
//Top-right | |
path.moveTo(measuredWidth.toFloat() - offset, strokeLength + offset) | |
path.lineTo(measuredWidth.toFloat() - offset, 0F + offset) | |
path.moveTo(measuredWidth.toFloat() - offset, 0F + offset) | |
path.lineTo(measuredWidth.toFloat() - offset - strokeLength, 0F + offset) | |
//Bottom-left | |
path.moveTo(0F + offset, measuredHeight - offset - strokeLength) | |
path.lineTo(0F + offset, measuredHeight - offset) | |
path.moveTo(0F + offset, measuredHeight - offset) | |
path.lineTo(0F + offset + strokeLength, measuredHeight - offset) | |
//Bottom-right | |
path.moveTo(measuredWidth.toFloat() - offset, measuredHeight - offset - strokeLength) | |
path.lineTo(measuredWidth.toFloat() - offset, measuredHeight - offset) | |
path.moveTo(measuredWidth.toFloat() - offset, measuredHeight - offset) | |
path.lineTo(measuredWidth.toFloat() - strokeLength, measuredHeight - offset) | |
} | |
override fun onDraw(canvas: Canvas) { | |
super.onDraw(canvas) | |
canvas.drawPath(path, paint) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment