convert FocusRectView to kotlin

This commit is contained in:
tibbi 2016-11-15 22:40:21 +01:00
parent e4c4504c71
commit 8b1b6ae413
2 changed files with 61 additions and 66 deletions

View File

@ -1,66 +0,0 @@
package com.simplemobiletools.camera;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.view.ViewGroup;
public class FocusRectView extends ViewGroup {
private static final int RECT_SIZE = 50;
private static final int RECT_DURATION = 500;
private static Paint mPaint;
private static Rect mRect;
private static Handler mHandler;
private static boolean mDrawRect;
private static int mPrimaryColor;
public FocusRectView(Context context) {
super(context);
setWillNotDraw(false);
mHandler = new Handler();
mPrimaryColor = getResources().getColor(R.color.colorPrimary);
setupPaint();
}
private void setupPaint() {
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(mPrimaryColor);
mPaint.setStrokeWidth(2);
}
public void drawFocusRect(int x, int y) {
mRect = new Rect(x - RECT_SIZE, y - RECT_SIZE, x + RECT_SIZE, y + RECT_SIZE);
toggleRect(true);
mHandler.removeCallbacksAndMessages(null);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
toggleRect(false);
}
}, RECT_DURATION);
}
private void toggleRect(boolean show) {
mDrawRect = show;
invalidate();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mDrawRect) {
canvas.drawRect(mRect, mPaint);
}
}
}

View File

@ -0,0 +1,61 @@
package com.simplemobiletools.camera
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.os.Handler
import android.view.ViewGroup
class FocusRectView(context: Context) : ViewGroup(context) {
companion object {
private val RECT_SIZE = 50
private val RECT_DURATION = 500
private var mDrawRect = false
private var mPrimaryColor = 0
lateinit var mPaint: Paint
lateinit var mHandler: Handler
lateinit var mRect: Rect
}
init {
setWillNotDraw(false)
mHandler = Handler()
mPrimaryColor = resources.getColor(R.color.colorPrimary)
setupPaint()
}
private fun setupPaint() {
mPaint = Paint().apply {
style = Paint.Style.STROKE
color = mPrimaryColor
strokeWidth = 2f
}
}
fun drawFocusRect(x: Int, y: Int) {
mRect = Rect(x - RECT_SIZE, y - RECT_SIZE, x + RECT_SIZE, y + RECT_SIZE)
toggleRect(true)
mHandler.removeCallbacksAndMessages(null)
mHandler.postDelayed({ toggleRect(false) }, RECT_DURATION.toLong())
}
private fun toggleRect(show: Boolean) {
mDrawRect = show
invalidate()
}
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
if (mDrawRect) {
canvas.drawRect(mRect, mPaint)
}
}
}