diff --git a/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/Extensions.kt b/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/Extensions.kt index 806b8285..73715988 100644 --- a/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/Extensions.kt +++ b/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/Extensions.kt @@ -1,6 +1,7 @@ package com.trendyol.uicomponents.imageslider import android.animation.ObjectAnimator +import android.content.res.Resources import android.graphics.Point import android.graphics.drawable.ColorDrawable import android.view.* @@ -10,6 +11,10 @@ import androidx.annotation.LayoutRes import androidx.databinding.DataBindingUtil import androidx.databinding.ViewDataBinding import com.bumptech.glide.Glide +import com.bumptech.glide.load.resource.bitmap.CenterCrop +import com.bumptech.glide.load.resource.bitmap.FitCenter +import com.bumptech.glide.load.resource.bitmap.RoundedCorners +import com.bumptech.glide.request.RequestOptions import kotlin.math.pow import kotlin.math.sqrt @@ -30,8 +35,13 @@ internal fun ViewGroup?.inflate( ) } -fun ImageView.loadImage(imageUrl: String) { - Glide.with(context).load(imageUrl).into(this) +fun ImageView.loadImage(imageUrl: String, cornerRadiusInDp: Double?) { + val requestBuilder = Glide.with(context).load(imageUrl) + if (cornerRadiusInDp == null || cornerRadiusInDp == 0.0) { + requestBuilder + } else { + requestBuilder.transform(RoundedCorners(dpToPx(cornerRadiusInDp))) + }.into(this) } internal val RETURN_ANIM_DURATION = 350 @@ -97,3 +107,5 @@ internal fun ColorDrawable.createFadeOut(): ObjectAnimator { 0 ) } + +internal fun dpToPx(dp: Double): Int = (dp * Resources.getSystem().displayMetrics.density).toInt() diff --git a/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/ImageSliderView.kt b/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/ImageSliderView.kt index 640bcecc..7371af68 100644 --- a/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/ImageSliderView.kt +++ b/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/ImageSliderView.kt @@ -47,7 +47,8 @@ class ImageSliderView @JvmOverloads constructor( itemClickListener = this, scaleType = viewState.scaleType, imageUrlList = viewState.imageList, - backgroundColor = viewState.backgroundColor + backgroundColor = viewState.backgroundColor, + cornerRadiusInDp = viewState.cornerRadiusInDp ) binding.viewPagerImageSlider.adapter = pagerAdapter binding.indicatorImageSlider.setViewPager(binding.viewPagerImageSlider) diff --git a/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/ImageSliderViewState.kt b/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/ImageSliderViewState.kt index c95e63b9..4de08fc0 100644 --- a/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/ImageSliderViewState.kt +++ b/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/ImageSliderViewState.kt @@ -11,6 +11,7 @@ data class ImageSliderViewState( var imageHeight: Int? = null, var isIndicatorAlwaysVisible: Boolean = false, val scaleType: ImageView.ScaleType = ImageView.ScaleType.CENTER_CROP, + val cornerRadiusInDp: Double? = null, @ColorInt val backgroundColor: Int = Color.WHITE ) { diff --git a/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/ImagesPagerAdapter.kt b/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/ImagesPagerAdapter.kt index 69610df7..3bd4f572 100644 --- a/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/ImagesPagerAdapter.kt +++ b/libraries/image-slider/src/main/java/com/trendyol/uicomponents/imageslider/ImagesPagerAdapter.kt @@ -18,7 +18,8 @@ class ImagesPagerAdapter( private val itemClickListener: SliderAdapterItemClickListener, private val scaleType: ImageView.ScaleType, private val imageUrlList: List, - @ColorInt private val backgroundColor: Int + @ColorInt private val backgroundColor: Int, + private val cornerRadiusInDp: Double? = null ) : PagerAdapter() { private var binding: ViewImageBinding? = null @@ -75,7 +76,7 @@ class ImagesPagerAdapter( private fun loadImage(imageView: ImageView, url: String?) { if (url == null) return - imageView.loadImage(url) + imageView.loadImage(url, cornerRadiusInDp) initializeZoomableView(imageView) } diff --git a/sample/src/main/java/com/trendyol/uicomponents/ImageSliderActivity.kt b/sample/src/main/java/com/trendyol/uicomponents/ImageSliderActivity.kt index eed7847d..3d9a99fa 100644 --- a/sample/src/main/java/com/trendyol/uicomponents/ImageSliderActivity.kt +++ b/sample/src/main/java/com/trendyol/uicomponents/ImageSliderActivity.kt @@ -1,6 +1,7 @@ package com.trendyol.uicomponents import android.os.Bundle +import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity import androidx.databinding.DataBindingUtil import com.trendyol.uicomponents.databinding.ActivityImageSliderBinding @@ -23,7 +24,9 @@ class ImageSliderActivity : AppCompatActivity() { "https://picsum.photos/id/1027/1920/1280", "https://picsum.photos/id/1028/1920/1280", "https://picsum.photos/id/1029/1920/1280" - ) + ), + cornerRadiusInDp = 8.0, + scaleType = ImageView.ScaleType.FIT_CENTER ) ) }