Android GlideApp FixedPreloadSizeProvider RecyclerViewPreloader,mix Java&Kotlin

Android GlideApp FixedPreloadSizeProvider RecyclerViewPreloader,mix Java&Kotlin

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

    implementation 'com.github.bumptech.glide:glide:4.15.1'
    kapt 'com.github.bumptech.glide:compiler:4.15.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.15.1'
    implementation ("com.github.bumptech.glide:recyclerview-integration:4.15.1") {
        // Excludes the support library because it's already included by Glide.
        transitive = false
    }

import android.content.Context
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.provider.MediaStore
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class MainActivity : AppCompatActivity() {
    private var mRequest: GlideRequest<Drawable>? = null

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

        mRequest = GlideApp.with(this)
            .asDrawable()
            .centerCrop()
            .override(RVPreload.PHOTO_SIZE)

        rvPreload()
    }

    private fun rvPreload() {
        val spanCount = 5

        var recyclerView: RecyclerView = findViewById(R.id.recycler_view)
        recyclerView?.layoutManager = GridLayoutManager(this, spanCount).apply {
            orientation = GridLayoutManager.VERTICAL
        }

        val adapter = MyAdapter(mRequest, this)
        recyclerView?.adapter = adapter


        CoroutineScope(Dispatchers.IO).launch {
            val items = readAllImage(applicationContext)

            withContext(Dispatchers.Main) {
                adapter.onChange(items)
            }
        }

        var preloader = RVPreload(adapter, this)
        preloader.attach(recyclerView)
    }

    private fun readAllImage(context: Context): ArrayList<MyData> {
        val photos = ArrayList<MyData>()

        //读取手机图片
        val cursor = context.contentResolver.query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            null,
            null,
            null,
            null
        )
        var index = 0
        while (cursor!!.moveToNext()) {
            //图片路径 uri
            val path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA))

            //图片名称
            //val name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME))

            //图片大小
            //val size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE))

            photos.add(MyData(path, index++))
        }
        cursor.close()

        return photos
    }
}
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" />

</androidx.constraintlayout.widget.ConstraintLayout>

import android.content.Context
import android.graphics.drawable.Drawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.recyclerview.widget.RecyclerView
import java.io.File


class MyAdapter(private val fullRequest: GlideRequest<Drawable>?, private val ctx: Context) :
    RecyclerView.Adapter<MyViewHolder>() {
    private var items: MutableList<MyData>? = null

    fun onChange(items: MutableList<MyData>) {
        this.items = items
        notifyDataSetChanged()
    }

    fun getItems(): MutableList<MyData> {
        return items!!
    }

    override fun getItemId(i: Int): Long {
        return RecyclerView.NO_ID
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(ctx)
            .inflate(R.layout.item, parent, false)
        val params = view.layoutParams
        params.width = RVPreload.PHOTO_SIZE
        params.height = RVPreload.PHOTO_SIZE
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        fullRequest?.load(File(items?.get(position)?.path))?.centerCrop()?.into(holder.image)
        holder.text.text = "$position"
    }

    override fun getItemCount(): Int {
        return items?.size ?: 0
    }
}


class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val image: AppCompatImageView = itemView.findViewById(R.id.image)
    val text: TextView = itemView.findViewById(R.id.text)
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="1dp">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/image"
        android:layout_width="80dp"
    android:scaleType="centerCrop"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher_background" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="-.-"
        android:textColor="@android:color/holo_red_dark"
        android:textSize="20dp" />
</RelativeLayout>

public class MyData{
    public String path;
    public int index;

    public MyData(String path,int index){
        this.path=path;
        this.index=index;
    }
}

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.module.AppGlideModule;


@GlideModule
public class MyModule extends AppGlideModule {
    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        builder.setLogLevel(Log.DEBUG);
    }

    @Override
    public boolean isManifestParsingEnabled() {
        return false;
    }
}

import android.content.Context;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.ListPreloader;
import com.bumptech.glide.integration.recyclerview.RecyclerViewPreloader;
import com.bumptech.glide.util.FixedPreloadSizeProvider;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class RVPreload implements ListPreloader.PreloadModelProvider<MyData> {
    public static int PHOTO_SIZE = 180;

    private MyAdapter mAdapter;

    private Context context;

    private int maxPreload = 500;

    private FixedPreloadSizeProvider<MyData> preloadSizeProvider = new FixedPreloadSizeProvider(PHOTO_SIZE, PHOTO_SIZE);

    public void attach(RecyclerView rv) {
        RecyclerViewPreloader<MyData> recyclerViewPreloader = new RecyclerViewPreloader<>(
                GlideApp.with(context),
                this,
                preloadSizeProvider,
                maxPreload
        );

        rv.addOnScrollListener(recyclerViewPreloader);
    }

    public RVPreload(MyAdapter adapter, Context context) {
        this.mAdapter = adapter;
        this.context = context;
    }

    @NonNull
    @Override
    public List<MyData> getPreloadItems(int position) {
        List<MyData> list = new ArrayList<>();
        list.add(mAdapter.getItems().get(position));
        return list;
    }

    @Nullable
    @Override
    public GlideRequest<Drawable> getPreloadRequestBuilder(@NonNull MyData item) {
        return GlideApp.with(context).asDrawable().load(new File(item.path)).centerCrop();
    }
}

Android GlideApp FixedPreloadSizeProvider RecyclerViewPreloader preload scroll smooth,Java_zhangphil的博客-CSDN博客【代码】Android Paging 3,kotlin(1)在实际的开发中,虽然Glide解决了快速加载图片的问题,但还有一个问题悬而未决:比如用户的头像,往往用户的头像是从服务器端读出的一个普通矩形图片,但是现在的设计一般要求在APP端的用户头像显示成圆形头像,那么此时虽然Glide可以加载,但加载出来的是一个矩形,如果要Glide_android 毛玻璃圆角。《Android图片加载与缓存开源框架:Android Glide》Android Glide是一个开源的图片加载和缓存处理的第三方框架。https://blog.csdn.net/zhangphil/article/details/131884306Android GlideApp GlideRequest FixedPreloadSizeProvider RecyclerViewPreloader,kotlin_zhangphil的博客-CSDN博客【代码】Android Paging 3,kotlin(1)在实际的开发中,虽然Glide解决了快速加载图片的问题,但还有一个问题悬而未决:比如用户的头像,往往用户的头像是从服务器端读出的一个普通矩形图片,但是现在的设计一般要求在APP端的用户头像显示成圆形头像,那么此时虽然Glide可以加载,但加载出来的是一个矩形,如果要Glide_android 毛玻璃圆角。《Android图片加载与缓存开源框架:Android Glide》Android Glide是一个开源的图片加载和缓存处理的第三方框架。https://blog.csdn.net/zhangphil/article/details/131813200

猜你喜欢

转载自blog.csdn.net/zhangphil/article/details/131905329