-
Notifications
You must be signed in to change notification settings - Fork 57
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() { | ||
|
||
private var ivFile: ImageView? = null | ||
private var ivUrl: ImageView? = null | ||
private var ivDrawable: ImageView? = null | ||
Comment on lines
+24
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?, | ||
|
@@ -18,4 +33,69 @@ class CoilFragment : Fragment() { | |
return inflater.inflate(R.layout.fragment_coil, container, false) | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
amaan118921 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
super.onViewCreated(view, savedInstanceState) | ||
|
||
// initializing the imageViews | ||
amaan118921 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) | ||
} | ||
|
||
} |
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> |
There was a problem hiding this comment.
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
kotlin-libraries-playground/kotlin-jvm/src/main/kotlin/playground/Markdown.kt
Lines 17 to 25 in ff3ae6a
There was a problem hiding this comment.
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.