From 8b1b6ae4134f01e9fb462c2d1e7c8c5fc3f534ba Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 15 Nov 2016 22:40:21 +0100 Subject: [PATCH] convert FocusRectView to kotlin --- .../camera/FocusRectView.java | 66 ------------------- .../simplemobiletools/camera/FocusRectView.kt | 61 +++++++++++++++++ 2 files changed, 61 insertions(+), 66 deletions(-) delete mode 100644 app/src/main/java/com/simplemobiletools/camera/FocusRectView.java create mode 100644 app/src/main/java/com/simplemobiletools/camera/FocusRectView.kt diff --git a/app/src/main/java/com/simplemobiletools/camera/FocusRectView.java b/app/src/main/java/com/simplemobiletools/camera/FocusRectView.java deleted file mode 100644 index 2cdf9163..00000000 --- a/app/src/main/java/com/simplemobiletools/camera/FocusRectView.java +++ /dev/null @@ -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); - } - } -} diff --git a/app/src/main/java/com/simplemobiletools/camera/FocusRectView.kt b/app/src/main/java/com/simplemobiletools/camera/FocusRectView.kt new file mode 100644 index 00000000..a845cd65 --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/camera/FocusRectView.kt @@ -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) + } + } +}