Android GlideApp FixedPreloadSizeProvider RecyclerViewPreloader preload scroll smooth,Java

Android GlideApp FixedPreloadSizeProvider RecyclerViewPreloader preload scroll smooth,Java

 

 

    
    

 




    

 

 

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 fullRequest = null;
    private GlideRequest 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(PHOTO_SIZE, PHOTO_SIZE);
        MyPreloadModelProvider preloadModelProvider = new MyPreloadModelProvider();

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

        rv.addOnScrollListener(preloader);


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


    private class MyPreloadModelProvider implements ListPreloader.PreloadModelProvider {

        @NonNull
        @Override
        public List getPreloadItems(int position) {
            List 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 {
        private Context context;
        private ArrayList items = new ArrayList<>();

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

        public void onChange(ArrayList 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 readAllImage(Context context) {
        ArrayList 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;
        }
    }
}

 

 




    

    

 

 

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的博客-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,Java,Android,java)