Skip to content

Commit

Permalink
Merge pull request #4 from Madrapps/kotlin_migration
Browse files Browse the repository at this point in the history
Kotlin migration
  • Loading branch information
thsaravana committed May 30, 2017
2 parents 40b51e5 + f9de1b0 commit 7b597f2
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 167 deletions.
12 changes: 10 additions & 2 deletions build.gradle
@@ -1,21 +1,29 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.1.2-4'
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

allprojects {
repositories {
jcenter()
maven {
url "http://dl.bintray.com/madrapps/maven"
url "http://dl.bintray.com/madrapps/maven"
}
maven {
url "https://maven.google.com"
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions eyedropper/build.gradle
@@ -1,9 +1,10 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: "com.jfrog.bintray"


def projectVersion = "1.1.0"
def projectVersion = "1.1.1"
def projectGroupId = "com.github.madrapps"
def siteUrl = 'https://github.com/Madrapps/EyeDropper'
def gitUrl = 'https://github.com/Madrapps/EyeDropper.git'
Expand All @@ -19,7 +20,7 @@ android {
defaultConfig {
minSdkVersion 11
targetSdkVersion 25
versionCode 3
versionCode 4
versionName version

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand All @@ -40,6 +41,7 @@ dependencies {
})
compile 'com.android.support:support-annotations:25.3.1'
testCompile 'junit:junit:4.12'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

install {
Expand Down Expand Up @@ -83,6 +85,7 @@ task sourcesJar(type: Jar) {
}

task javadoc(type: Javadoc) {
excludes = ['**/*.kt'] // < ---- Exclude all kotlin files from javadoc file.
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
Expand All @@ -101,8 +104,8 @@ Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())

bintray {
user = properties.getProperty("bintray.user");
key = properties.getProperty("bintray.apikey");
user = properties.getProperty("bintray.user")
key = properties.getProperty("bintray.apikey")

configurations = ['archives']
pkg {
Expand All @@ -119,4 +122,7 @@ bintray {
}
}
}
}
repositories {
mavenCentral()
}
151 changes: 0 additions & 151 deletions eyedropper/src/main/java/com/madrapps/eyedropper/EyeDropper.java

This file was deleted.

111 changes: 111 additions & 0 deletions eyedropper/src/main/java/com/madrapps/eyedropper/EyeDropper.kt
@@ -0,0 +1,111 @@
package com.madrapps.eyedropper

import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.drawable.BitmapDrawable
import android.support.annotation.ColorInt
import android.view.MotionEvent
import android.view.MotionEvent.ACTION_DOWN
import android.view.MotionEvent.ACTION_UP
import android.view.View
import android.view.View.DRAWING_CACHE_QUALITY_LOW
import android.widget.ImageView

class EyeDropper(val view: View, val listener: ColorSelectionListener) {
private val NO_COLOR = Color.TRANSPARENT
private val INVERT_MATRIX = Matrix()

/**
* Register a callback to be invoked when the color selection begins or ends.
*/
var selectionListener: SelectionListener? = null

init {
if (view.shouldDrawingCacheBeEnabled()) {
view.isDrawingCacheEnabled = true
view.drawingCacheQuality = DRAWING_CACHE_QUALITY_LOW
}
setTouchListener()
}

fun setTouchListener() {
view.setOnTouchListener { _, event ->
if (event.down()) selectionListener?.onSelectionStart(event)
notifyColorSelection(event.x.toInt(), event.y.toInt())
if (event.up()) selectionListener?.onSelectionEnd(event)
true
}
}

fun getColorAtPoint(x: Int, y: Int): Int {
when (view) {
is ImageView -> return handleIfImageView(view, x, y)
else -> return getPixelAtPoint(view.drawingCache, x, y)
}
}

fun handleIfImageView(view: ImageView, x: Int, y: Int): Int {
val drawable = view.drawable
when (drawable) {
is BitmapDrawable -> {
view.imageMatrix.invert(INVERT_MATRIX)
val mappedPoints = floatArrayOf(x.toFloat(), y.toFloat())
INVERT_MATRIX.mapPoints(mappedPoints)
return getPixelAtPoint(drawable.bitmap, mappedPoints[0].toInt(), mappedPoints[1].toInt())
}
else -> return NO_COLOR
}
}

fun getPixelAtPoint(bitmap: Bitmap, x: Int, y: Int): Int {
if (bitmap.isValidCoordinate(x, y)) {
return bitmap.getPixel(x, y)
}
return NO_COLOR
}

fun notifyColorSelection(x: Int, y: Int) {
val colorAtPoint = getColorAtPoint(x, y)
listener.onColorSelected(colorAtPoint)
}


/**
* Optional listener to listen to before and after selection events
*/
interface SelectionListener {
/**
* Invoked when the user touches the view to select a color. This corresponds to the [ ][MotionEvent.ACTION_DOWN] event.
*
* @param event the down motion event
*/
fun onSelectionStart(event: MotionEvent)

/**
* Invoked when the color selection is finished. This corresponds to the [MotionEvent.ACTION_UP]
* event.
*
* @param event the up motion event
*/
fun onSelectionEnd(event: MotionEvent)
}

interface ColorSelectionListener {
/**
* Invoked when a color is selected from the given view
* @param color the selected color
*/
fun onColorSelected(@ColorInt color: Int)
}
}

fun Bitmap.isValidCoordinate(x: Int, y: Int): Boolean {
return x in 1..(width - 1) && y in 1..(height - 1)
}

fun View.shouldDrawingCacheBeEnabled(): Boolean = (this !is ImageView) && !isDrawingCacheEnabled

fun MotionEvent.down() = (actionMasked == ACTION_DOWN)
fun MotionEvent.up() = (actionMasked == ACTION_UP)
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Sun Apr 30 08:56:55 IST 2017
#Thu May 25 21:47:09 IST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-milestone-1-all.zip
14 changes: 8 additions & 6 deletions sample/build.gradle
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.madrapps.eyedropperapp"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
versionCode 11
versionName "1.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -22,11 +22,13 @@ android {

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.github.madrapps:eyedropper:1.0.0'
compile project(':eyedropper')
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'

testCompile 'junit:junit:4.12'

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
}

0 comments on commit 7b597f2

Please sign in to comment.