Thursday, August 20, 2020

android kotlin - ImageView border radius

MainActivity.kt

package com.example.jetpack

import android.content.Context
import android.graphics.*
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.util.TypedValue
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import java.io.IOException


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // get bitmap from assets
        val bitmap: Bitmap? = assetsToBitmap("flower5.jpg")

        // set image view images from assets
        bitmap?.apply {
            imageView.setImageBitmap(this)
            imageView2.setImageBitmap(this)
        }

        // programmatically add rounded corners border to image view
        imageView2.addRoundedBorder(
            borderColor = Color.parseColor("#D3212D"),
            borderWidth = 5.dpToPixels(applicationContext),
            radius = 10.dpToPixels(applicationContext).toFloat()
        )
    }
}


// extension function to add rounded corner border to image view
fun ImageView.addRoundedBorder(
    borderColor:Int = Color.BLACK,
    borderWidth: Int = 15,
    radius: Float = 40F
){
    // initialize a new gradient drawable instance as image view border
    val gradientDrawable = GradientDrawable()

    // apply the the border properties
    gradientDrawable.apply {
        setStroke(borderWidth,borderColor)
        cornerRadius = radius
    }

    // set image view padding to show border
    val padding = borderWidth * 3
    setPadding(padding,padding,padding,padding)

    // finally, add the rounded corners border to image view
    background = gradientDrawable
}


// extension function to convert dp to equivalent pixels
fun Int.dpToPixels(context: Context):Int = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,this.toFloat(),context.resources.displayMetrics
).toInt()


// extension function to get bitmap from assets
fun Context.assetsToBitmap(fileName: String): Bitmap?{
    return try {
        with(assets.open(fileName)){
            BitmapFactory.decodeStream(this)
        }
    } catch (e: IOException) { null }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#EDEAE0"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="290dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:background="@drawable/image_view_border"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="290dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        tools:srcCompat="@tools:sample/avatars" />

</androidx.constraintlayout.widget.ConstraintLayout>
res/drawable/image_view_border.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- we can set a background color by uncomment this line -->
    <!--solid android:color="#fff"/-->

    <!-- set border corner radius -->
    <corners android:radius="10dp"/>

    <!-- add a border -->
    <stroke
        android:width="5dp"
        android:color="#4D1A7F" />
    <padding
        android:left="15dp"
        android:top="15dp"
        android:right="15dp"
        android:bottom="15dp" />
</shape>