下面是一个示例代码,展示如何使用Glide加载网络图片并自适应ImageView的宽高。

首先,确保在项目的build.gradle文件中添加Glide依赖库:

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.13.0'
    kapt 'com.github.bumptech.glide:compiler:4.13.0'
}

接下来,创建一个自定义的ImageView类,继承自androidx.appcompat.widget.AppCompatImageView:

import android.content.Context
import android.util.AttributeSet
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition
import androidx.appcompat.widget.AppCompatImageView

class CustomImageView : AppCompatImageView {

    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    fun loadUrl(url: String) {
        Glide.with(this)
            .load(url)
            .into(object : Target<Drawable> {
                override fun onLoadStarted(placeholder: Drawable?) {
                    // 在图片加载开始前,可以设置一个加载中的占位图
                    setImageDrawable(placeholder)
                }

                override fun onLoadFailed(errorDrawable: Drawable?) {
                    // 在图片加载失败时,可以设置一个加载失败的占位图
                    setImageDrawable(errorDrawable)
                }

                override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
                    // 在图片加载成功后,将图片设置到ImageView中
                    setImageDrawable(resource)
                }

                override fun onLoadCleared(placeholder: Drawable?) {
                    // 图片加载被清除时的回调
                }

                override fun getSize(cb: SizeReadyCallback) {
                    // 在这里可以设置ImageView的宽高,例如:
                    cb.onSizeReady(500, 500)
                }

                override fun removeCallback(cb: SizeReadyCallback) {
                    // 移除回调
                }

                override fun setRequest(request: Request?) {
                    // 设置请求
                }

                override fun getRequest(): Request? {
                    // 获取请求
                    return null
                }

                override fun onStart() {
                    // 在图片加载开始前的回调
                }

                override fun onStop() {
                    // 在图片加载停止时的回调
                }

                override fun onDestroy() {
                    // 在图片加载销毁时的回调
                }
            })
    }
}

在上面的代码中,loadUrl方法用于加载网络图片,并根据需要设置ImageView的宽高。在getSize方法中,可以设置ImageView的宽高,这里是设置为500x500。你可以根据实际需求进行修改。

最后,在布局文件中使用CustomImageView,并调用loadUrl方法加载网络图片:

<com.example.app.CustomImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitCenter" />
val imageView = findViewById<CustomImageView>(R.id.imageView)
imageView.loadUrl("https://example.com/image.jpg")

以上就是使用Glide加载网络图片并自适应ImageView宽高的详细代码与使用方法。

Android kotlin 自定义ImageView 使用glide4130加载网络图片自适应宽高详细代码与使用

原文地址: https://www.cveoy.top/t/topic/i27p 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录