Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added How to use Coil Image Loading Library. #117

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ android {
}

dependencies {

implementation(COIL.compose)
implementation(Kotlin.stdlib)
implementation(AndroidX.core.ktx)
implementation(AndroidX.appCompat)
Expand Down
82 changes: 81 additions & 1 deletion android/src/main/java/playground/android/CoilFragment.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package playground.android

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.Fragment
import coil.load
import coil.transform.CircleCropTransformation
import coil.transform.RoundedCornersTransformation
import java.io.File



/**
* coil-kt/coil - An image loading library for Android backed by Kotlin Coroutines

- [Website](https://coil-kt.github.io/coil/)
- [GitHub coil-kt/coil](https://github.com/coil-kt/coil)
*/
class CoilFragment : Fragment() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add links to the project repo and documentation?
That will greatly add new comers

/**
* jetbrains/markdown : Multiplatform Markdown processor written in Kotlin
*
* - [Website](https://github.com/JetBrains/markdown)
* - [Github](https://github.com/JetBrains/markdown)
*/
fun main(){
generateHtml()
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have now added the references.


private var ivFile: ImageView? = null
private var ivUrl: ImageView? = null
private var ivDrawable: ImageView? = null
Comment on lines +24 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LouisCAD do you think those views should be nullable? I know nothing about Android

Copy link
Author

@amaan118921 amaan118921 Oct 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If not nullable, we can initialize them later, but if we use lateinit, there might be possibility of views not being initialized, which would lead to crash of the application, so, i think this nullable would handle it correctly, since we are using a null operator while accessing the views.


override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand All @@ -18,4 +33,69 @@ class CoilFragment : Fragment() {
return inflater.inflate(R.layout.fragment_coil, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

// initializing the imageViews
initializeImages()

//Loading from a URL
loadFromURL()

//Loading from an image drawable
loadFromDrawable()

// Loading from a file
loadFromFile()

//adding placeholder to loading images
addingPlaceholder()

//applying some basic transformations to image using coil

// Crops and centres the image into a circle.
circleCropTransformation()

// Crops the image to fit the target's dimensions and rounds the corners of the image.
roundedCornerTransformation()


}

private fun roundedCornerTransformation() {
ivUrl?.load("https://via.placeholder.com/600/92c952") {
transformations(RoundedCornersTransformation())
}
}

private fun circleCropTransformation() {
ivUrl?.load("https://via.placeholder.com/600/92c952") {
transformations(CircleCropTransformation())
}
}

private fun addingPlaceholder() {
ivUrl?.load("https://via.placeholder.com/600/92c952") {
placeholder(R.drawable.placeholder_img)
}
}

private fun loadFromFile() {
ivFile?.load(File("/path/to/some_image_placeholder.png"))
}

private fun loadFromDrawable() {
ivDrawable?.load(R.drawable.placeholder_img)
}

private fun loadFromURL() {
ivUrl?.load("https://via.placeholder.com/600/92c952")
}

private fun initializeImages() {
ivFile = view?.findViewById(R.id.ivFile)
ivUrl = view?.findViewById(R.id.ivUrl)
ivDrawable = view?.findViewById(R.id.ivDrawable)
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 22 additions & 8 deletions android/src/main/res/layout/fragment_coil.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CoilFragment">


<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />

</FrameLayout>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_above="@id/ivDrawable"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:id="@+id/ivFile" />
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentEnd="true"
android:layout_marginEnd="50dp"
android:layout_centerVertical="true"
android:id="@+id/ivUrl" />
<ImageView
android:layout_centerVertical="true"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginStart="50dp"
android:id="@+id/ivDrawable" />
</RelativeLayout>
1 change: 1 addition & 0 deletions kotlin-jvm/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ dependencies {
implementation("com.apollographql.apollo:apollo-runtime:_")
implementation(KotlinX.serialization.json)
implementation(KotlinX.serialization.properties)

// Keep dependencies sorted to minimize merge conflicts on pull-requests!
}

Expand Down