Android GlideApp FixedPreloadSizeProvider RecyclerViewPreloader,mix Java&Kotlin

Android GlideApp FixedPreloadSizeProvider RecyclerViewPreloader,mix Java&Kotlin

    
    

    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? = 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 {
        val photos = ArrayList()

        //读取手机图片
        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
    }
}



    

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?, private val ctx: Context) :
    RecyclerView.Adapter() {
    private var items: MutableList? = null

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

    fun getItems(): MutableList {
        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)
}




    

    

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 {
    public static int PHOTO_SIZE = 180;

    private MyAdapter mAdapter;

    private Context context;

    private int maxPreload = 500;

    private FixedPreloadSizeProvider preloadSizeProvider = new FixedPreloadSizeProvider(PHOTO_SIZE, PHOTO_SIZE);

    public void attach(RecyclerView rv) {
        RecyclerViewPreloader 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 getPreloadItems(int position) {
        List list = new ArrayList<>();
        list.add(mAdapter.getItems().get(position));
        return list;
    }

    @Nullable
    @Override
    public GlideRequest 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

你可能感兴趣的:(Android,kotlin,Java,android,kotlin,java)