Android GlideApp FixedPreloadSizeProvider RecyclerViewPreloader preload scroll smooth,Java

Android GlideApp FixedPreloadSizeProvider RecyclerViewPreloader preload scroll smooth,Java

 

 

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

 

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

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

</RelativeLayout>

 

 

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.ListPreloader;
import com.bumptech.glide.RequestBuilder;
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 MainActivity extends AppCompatActivity {
    private GlideRequest<Drawable> fullRequest = null;
    private GlideRequest<Drawable> preloadRequest = null;
    private MAdapter mAdapter;

    private int PHOTO_SIZE = 180;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fullRequest = GlideApp.with(this).asDrawable().centerCrop().override(PHOTO_SIZE);
        preloadRequest = fullRequest;

        rePreload();
    }

    private void rePreload() {
        int spanCount = 5;

        RecyclerView rv = findViewById(R.id.recycler_view);
        GridLayoutManager layoutManager = new GridLayoutManager(this, spanCount);
        rv.setLayoutManager(layoutManager);

        mAdapter = new MAdapter(this);
        rv.setAdapter(mAdapter);

        int loadSize = 500;
        FixedPreloadSizeProvider preloadSizeProvider = new FixedPreloadSizeProvider<MyData>(PHOTO_SIZE, PHOTO_SIZE);
        MyPreloadModelProvider preloadModelProvider = new MyPreloadModelProvider();

        RecyclerViewPreloader<MyData> preloader = new RecyclerViewPreloader(
                GlideApp.with(this),
                preloadModelProvider,
                preloadSizeProvider,
                loadSize
        );

        rv.addOnScrollListener(preloader);


        new Thread(new Runnable() {
            @Override
            public void run() {
                ArrayList<MyData> items = readAllImage(getApplicationContext());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.onChange(items);
                    }
                });
            }
        }).start();
    }


    private class MyPreloadModelProvider implements ListPreloader.PreloadModelProvider<MyData> {

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

        @Nullable
        @Override
        public RequestBuilder<?> getPreloadRequestBuilder(@NonNull MyData item) {
            return preloadRequest.load(new File(item.path));
        }
    }

    private class MAdapter extends RecyclerView.Adapter<MyVH> {
        private Context context;
        private ArrayList<MyData> items = new ArrayList<>();

        public MAdapter(Context context) {
            this.context = context;
        }

        public void onChange(ArrayList<MyData> items) {
            this.items = items;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public MyVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
            ViewGroup.LayoutParams params = view.getLayoutParams();
            params.width = PHOTO_SIZE;
            params.height = PHOTO_SIZE;
            return new MyVH(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyVH holder, int position) {
            fullRequest.load(new File(items.get(position).path)).centerCrop().into(holder.image);
            holder.text.setText(position + "");
        }

        @Override
        public int getItemCount() {
            return items.size();
        }
    }

    private class MyVH extends RecyclerView.ViewHolder {
        public AppCompatImageView image;
        public TextView text;

        public MyVH(@NonNull View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.image);
            text = itemView.findViewById(R.id.text);
        }
    }

    private ArrayList<MyData> readAllImage(Context context) {
        ArrayList<MyData> photos = new ArrayList<>();

        //读取手机图片
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                null,
                null,
                null,
                null
        );

        int index = 0;
        while (cursor.moveToNext()) {
            //图片路径 uri
            String 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(new MyData(path, index++));
        }

        cursor.close();

        return photos;
    }

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

        public MyData(String p, int i) {
            path = p;
            index = i;
        }
    }
}

 

 

<?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:layout_height="wrap_content"
        android:scaleType="centerCrop"
        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>

 

 

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.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.AppGlideModule;

@GlideModule
public class MyModule extends AppGlideModule {
    private LruResourceCache mCache = new LruResourceCache(1024 * 1024 * 800);

    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        builder.setMemoryCache(mCache);
        builder.setLogLevel(Log.DEBUG);
    }

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

 

 

Android GlideApp GlideRequest FixedPreloadSizeProvider RecyclerViewPreloader, kotlin_zhangphil's blog-CSDN blog [Code] Android Paging 3, kotlin (1) In actual development, although Glide solves the problem of fast loading pictures, there is still one unresolved problem: such as the user's avatar , often the user's avatar is an ordinary rectangular picture read from the server, but the current design generally requires the user's avatar to be displayed as a circular avatar on the APP side, so although Glide can load it at this time, the loaded one is a rectangle. If you want Glide_android frosted glass rounded corners. "Android Image Loading and Caching Open Source Framework: Android Glide" Android Glide is an open source third-party framework for image loading and caching processing. https://blog.csdn.net/zhangphil/article/details/131813200

 

Guess you like

Origin blog.csdn.net/zhangphil/article/details/131884306