Skip to content

Commit

Permalink
Merge pull request #79 from Trendyol/addCornerRadiusForImageSlider
Browse files Browse the repository at this point in the history
add corner radius in dp
  • Loading branch information
belfuogretir authored Jul 27, 2021
2 parents 30ff63d + 73eccfc commit c9ba131
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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.*
Expand All @@ -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

Expand All @@ -30,8 +35,13 @@ internal fun <T : ViewDataBinding> 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
Expand Down Expand Up @@ -97,3 +107,5 @@ internal fun ColorDrawable.createFadeOut(): ObjectAnimator {
0
)
}

internal fun dpToPx(dp: Double): Int = (dp * Resources.getSystem().displayMetrics.density).toInt()
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class ImagesPagerAdapter(
private val itemClickListener: SliderAdapterItemClickListener,
private val scaleType: ImageView.ScaleType,
private val imageUrlList: List<String>,
@ColorInt private val backgroundColor: Int
@ColorInt private val backgroundColor: Int,
private val cornerRadiusInDp: Double? = null
) : PagerAdapter() {

private var binding: ViewImageBinding? = null
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
)
)
}
Expand Down

0 comments on commit c9ba131

Please sign in to comment.