mysql first使用_如何使用Cotlin First Image Loader线圈

mysql first使用

文章的重点 (The Takeaway From the Article)

By the end of this article, you’ll learn how to use the new image loader from the Instacart team. Along with the generic usage, you can also gain knowledge about transformations, request canceling, and image sampling features of Coil. Last but not least, there’s a migration guide from Glide and Picasso to Coil.

在本文的最后,您将学习如何使用Instacart团队的新图像加载器。 除了通用用法外,您还可以获得有关Coil的变换,请求取消和图像采样功能的知识。 最后但并非最不重要的一点是,有一个从Glide和Picasso到Coil的迁移指南。

介绍 (Introduction)

Coil is the new image loader from the Instacart team, using many advanced features like coroutines, OkHttp, and androidX.lifecycles. Coil adds around 1,500 functions to your APK, which is comparable to Picasso and less then Glide and Fresco. Coil also includes several advanced features, like image sampling, effective memory usage, and automatic canceling/pausing of requests.

Coil是Instacart团队的新图像加载器,使用了协程,OkHttp和androidX.lifecycle等许多高级功能。 Coil为您的APK添加了约1,500个功能,与Picasso相当,而Glide和Fresco则更少。 Coil还包括一些高级功能,例如图像采样,有效的内存使用以及请求的自动取消/暂停。

Coil, by default, is fully compatible with R8 optimization techniques, so there is no need for developers to add any ProGuard rules related to Coil.

默认情况下,Coil与R8优化技术完全兼容,因此开发人员无需添加任何与Coil相关的ProGuard规则。

积分 (Integration)

Coil requires Java 8-byte code, so add the following lines in your build.gradle file to make it work.

Coil需要Java 8字节代码,因此请在build.gradle文件中添加以下几行以使其起作用。

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}


tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

Coil has four artifacts; each has its purpose. But mainly we use the following two :

线圈有四个工件。 每个都有其目的。 但是主要我们使用以下两个:

  1. io.coil-kt: Coil: The default artifact, which has the Coil singleton. If you’re not using dependency injection or maintaining any instance of Coil across your app, then it’s better to use this artifact.

    io.coil-kt: Coil :默认工件,具有Coil单例。 如果您没有在应用程序中使用依赖项注入或维护任何Coil实例,那么最好使用此工件。

  2. io.coil-kt:coil-base: The base artifact, which does not include the Coil singleton. Prefer this artifact if you’re using dependency injection to inject Coil instance(s).

    io.coil-kt:coil-base :基本工件, 包括Coil单例。 如果您正在使用依赖项注入来注入Coil实例,则最好使用此工件。

//Singleton artifact
implementation "io.coil-kt:coil:0.9.5"
//Base artifact without singleton
implementation "io.coil-kt:coil-base:0.9.5"

功能性 (Functionality)

Now that we’ve integrated the library successfully, it’s time to put Coil in action. Let’s start by loading a simple remote image. Have a look:

现在我们已经成功集成了库,是时候将Coil付诸实践了。 让我们从加载一个简单的远程映像开始。 看一看:

imageView.load("https://www.example.com/image.jpg")

As Coil is the first image loader that was developed in Kotlin, it took the leverage of using many of its advanced features, and one of them is extensions. The load is an extension function from Coil, which makes it simple to load a remote image than any other library.

由于Coil是Kotlin开发的第一款图像加载器,因此它利用了许多高级功能,其中之一就是扩展功能。 load是Coil的extension功能,与其他任何库相比,它使加载远程映像更加容易。

With the load extension, Coil saved us the time of passing the context and creating the instance of an imageloader all the time. Have a look at the extension from Coil:

通过load扩展,Coil一直为我们节省了传递上下文和创建imageloader实例的时间。 看一下Coil的扩展名:

inline fun ImageView.load(
    uri: String?,
    imageLoader: ImageLoader = Coil.loader(),
    builder: LoadRequestBuilder.() -> Unit = {}
): RequestDisposable {
    return imageLoader.load(context, uri) {
        target(this@load)
        builder()
    }
}

It took the leverage of taking context from imageView and creating a Coil loader. This is why native language libraries are preferable.

它利用了从imageView获取上下文并创建Coil loader的优势。 这就是为什么首选本地语言库的原因。

占位符和错误图片 (Placeholder and error images)

Almost all libraries like Glide, Picasso, and Fresco have this feature — showing a dummy image until the original image load into the view (placeholder) and showing an error image if the request fails (error). Coil also has this feature out of the box, and the exciting part is the syntax: Implementing them is more immersed with language syntax like let and apply features in Kotlin. Have a look:

几乎所有的库(如Glide,Picasso和Fresco)都具有此功能-在原始图像加载到视图之前显示一个虚拟图像(占位符),并在请求失败时显示一个错误图像(错误)。 Coil还具有开箱即用的功能,而令人兴奋的部分是语法:在Kotlin中,实现它们更沉浸在语言语法中,例如letapply功能。 看一看:

fun loadWithCoil(imageView: ImageView, imageUrl: String){
    imageView.load(imageUrl){
        placeholder(R.drawable.placeholder)
        error(R.drawable.error)
    }
}

预载图片 (Preload images)

Coil makes use of coroutines to download the images more effectively. We can download a remote image using the get suspend function. Have a look:

Coil利用协同程序更有效地下载图像。 我们可以使用get挂起功能下载远程映像。 看一看:

val image = Coil.get(imageUrl)

With the get suspend function, we can download a remote image as drawable.

使用get暂停功能,我们可以将远程图像下载为drawable

回呼 (Callbacks)

There will be some cases where you need a callback after a remote image is downloaded. Coil covers it with the targets feature. Have a look:

在某些情况下,下载远程映像后需要回调。 线圈具有targets功能。 看一看:

fun coilWithCallbacks(){
    Coil.load(context, "https://www.example.com/image.jpg") {
        target { drawable ->
            // Handle the successful result.
        }
    }
}

There are three types of targets: Target, ViewTarget, and possibleViewTarget.

目标分为三种: TargetViewTargetpossibleViewTarget ViewTarget

  • Target is used when the request isn’t linked to any view.

    当请求未链接到任何视图时,将使用Target

  • ViewTarget is used when the request is linked to an imageview, such as showing a placeholder until the request is completed.

    当请求链接到imageview时使用ViewTarget ,例如显示占位符直到请求完成。

  • possibleViewTarget is used if there is a need for bitmap pooling.

    possibleViewTarget如果有需要的位图池使用。

转变 (Transformations)

Now that we’re done with different kinds of image loading techniques from Coil, it’s time to explore transformations. Transformation is a handy functionality that any image loader should contain. Out of the box, Coil comes with four transformations: blur, circle crop, grayscale, and rounded corners. The below code demonstrates the usage:

现在,我们已经完成了Coil提供的各种图像加载技术的准备,现在该探讨转换了。 转换是任何图像加载器都应包含的便捷功能。 开箱即用,Coil带有四个转换: 模糊 , 圆形裁剪 , 灰度和圆角 。 以下代码演示了用法:

fun loadWithCoil(imageView: ImageView, imageUrl: String){
    imageView.load(imageUrl){
        placeholder(R.drawable.gradient_place_details)
        error(R.drawable.gradient_place_details)
        transformations(CircleCropTransformation())
    }
}

取消请求 (Cancel Request)

Effectively utilizing the resources is the key to the performance of the app. Coil, by default, cancels the request when the imageview is detached, or the context is destroyed, or another request is started with the view. Coil covers all the cases of memory leaks by default, but there will be situations when you want to cancel the request. For that purpose, Coil returns RequestDisposable for every load request through which you can cancel the request. Have a look:

有效利用资源是应用程序性能的关键。 默认情况下,当分离imageview视图,破坏上下文或使用该视图启动另一个请求时,线圈默认会取消该请求。 默认情况下,Coil涵盖所有内存泄漏的情况,但是在某些情况下,您想取消请求。 为此,Coil对每个加载请求都返回RequestDisposable ,您可以通过该请求取消请求。 看一看:

val disposable = imageView.load("https://www.example.com/image.jpg")
// Cancel the request.
disposable.dispose()

图像取样 (Image Sampling)

Image sampling is the most advanced technique used to load the images with quality based on the size of the imageview. Suppose there is an image of size 500 x 500 on disk. Initially, Coil loads 100 x 100 and uses it as a placeholder until full quality is loaded. It works as a progressive JPEG. We can enable this while loading any imageloader request from Coil through crossfade features. Have a look:

图像采样是最先进的技术,用于根据imageview的大小加载具有质量的imageview 。 假设磁盘上有一个大小为500 x 500的映像。 最初,线圈加载100 x 100并将其用作占位符,直到加载完整质量。 它用作渐进式JPEG 。 我们可以通过crossfade功能从Coil加载任何imageloader加载imageloader请求时启用此功能。 看一看:

imageView.load(imageUrl){
crossfade
(true)}

从滑翔/毕加索的迁移 (Migration from Glide/Picasso)

The Coil is an easy-to-use Kotlin native image loader. It covers all basic and advanced usages with extensions to make it simple for developers. Now, let’s compare the image loading code between Glide, Picasso, and Coil.

Coil是易于使用的Kotlin本机图像加载器。 它涵盖了扩展的所有基本用法和高级用法,使开发人员可以轻松使用。 现在,让我们比较一下Glide,毕加索和Coil之间的图像加载代码。

基本用法: (Basic usage:)

// GlideGlide.with(context)
.load(url)
.into(imageView)
// PicassoPicasso.get()
.load(url)
.into(imageView)
// Coil
imageView.load(url)

回调请求 (Callback requests)

// Glide (has optional callbacks for start and error)
Glide.with(context)
    .load(url)
    .into(object : CustomTarget() {
        override fun onResourceReady(resource: Drawable, transition: Transition) {
            // Handle the successful result.
        }


        override fun onLoadCleared(placeholder: Drawable) {
            // Remove the drawable provided in onResourceReady from any Views and ensure no references to it remain.
        }
    })


// Picasso
Picasso.get()
    .load(url)
    .into(object : BitmapTarget {
        override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom) {
            // Handle the successful result.
        }


        override fun onBitmapFailed(e: Exception, errorDrawable: Drawable?) {
            // Handle the error drawable.
        }


        override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
            // Handle the placeholder drawable.
        }
    })


// Coil (has optional callbacks for start and error)
Coil.load(context, url) {
    target { drawable ->
        // Handle the successful result.
    }
}

自订要求 (Custom requests)

imageView.scaleType = ImageView.ScaleType.FIT_CENTER


// Glide
Glide.with(context)
    .load(url)
    .placeholder(placeholder)
    .fitCenter()
    .into(imageView)


// Picasso
Picasso.get()
    .load(url)
    .placeholder(placeholder)
    .fit()
    .into(imageView)


// Coil (autodetects the scale type)
imageView.load(url) {
    placeholder(placeholder)
}

奖金 (Bonus)

To learn more about Kotlin, read the previous parts of this Advanced Programming With Kotlin series:

要了解有关Kotlin的更多信息,请阅读“使用Kotlin进行高级编程”系列的先前部分:

  • “Advanced Programming With Kotlin”

    “使用Kotlin进行高级编程 ”

  • “Advanced Programming With Kotlin — Part 2”

    “使用Kotlin进行高级编程-第2部分 ”

  • “Advanced Programming With Kotlin — Part 3”

    “使用Kotlin进行高级编程-第3部分 ”

  • “Advanced Android Programing in Kotlin — Part 4”

    “ Kotlin中的高级Android编程-第4部分”

To learn more about Kotlin Coroutines and other advanced features of Kotlin, read the following articles:

要了解有关Kotlin协程和Kotlin的其他高级功能的更多信息,请阅读以下文章:

  • “Kotlin Coroutines, From the Basic to the Advanced”

    “ Kotlin协程,从基础到高级 ”

  • “How to Use Kotlin Sealed Classes for State Management”

    “如何使用Kotlin密封类进行状态管理”

  • “Asynchronous Data Loading With New Kotlin Flow”

    “使用新的Kotlin Flow进行异步数据加载 ”

  • “Exploring Collections and Sequences in Kotlin”

    “探索Kotlin的藏品和序列”

  • Why and How to Use Kotlin’s Native Serialization Library

    为什么和如何使用Kotlin的本机序列化库

  • Learn How to Combine Kotlin flows

    了解如何结合Kotlin流程

Thank you for reading.

感谢您的阅读。

翻译自: https://medium.com/better-programming/how-to-use-coil-kotlins-native-image-loader-d6715dda7d26

mysql first使用

你可能感兴趣的:(python)