diff --git a/multiimageview/build.gradle b/multiimageview/build.gradle index ea36db7..f8d9847 100644 --- a/multiimageview/build.gradle +++ b/multiimageview/build.gradle @@ -4,13 +4,13 @@ apply plugin: 'com.novoda.bintray-release' android { compileSdkVersion 25 - buildToolsVersion "25.0.0" + buildToolsVersion "25.0.3" defaultConfig { minSdkVersion 16 targetSdkVersion 25 versionCode 1 - versionName "0.1" + versionName "0.1.1" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" @@ -29,7 +29,7 @@ android { publish { groupId = 'com.github.stfalcon' artifactId = 'multiimageview' - publishVersion = '0.1' + publishVersion = '0.1.1' desc = 'Library for display a few images in one MultiImageView like avatar of group chat' licences = ['Apache-2.0'] uploadName='MultiImageView' diff --git a/multiimageview/src/main/java/com/stfalcon/multiimageview/MultiImageView.kt b/multiimageview/src/main/java/com/stfalcon/multiimageview/MultiImageView.kt index 243c0dc..55ec398 100644 --- a/multiimageview/src/main/java/com/stfalcon/multiimageview/MultiImageView.kt +++ b/multiimageview/src/main/java/com/stfalcon/multiimageview/MultiImageView.kt @@ -21,6 +21,7 @@ import android.graphics.* import android.graphics.drawable.Drawable import android.media.ThumbnailUtils import android.util.AttributeSet +import android.util.TypedValue import android.widget.ImageView import java.util.* @@ -35,9 +36,29 @@ class MultiImageView(context: Context, attrs: AttributeSet) : ImageView(context, field = value invalidate() } + + private val paintDivider = Paint(Paint.ANTI_ALIAS_FLAG) + + var dividerColor = Color.WHITE + set(value) { + field = value + paintDivider.color = value + invalidate() + } + var dividerWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1f, resources.displayMetrics) + set(value) { + field = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, resources.displayMetrics) + invalidate() + } + //Corners radius for rectangle shape var rectCorners = 100 + init { + paintDivider.isAntiAlias = true + paintDivider.color = dividerColor + } + private val bitmaps = ArrayList() private val path = Path() private val rect = RectF() @@ -68,7 +89,7 @@ class MultiImageView(context: Context, attrs: AttributeSet) : ImageView(context, * recreate MultiDrawable and set it as Drawable to ImageView */ private fun refresh() { - multiDrawable = MultiDrawable(bitmaps) + multiDrawable = MultiDrawable(bitmaps, dividerWidth) setImageDrawable(multiDrawable) } @@ -89,6 +110,7 @@ class MultiImageView(context: Context, attrs: AttributeSet) : ImageView(context, path.addOval(rect, Path.Direction.CW) } //Clip with shape + canvas.drawPath(path, paintDivider) canvas.clipPath(path) } super.onDraw(canvas) @@ -102,10 +124,12 @@ class MultiImageView(context: Context, attrs: AttributeSet) : ImageView(context, } } -class MultiDrawable(val bitmaps: ArrayList) : Drawable() { +class MultiDrawable(val bitmaps: ArrayList, val dividerWidth: Float) : Drawable() { private val paint = Paint(Paint.ANTI_ALIAS_FLAG) private val items = ArrayList() + fun halfDivider(): Int = (dividerWidth / 2).toInt() + /** * Create PhotoItem with position and size depends of count of images */ @@ -117,25 +141,25 @@ class MultiDrawable(val bitmaps: ArrayList) : Drawable() { } else if (bitmaps.size == 2) { val bitmap1 = scaleCenterCrop(bitmaps[0], bounds.width(), bounds.height() / 2) val bitmap2 = scaleCenterCrop(bitmaps[1], bounds.width(), bounds.height() / 2) - items.add(PhotoItem(bitmap1, Rect(0, 0, bounds.width() / 2, bounds.height()))) - items.add(PhotoItem(bitmap2, Rect(bounds.width() / 2, 0, bounds.width(), bounds.height()))) + items.add(PhotoItem(bitmap1, Rect(0, 0, (bounds.width() / 2) - halfDivider(), bounds.height()))) + items.add(PhotoItem(bitmap2, Rect((bounds.width() / 2) + halfDivider(), 0, bounds.width(), bounds.height()))) } else if (bitmaps.size == 3) { val bitmap1 = scaleCenterCrop(bitmaps[0], bounds.width(), bounds.height() / 2) val bitmap2 = scaleCenterCrop(bitmaps[1], bounds.width() / 2, bounds.height() / 2) val bitmap3 = scaleCenterCrop(bitmaps[2], bounds.width() / 2, bounds.height() / 2) - items.add(PhotoItem(bitmap1, Rect(0, 0, bounds.width() / 2, bounds.height()))) - items.add(PhotoItem(bitmap2, Rect(bounds.width() / 2, 0, bounds.width(), bounds.height() / 2))) - items.add(PhotoItem(bitmap3, Rect(bounds.width() / 2, bounds.height() / 2, bounds.width(), bounds.height()))) + items.add(PhotoItem(bitmap1, Rect(0, 0, (bounds.width() / 2) - halfDivider(), bounds.height()))) + items.add(PhotoItem(bitmap2, Rect((bounds.width() / 2) + halfDivider(), 0, bounds.width(), (bounds.height() / 2) - halfDivider()))) + items.add(PhotoItem(bitmap3, Rect((bounds.width() / 2) + halfDivider(), (bounds.height() / 2) + halfDivider(), bounds.width(), bounds.height()))) } if (bitmaps.size == 4) { val bitmap1 = scaleCenterCrop(bitmaps[0], bounds.width() / 2, bounds.height() / 2) val bitmap2 = scaleCenterCrop(bitmaps[1], bounds.width() / 2, bounds.height() / 2) val bitmap3 = scaleCenterCrop(bitmaps[2], bounds.width() / 2, bounds.height() / 2) val bitmap4 = scaleCenterCrop(bitmaps[3], bounds.width() / 2, bounds.height() / 2) - items.add(PhotoItem(bitmap1, Rect(0, 0, bounds.width() / 2, bounds.height() / 2))) - items.add(PhotoItem(bitmap2, Rect(0, bounds.height() / 2, bounds.width() / 2, bounds.height()))) - items.add(PhotoItem(bitmap3, Rect(bounds.width() / 2, 0, bounds.width(), bounds.height() / 2))) - items.add(PhotoItem(bitmap4, Rect(bounds.width() / 2, bounds.height() / 2, bounds.width(), bounds.height()))) + items.add(PhotoItem(bitmap1, Rect(0, 0, (bounds.width() / 2) - halfDivider(), (bounds.height() / 2) - halfDivider()))) + items.add(PhotoItem(bitmap2, Rect(0, (bounds.height() / 2) + halfDivider(), (bounds.width() / 2) - halfDivider(), bounds.height()))) + items.add(PhotoItem(bitmap3, Rect((bounds.width() / 2) + halfDivider(), 0, bounds.width(), (bounds.height() / 2) - halfDivider()))) + items.add(PhotoItem(bitmap4, Rect((bounds.width() / 2) + halfDivider(), (bounds.height() / 2) + halfDivider(), bounds.width(), bounds.height()))) } } @@ -176,5 +200,4 @@ class MultiDrawable(val bitmaps: ArrayList) : Drawable() { paint.colorFilter = colorFilter } //***------------------***// -} - +} \ No newline at end of file