diff --git a/.travis.yml b/.travis.yml index a03d576..769e989 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,14 @@ language: android - -jdk: - - oraclejdk8 +sudo: required +jdk: oraclejdk8 +dist: trusty android: components: - tools - platform-tools - - build-tools-28.0.3 - - android-28 + - build-tools-29.0.2 + - android-29 - extra-android-m2repository licenses: - 'android-sdk-preview-license-.+' @@ -17,7 +17,10 @@ android: before_install: - yes | sdkmanager "platforms;android-28" + - yes | sdkmanager "platforms;android-29" + +before_script: - chmod +x gradlew script: - - ./gradlew clean build \ No newline at end of file + - ./gradlew clean build \ No newline at end of file diff --git a/README.md b/README.md index b88a6d3..4e4f70a 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ allprojects { ``` ``` dependencies { - implementation 'com.github.santalu:diagonal-imageview:1.1.0' + implementation 'com.github.santalu:diagonal-imageview:1.1.1' } ``` diff --git a/app/build.gradle b/app/build.gradle index 0af9412..6a1e82a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -3,12 +3,12 @@ apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { - compileSdkVersion rootProject.compileSdkVersion + compileSdkVersion versions.compileSdk defaultConfig { applicationId "com.santalu.sample" - minSdkVersion rootProject.minSdkVersion - targetSdkVersion rootProject.targetSdkVersion + minSdkVersion versions.minSdk + targetSdkVersion versions.targetSdk versionCode 1 versionName "1.0" } @@ -23,10 +23,11 @@ android { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation project(':diagonalimageview') - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$rootProject.kotlinVersion" - implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion" - implementation "com.google.android.material:material:$rootProject.materialVersion" - implementation "androidx.recyclerview:recyclerview:$rootProject.recyclerViewVersion" - implementation "androidx.gridlayout:gridlayout:$rootProject.gridLayoutVersion" + implementation project(':library') + + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$versions.kotlin" + implementation "androidx.appcompat:appcompat:$versions.appcompat" + implementation "com.google.android.material:material:$versions.material" + implementation "androidx.recyclerview:recyclerview:$versions.recyclerview" + implementation "androidx.gridlayout:gridlayout:$versions.gridlayout" } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c01d61e..50fb8ff 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ + android:theme="@style/AppTheme" + tools:ignore="GoogleAppIndexingWarning"> @@ -18,11 +20,17 @@ - + - + - + diff --git a/app/src/main/java/com/santalu/myapplication/Extensions.kt b/app/src/main/java/com/santalu/myapplication/Extensions.kt deleted file mode 100644 index dc1b20c..0000000 --- a/app/src/main/java/com/santalu/myapplication/Extensions.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.santalu.myapplication - -import android.content.Context -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.Toast - -/** - * Created by fatih.santalu on 7/24/2018. - */ - -internal fun ViewGroup.inflate(layoutRes: Int): View { - return LayoutInflater.from(context).inflate(layoutRes, this, false) -} - -internal fun Context.toast(text: CharSequence) { - Toast.makeText(this, text, Toast.LENGTH_SHORT).show() -} \ No newline at end of file diff --git a/app/src/main/java/com/santalu/myapplication/MainActivity.kt b/app/src/main/java/com/santalu/myapplication/MainActivity.kt index 6ae439f..cb56c70 100644 --- a/app/src/main/java/com/santalu/myapplication/MainActivity.kt +++ b/app/src/main/java/com/santalu/myapplication/MainActivity.kt @@ -1,19 +1,37 @@ package com.santalu.myapplication +import android.content.Context +import android.content.Intent import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.cardListSample import kotlinx.android.synthetic.main.activity_main.gridSample import kotlinx.android.synthetic.main.activity_main.listSample -class MainActivity : AppCompatActivity() { +class MainActivity: AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - listSample.setOnClickListener { SampleListActivity.start(this) } - cardListSample.setOnClickListener { SampleCardListActivity.start(this) } - gridSample.setOnClickListener { SampleGridActivity.start(this) } + listSample.setOnClickListener { + startActivity(Intent(this, SampleListActivity::class.java)) + } + cardListSample.setOnClickListener { + startActivity(Intent(this, SampleCardListActivity::class.java)) + } + gridSample.setOnClickListener { + startActivity(Intent(this, SampleGridActivity::class.java)) + } } } + +internal fun ViewGroup.inflate(layoutRes: Int): View = + LayoutInflater.from(context).inflate(layoutRes, this, false) + +internal fun Context.toast(text: CharSequence) = + Toast.makeText(this, text, Toast.LENGTH_SHORT).show() diff --git a/app/src/main/java/com/santalu/myapplication/SampleCardListActivity.kt b/app/src/main/java/com/santalu/myapplication/SampleCardListActivity.kt index 7114944..01e4112 100644 --- a/app/src/main/java/com/santalu/myapplication/SampleCardListActivity.kt +++ b/app/src/main/java/com/santalu/myapplication/SampleCardListActivity.kt @@ -1,7 +1,5 @@ package com.santalu.myapplication -import android.app.Activity -import android.content.Intent import android.os.Bundle import android.view.View import android.view.ViewGroup @@ -15,11 +13,12 @@ import kotlinx.android.synthetic.main.activity_list.recyclerView * Created by fatih.santalu on 7/24/2018. */ -class SampleCardListActivity : AppCompatActivity() { +class SampleCardListActivity: AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_list) + supportActionBar?.setDisplayHomeAsUpEnabled(true) recyclerView.apply { setHasFixedSize(true) @@ -27,11 +26,9 @@ class SampleCardListActivity : AppCompatActivity() { } } - class SampleAdapter : Adapter() { + class SampleAdapter: Adapter() { - override fun getItemCount(): Int { - return 20 - } + override fun getItemCount(): Int = 20 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SampleViewHolder { val view = parent.inflate(R.layout.item_card_list) @@ -44,17 +41,6 @@ class SampleCardListActivity : AppCompatActivity() { } } - class SampleViewHolder(itemView: View) : ViewHolder(itemView) + class SampleViewHolder(itemView: View): ViewHolder(itemView) } - - companion object { - - fun start(activity: Activity) { - with(activity) { - intent = Intent(this, SampleCardListActivity::class.java) - startActivity(intent) - } - } - } - } \ No newline at end of file diff --git a/app/src/main/java/com/santalu/myapplication/SampleGridActivity.kt b/app/src/main/java/com/santalu/myapplication/SampleGridActivity.kt index f883093..ea9297e 100644 --- a/app/src/main/java/com/santalu/myapplication/SampleGridActivity.kt +++ b/app/src/main/java/com/santalu/myapplication/SampleGridActivity.kt @@ -1,7 +1,5 @@ package com.santalu.myapplication -import android.app.Activity -import android.content.Intent import android.os.Bundle import android.view.View import androidx.appcompat.app.AppCompatActivity @@ -11,11 +9,12 @@ import com.santalu.diagonalimageview.DiagonalImageView * Created by fatih.santalu on 7/24/2018. */ -class SampleGridActivity : AppCompatActivity() { +class SampleGridActivity: AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_grid) + supportActionBar?.setDisplayHomeAsUpEnabled(true) } fun onImageClick(view: View) { @@ -23,14 +22,4 @@ class SampleGridActivity : AppCompatActivity() { toast("start ${view.start} end ${view.end} clicked") } } - - companion object { - - fun start(activity: Activity) { - with(activity) { - intent = Intent(this, SampleGridActivity::class.java) - startActivity(intent) - } - } - } } \ No newline at end of file diff --git a/app/src/main/java/com/santalu/myapplication/SampleListActivity.kt b/app/src/main/java/com/santalu/myapplication/SampleListActivity.kt index 6222c8d..0460efa 100644 --- a/app/src/main/java/com/santalu/myapplication/SampleListActivity.kt +++ b/app/src/main/java/com/santalu/myapplication/SampleListActivity.kt @@ -1,7 +1,5 @@ package com.santalu.myapplication -import android.app.Activity -import android.content.Intent import android.graphics.Rect import android.os.Bundle import android.view.View @@ -12,7 +10,8 @@ import androidx.recyclerview.widget.RecyclerView.Adapter import androidx.recyclerview.widget.RecyclerView.ItemDecoration import androidx.recyclerview.widget.RecyclerView.State import androidx.recyclerview.widget.RecyclerView.ViewHolder -import com.santalu.diagonalimageview.DiagonalImageView +import com.santalu.diagonalimageview.Direction.NONE +import com.santalu.diagonalimageview.Direction.TOP import com.santalu.myapplication.SampleListActivity.SampleAdapter.SampleViewHolder import kotlinx.android.synthetic.main.activity_list.recyclerView import kotlinx.android.synthetic.main.item_list.view.image @@ -21,11 +20,12 @@ import kotlinx.android.synthetic.main.item_list.view.image * Created by fatih.santalu on 7/24/2018. */ -class SampleListActivity : AppCompatActivity() { +class SampleListActivity: AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_list) + supportActionBar?.setDisplayHomeAsUpEnabled(true) recyclerView.apply { val overlap = resources.getDimensionPixelSize(R.dimen.overlap_size) @@ -35,11 +35,9 @@ class SampleListActivity : AppCompatActivity() { } } - class SampleAdapter : Adapter() { + class SampleAdapter: Adapter() { - override fun getItemCount(): Int { - return 20 - } + override fun getItemCount(): Int = 20 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SampleViewHolder { val view = parent.inflate(R.layout.item_list) @@ -48,15 +46,15 @@ class SampleListActivity : AppCompatActivity() { override fun onBindViewHolder(holder: SampleViewHolder, position: Int) { with(holder.itemView) { - image.start = if (position == 0) DiagonalImageView.NONE else DiagonalImageView.TOP + image.start = if (position == 0) NONE else TOP setOnClickListener { context.toast("position $position clicked") } } } - class SampleViewHolder(itemView: View) : ViewHolder(itemView) + class SampleViewHolder(itemView: View): ViewHolder(itemView) } - class OverlapItemDecoration(private val overlap: Int) : ItemDecoration() { + class OverlapItemDecoration(private val overlap: Int): ItemDecoration() { override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: State) { super.getItemOffsets(outRect, view, parent, state) @@ -65,14 +63,4 @@ class SampleListActivity : AppCompatActivity() { } } } - - companion object { - - fun start(activity: Activity) { - with(activity) { - intent = Intent(this, SampleListActivity::class.java) - startActivity(intent) - } - } - } } \ No newline at end of file diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml index 7daee77..30222cc 100644 --- a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -1,14 +1,14 @@ + android:viewportHeight="108"> + android:strokeWidth="1" + android:strokeColor="#00000000"> + android:strokeWidth="1" + android:strokeColor="#00000000"/> diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml index 4ea54ca..73fa9fa 100644 --- a/app/src/main/res/drawable/ic_launcher_background.xml +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -1,171 +1,170 @@ - + android:viewportHeight="108"> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> + android:strokeWidth="0.8" + android:strokeColor="#33FFFFFF"/> diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index e7034ca..87ed066 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -4,7 +4,7 @@ 182dp 120dp 4dp - 56dp + 36dp 8dp 4dp diff --git a/build.gradle b/build.gradle index ee46ccf..a677d34 100644 --- a/build.gradle +++ b/build.gradle @@ -3,20 +3,22 @@ buildscript { ext { - // Sdk - compileSdkVersion = 28 - targetSdkVersion = 28 - minSdkVersion = 15 - - // Tools - gradleVersion = '3.2.1' - kotlinVersion = '1.3.10' - - // Libraries - appCompatVersion = '1.0.2' - materialVersion = '1.0.0' - recyclerViewVersion = '1.0.0' - gridLayoutVersion = '1.0.0' + versions = [ + // SDK + compileSdk : 29, + targetSdk : 29, + minSdk : 15, + + // TOOLS + gradle : '3.5.3', + kotlin : '1.3.61', + + // CORE + appcompat : '1.1.0', + material : '1.1.0', + recyclerview: '1.1.0', + gridlayout : '1.0.0' + ] } repositories { @@ -25,11 +27,8 @@ buildscript { } dependencies { - classpath "com.android.tools.build:gradle:$gradleVersion" - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion" - - // NOTE: Do not place your application dependencies here; they belong - // in the individual module build.gradle files + classpath "com.android.tools.build:gradle:$versions.gradle" + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$versions.kotlin" } } diff --git a/diagonalimageview/src/main/AndroidManifest.xml b/diagonalimageview/src/main/AndroidManifest.xml deleted file mode 100644 index 06e8288..0000000 --- a/diagonalimageview/src/main/AndroidManifest.xml +++ /dev/null @@ -1,2 +0,0 @@ - diff --git a/diagonalimageview/src/main/java/com/santalu/diagonalimageview/DiagonalImageView.kt b/diagonalimageview/src/main/java/com/santalu/diagonalimageview/DiagonalImageView.kt deleted file mode 100644 index 3496198..0000000 --- a/diagonalimageview/src/main/java/com/santalu/diagonalimageview/DiagonalImageView.kt +++ /dev/null @@ -1,281 +0,0 @@ -package com.santalu.diagonalimageview - -import android.annotation.SuppressLint -import android.content.Context -import android.graphics.Canvas -import android.graphics.Color -import android.graphics.Paint -import android.graphics.Paint.Style -import android.graphics.Path -import android.graphics.RectF -import android.graphics.Region -import android.os.Build.VERSION -import android.os.Build.VERSION_CODES -import android.util.AttributeSet -import android.view.MotionEvent -import androidx.appcompat.widget.AppCompatImageView - -/** - * Created by fatih.santalu on 7/24/2018. - */ - -class DiagonalImageView : AppCompatImageView { - - private val clipPath by lazy { Path() } - private val borderPath by lazy { Path() } - private val borderPaint by lazy { Paint(Paint.ANTI_ALIAS_FLAG) } - private val clickRegion by lazy { Region() } - private val clickRect by lazy { RectF() } - - var start = NONE - var end = NONE - var distance = 0f - - var borderEnabled = false - var borderSize = 0f - var borderColor = Color.BLACK - - constructor(context: Context) : super(context) - - constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { - init(context, attrs) - } - - constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : - super(context, attrs, defStyleAttr) { - init(context, attrs) - } - - private fun init(context: Context, attrs: AttributeSet?) { - attrs?.let { - val a = context.obtainStyledAttributes(it, R.styleable.DiagonalImageView) - - with(a) { - start = getInt(R.styleable.DiagonalImageView_di_start, NONE) - end = getInt(R.styleable.DiagonalImageView_di_end, NONE) - distance = getDimensionPixelSize(R.styleable.DiagonalImageView_di_distance, 0).toFloat() - borderEnabled = getBoolean(R.styleable.DiagonalImageView_di_borderEnabled, false) - borderSize = getDimensionPixelSize(R.styleable.DiagonalImageView_di_borderSize, 0).toFloat() - borderColor = getColor(R.styleable.DiagonalImageView_di_borderColor, Color.BLACK) - recycle() - } - - borderPaint.apply { - style = Style.STROKE - color = borderColor - strokeWidth = borderSize - } - - // refer this https://developer.android.com/guide/topics/graphics/hardware-accel.html#unsupported - val layerType = - if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) LAYER_TYPE_HARDWARE else LAYER_TYPE_SOFTWARE - setLayerType(layerType, null) - } - } - - @SuppressLint("ClickableViewAccessibility") - override fun onTouchEvent(event: MotionEvent?): Boolean { - event - ?.takeUnless { clickRegion.isEmpty } - ?.actionMasked - ?.takeIf { - it == MotionEvent.ACTION_DOWN && - !clickRegion.contains(event.x.toInt(), event.y.toInt()) - } - ?.run { - return false - } - return super.onTouchEvent(event) - } - - override fun dispatchDraw(canvas: Canvas?) { - canvas - ?.takeUnless { clipPath.isEmpty } - ?.run { - clipPath(clipPath) - } - super.dispatchDraw(canvas) - } - - override fun onDraw(canvas: Canvas?) { - if (clipPath.isEmpty) { - super.onDraw(canvas) - return - } - canvas?.apply { - val lastSave = save() - clipPath(clipPath) - super.onDraw(this) - // draw border - borderPath.takeUnless { it.isEmpty } - ?.run { - drawPath(this, borderPaint) - } - restoreToCount(lastSave) - } - } - - override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { - super.onLayout(changed, left, top, right, bottom) - if (changed) { - setClipPath() - } - } - - override fun invalidate() { - super.invalidate() - setClipPath() - } - - private fun setClipPath() { - val width = measuredWidth.toFloat() - val height = measuredHeight.toFloat() - - if (width <= 0 || height <= 0) { - return - } - - clipPath.reset() - borderPath.reset() - - when (start) { - TOP -> { - if (end == TOP || end == LEFT) { - clipPath.apply { - moveTo(0f, 0f) - lineTo(width, distance) - lineTo(width, height) - lineTo(0f, height) - } - - borderPath.takeIf { borderEnabled } - ?.apply { - moveTo(0f, 0f) - lineTo(width, distance) - } - } else { - clipPath.apply { - moveTo(0f, distance) - lineTo(width, 0f) - lineTo(width, height) - lineTo(0f, height) - } - - borderPath.takeIf { borderEnabled } - ?.apply { - moveTo(0f, distance) - lineTo(width, 0f) - } - } - } - BOTTOM -> { - if (end == TOP || end == LEFT) { - clipPath.apply { - moveTo(0f, 0f) - lineTo(width, 0f) - lineTo(width, height - distance) - lineTo(0f, height) - } - - borderPath.takeIf { borderEnabled } - ?.apply { - moveTo(0f, height) - lineTo(width, height - distance) - } - } else { - clipPath.apply { - moveTo(0f, 0f) - lineTo(width, 0f) - lineTo(width, height) - lineTo(0f, height - distance) - } - - borderPath.takeIf { borderEnabled } - ?.apply { - moveTo(0f, height - distance) - lineTo(width, height) - } - } - } - LEFT -> { - if (end == TOP || end == LEFT) { - clipPath.apply { - moveTo(distance, 0f) - lineTo(width, 0f) - lineTo(width, height) - lineTo(0f, height) - } - - borderPath.takeIf { borderEnabled } - ?.apply { - moveTo(distance, 0f) - lineTo(0f, height) - } - } else { - clipPath.apply { - moveTo(0f, 0f) - lineTo(width, 0f) - lineTo(width, height) - lineTo(distance, height) - } - - borderPath.takeIf { borderEnabled } - ?.apply { - moveTo(0f, 0f) - lineTo(distance, height) - } - } - } - RIGHT -> { - if (end == TOP || end == LEFT) { - clipPath.apply { - moveTo(0f, 0f) - lineTo(width, 0f) - lineTo(width - distance, height) - lineTo(0f, height) - } - - borderPath.takeIf { borderEnabled } - ?.apply { - moveTo(width, 0f) - lineTo(width - distance, height) - } - } else { - clipPath.apply { - moveTo(0f, 0f) - lineTo(width - distance, 0f) - lineTo(width, height) - lineTo(0f, height) - } - - borderPath.takeIf { borderEnabled } - ?.apply { - moveTo(width - distance, 0f) - lineTo(width, height) - } - } - } - else -> return - } - - clipPath.close() - borderPath.close() - - clipPath.computeBounds(clickRect, true) - val region = Region( - clickRect.left.toInt(), - clickRect.top.toInt(), - clickRect.right.toInt(), - clickRect.bottom.toInt() - ) - clickRegion.setPath(clipPath, region) - } - - companion object { - const val NONE = 0 - const val LEFT = 1 - const val TOP = 2 - const val RIGHT = 3 - const val BOTTOM = 4 - } -} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 3d8ce0c..09f8244 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,12 +6,12 @@ # http://www.gradle.org/docs/current/userguide/build_environment.html # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. -org.gradle.jvmargs=-Xmx1536m +org.gradle.jvmargs = -Xmx1536m # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true # Kotlin code style for this project: "official" or "obsolete": -kotlin.code.style=official -android.useAndroidX=true -android.enableJetifier=true +kotlin.code.style = official +android.useAndroidX = true +android.enableJetifier = true diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 4c54487..38f3d45 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Fri Oct 05 16:01:27 EET 2018 -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip +distributionBase = GRADLE_USER_HOME +distributionPath = wrapper/dists +zipStoreBase = GRADLE_USER_HOME +zipStorePath = wrapper/dists +distributionUrl = https\://services.gradle.org/distributions/gradle-5.6.4-all.zip diff --git a/diagonalimageview/.gitignore b/library/.gitignore similarity index 100% rename from diagonalimageview/.gitignore rename to library/.gitignore diff --git a/diagonalimageview/build.gradle b/library/build.gradle similarity index 56% rename from diagonalimageview/build.gradle rename to library/build.gradle index 532d690..8e9c6e4 100644 --- a/diagonalimageview/build.gradle +++ b/library/build.gradle @@ -2,13 +2,13 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' android { - compileSdkVersion rootProject.compileSdkVersion + compileSdkVersion versions.compileSdk defaultConfig { - minSdkVersion rootProject.minSdkVersion - targetSdkVersion rootProject.targetSdkVersion - versionCode 10 - versionName "1.1.0" + minSdkVersion versions.minSdk + targetSdkVersion versions.targetSdk + versionCode 11 + versionName "1.1.1" } buildTypes { @@ -26,6 +26,7 @@ android { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$rootProject.kotlinVersion" - implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion" + + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$versions.kotlin" + implementation "androidx.appcompat:appcompat:$versions.appcompat" } diff --git a/diagonalimageview/proguard-rules.pro b/library/proguard-rules.pro similarity index 100% rename from diagonalimageview/proguard-rules.pro rename to library/proguard-rules.pro diff --git a/library/src/main/AndroidManifest.xml b/library/src/main/AndroidManifest.xml new file mode 100644 index 0000000..ae408a6 --- /dev/null +++ b/library/src/main/AndroidManifest.xml @@ -0,0 +1 @@ + diff --git a/library/src/main/java/com/santalu/diagonalimageview/DiagonalImageView.kt b/library/src/main/java/com/santalu/diagonalimageview/DiagonalImageView.kt new file mode 100644 index 0000000..e56844b --- /dev/null +++ b/library/src/main/java/com/santalu/diagonalimageview/DiagonalImageView.kt @@ -0,0 +1,248 @@ +package com.santalu.diagonalimageview + +import android.content.Context +import android.graphics.Canvas +import android.graphics.Color +import android.graphics.Paint +import android.graphics.Paint.Style +import android.graphics.Path +import android.os.Build.VERSION +import android.os.Build.VERSION_CODES +import android.util.AttributeSet +import androidx.appcompat.widget.AppCompatImageView +import com.santalu.diagonalimageview.Direction.BOTTOM +import com.santalu.diagonalimageview.Direction.LEFT +import com.santalu.diagonalimageview.Direction.NONE +import com.santalu.diagonalimageview.Direction.RIGHT +import com.santalu.diagonalimageview.Direction.TOP + +/** + * Created by fatih.santalu on 7/24/2018. + */ + +class DiagonalImageView @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0 +): AppCompatImageView(context, attrs, defStyleAttr) { + + private val clipPath: Path = Path() + private val borderPath: Path = Path() + private val borderPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG) + + var start: Direction = NONE + var end: Direction = NONE + var distance: Float = 0f + + var borderEnabled: Boolean = false + var borderSize: Float = 0f + var borderColor: Int = Color.BLACK + + init { + attrs?.let { + context.obtainStyledAttributes(it, R.styleable.DiagonalImageView).apply { + start = Direction.get(getInt(R.styleable.DiagonalImageView_di_start, 0)) + end = Direction.get(getInt(R.styleable.DiagonalImageView_di_end, 0)) + distance = getDimensionPixelSize(R.styleable.DiagonalImageView_di_distance, 0).toFloat() + borderEnabled = getBoolean(R.styleable.DiagonalImageView_di_borderEnabled, false) + borderSize = getDimensionPixelSize(R.styleable.DiagonalImageView_di_borderSize, 0).toFloat() + borderColor = getColor(R.styleable.DiagonalImageView_di_borderColor, Color.BLACK) + recycle() + } + + borderPaint.apply { + style = Style.STROKE + color = borderColor + strokeWidth = borderSize + } + + // https://developer.android.com/guide/topics/graphics/hardware-accel.html#unsupported + val layerType = if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) LAYER_TYPE_HARDWARE else LAYER_TYPE_SOFTWARE + setLayerType(layerType, null) + } + } + + override fun invalidate() { + super.invalidate() + setClipPath() + } + + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec) + setClipPath() + } + + override fun dispatchDraw(canvas: Canvas?) { + if (!clipPath.isEmpty) { + canvas?.clipPath(clipPath) + } + super.dispatchDraw(canvas) + } + + override fun onDraw(canvas: Canvas?) { + if (clipPath.isEmpty) { + super.onDraw(canvas) + return + } + + canvas?.apply { + save() + clipPath(clipPath) + super.onDraw(this) + if (!borderPath.isEmpty) { + drawPath(borderPath, borderPaint) + } + restore() + } + } + + private fun setClipPath() { + val width = measuredWidth.toFloat() + val height = measuredHeight.toFloat() + if (width <= 0 || height <= 0) return + + clipPath.reset() + borderPath.reset() + + when (start) { + LEFT -> createLeftPath(width, height) + TOP -> createTopPath(width, height) + RIGHT -> createRightPath(width, height) + BOTTOM -> createBottomPath(width, height) + else -> return + } + + clipPath.close() + borderPath.close() + } + + private fun isTopOrLeft(): Boolean = end == TOP || end == LEFT + + private fun createTopPath(width: Float, height: Float) { + if (isTopOrLeft()) { + clipPath.apply { + moveTo(0f, 0f) + lineTo(width, distance) + lineTo(width, height) + lineTo(0f, height) + } + + if (borderEnabled) { + borderPath.apply { + moveTo(0f, 0f) + lineTo(width, distance) + } + } + } else { + clipPath.apply { + moveTo(0f, distance) + lineTo(width, 0f) + lineTo(width, height) + lineTo(0f, height) + } + + if (borderEnabled) { + borderPath.apply { + moveTo(0f, distance) + lineTo(width, 0f) + } + } + } + } + + private fun createBottomPath(width: Float, height: Float) { + if (isTopOrLeft()) { + clipPath.apply { + moveTo(0f, 0f) + lineTo(width, 0f) + lineTo(width, height - distance) + lineTo(0f, height) + } + + if (borderEnabled) { + borderPath.apply { + moveTo(0f, height) + lineTo(width, height - distance) + } + } + } else { + clipPath.apply { + moveTo(0f, 0f) + lineTo(width, 0f) + lineTo(width, height) + lineTo(0f, height - distance) + } + + if (borderEnabled) { + borderPath.apply { + moveTo(0f, height - distance) + lineTo(width, height) + } + } + } + } + + private fun createLeftPath(width: Float, height: Float) { + if (isTopOrLeft()) { + clipPath.apply { + moveTo(distance, 0f) + lineTo(width, 0f) + lineTo(width, height) + lineTo(0f, height) + } + + if (borderEnabled) { + borderPath.apply { + moveTo(distance, 0f) + lineTo(0f, height) + } + } + } else { + clipPath.apply { + moveTo(0f, 0f) + lineTo(width, 0f) + lineTo(width, height) + lineTo(distance, height) + } + + if (borderEnabled) { + borderPath.apply { + moveTo(0f, 0f) + lineTo(distance, height) + } + } + } + } + + private fun createRightPath(width: Float, height: Float) { + if (isTopOrLeft()) { + clipPath.apply { + moveTo(0f, 0f) + lineTo(width, 0f) + lineTo(width - distance, height) + lineTo(0f, height) + } + + if (borderEnabled) { + borderPath.apply { + moveTo(width, 0f) + lineTo(width - distance, height) + } + } + } else { + clipPath.apply { + moveTo(0f, 0f) + lineTo(width - distance, 0f) + lineTo(width, height) + lineTo(0f, height) + } + + if (borderEnabled) { + borderPath.apply { + moveTo(width - distance, 0f) + lineTo(width, height) + } + } + } + } +} \ No newline at end of file diff --git a/library/src/main/java/com/santalu/diagonalimageview/Direction.kt b/library/src/main/java/com/santalu/diagonalimageview/Direction.kt new file mode 100644 index 0000000..bda62df --- /dev/null +++ b/library/src/main/java/com/santalu/diagonalimageview/Direction.kt @@ -0,0 +1,18 @@ +package com.santalu.diagonalimageview + +/** + * Created by fatih.santalu on 7/24/2018. + */ + +enum class Direction { + NONE, + LEFT, + TOP, + RIGHT, + BOTTOM; + + companion object { + + fun get(ordinal: Int): Direction = values().find { it.ordinal == ordinal } ?: NONE + } +} \ No newline at end of file diff --git a/diagonalimageview/src/main/res/values/attrs.xml b/library/src/main/res/values/attrs.xml similarity index 61% rename from diagonalimageview/src/main/res/values/attrs.xml rename to library/src/main/res/values/attrs.xml index e0f1aaf..a046ce6 100644 --- a/diagonalimageview/src/main/res/values/attrs.xml +++ b/library/src/main/res/values/attrs.xml @@ -2,22 +2,22 @@ - + - + - - - - + + + + \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 808585e..3306997 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1 +1 @@ -include ':app', ':diagonalimageview' +include ':app', ':library'