Android load all photos into RecyclerView,support pinch to zoom by ScaleGestureDetector,Java(3)

Android load all photos place into RecyclerView,support  pinch to zoom by ScaleGestureDetector,Java(3)

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

    implementation 'com.github.piasy:BigImageViewer:1.8.1'
    implementation 'com.github.piasy:GlideImageLoader:1.8.1'

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

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView = null;
    private PhotoAdapter mPhotoAdapter = null;
    private GridLayoutManager mLayoutManager = null;
    private ScaleGestureDetector mScaleGestureDetector = null;

    private String TAG = "fly";
    private int[] ZOOM = {3, 5, 9};


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

        mRecyclerView = findViewById(R.id.recycler_view);
        mLayoutManager = new GridLayoutManager(this, ZOOM[2]);
        mLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mScaleGestureDetector = new ScaleGestureDetector(this, new ScaleGestureDetector.SimpleOnScaleGestureListener() {
            @Override
            public boolean onScaleBegin(@NonNull ScaleGestureDetector detector) {
                Log.d(TAG, "--onScaleBegin--");
                return super.onScaleBegin(detector);
            }

            @Override
            public boolean onScale(@NonNull ScaleGestureDetector detector) {
                Log.d(TAG, "--onScale--");
                return super.onScale(detector);
            }

            @Override
            public void onScaleEnd(@NonNull ScaleGestureDetector detector) {
                super.onScaleEnd(detector);

                Log.d(TAG, "--onScaleEnd--");
                Log.d(TAG, "TimeDelta-" + detector.getTimeDelta());
                Log.d(TAG, "ScaleFactor-" + detector.getScaleFactor());
                Log.d(TAG, "currentSpan-" + detector.getCurrentSpan());

                scale(detector);
            }

            private boolean scale(ScaleGestureDetector detector) {
                if (detector.getCurrentSpan() > 50 && detector.getTimeDelta() > 10) {
                    float scaleFactor = detector.getScaleFactor();
                    int curSpanCount = mLayoutManager.getSpanCount();
                    Log.d(TAG, "LayoutManager SpanCount()-$curSpanCount");

                    int curIndex = getSpanCountIndex(curSpanCount);
                    if (scaleFactor < 1) {
                        if (curSpanCount == ZOOM[ZOOM.length - 1]) {
                            return true;
                        } else {
                            mLayoutManager.setSpanCount(ZOOM[curIndex + 1]);
                        }
                    } else {
                        if (curSpanCount == ZOOM[0]) {
                            return true;
                        } else {
                            mLayoutManager.setSpanCount(ZOOM[curIndex - 1]);
                        }
                    }

                    mRecyclerView.setLayoutManager(mLayoutManager);
                    mPhotoAdapter.dataSetChanged();

                    return true;
                }

                return false;
            }
        });

        mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mScaleGestureDetector.onTouchEvent(event);
                return false;
            }
        });

        List<Photo> photos = readAllImage(this);
        mPhotoAdapter = new PhotoAdapter(getApplicationContext(), photos);
        mRecyclerView.setAdapter(mPhotoAdapter);
        mPhotoAdapter.dataSetChanged();
    }

    private int getSpanCountIndex(int spanCount) {
        int index = 0;
        for (int i = 0; i < ZOOM.length; i++) {
            if (spanCount == ZOOM[i]) {
                index = i;
            }
        }
        return index;
    }

    private static List<Photo> readAllImage(Context context) {
        List<Photo> photos = new ArrayList<>();

        //读取手机图片
        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {
            //图片的路径
            String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));

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

            //图片最后修改日期
            File file = new File(path);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String date = sdf.format(new Date(file.lastModified()));

            //图片大小
            long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE));
            Photo photo = new Photo(name, date, size, path, getImageUri(context, path));

            photos.add(photo);
        }

        cursor.close();

        return photos;
    }

    private static Uri getImageUri(Context context, String filePath) {
        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=? ",
                new String[]{filePath}, null);
        if (cursor != null && cursor.moveToFirst()) {
            int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID));
            Uri baseUri = Uri.parse("content://media/external/images/media");
            return Uri.withAppendedPath(baseUri, "" + id);
        } else {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DATA, filePath);
            return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        }
    }
}

<?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 android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.github.piasy.biv.BigImageViewer;
import com.github.piasy.biv.loader.glide.GlideImageLoader;
import com.github.piasy.biv.view.BigImageView;

import java.util.List;

public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder> {
    private List<Photo> photos;

    public PhotoAdapter(Context context, List<Photo> photos) {
        this.photos = photos;

        BigImageViewer.initialize(GlideImageLoader.with(context));
    }

    @Override
    public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.photo, parent, false);
        PhotoViewHolder holder = new PhotoViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(PhotoViewHolder holder, int position) {
        holder.textView.setText(String.valueOf(position));
        holder.imageView.showImage(photos.get(position).uri);
    }

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

    public class PhotoViewHolder extends RecyclerView.ViewHolder {
        public BigImageView imageView;

        public TextView textView;

        public PhotoViewHolder(View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.image);
            textView = itemView.findViewById(R.id.text);
        }
    }

    public void dataSetChanged() {
        notifyDataSetChanged();
    }
}

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

    <com.github.piasy.biv.view.BigImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        app:failureImage="@android:drawable/ic_dialog_alert"
        app:failureImageInitScaleType="center"
        app:optimizeDisplay="true" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/image"
        android:layout_centerHorizontal="true"
        android:maxLines="1"
        android:text="-.-"
        android:textColor="@android:color/black" />
</RelativeLayout>

import android.net.Uri;

public class Photo {
    public String name;//名称
    public String date;//日期
    public long size;  //大小
    public String path;//路径

    public Uri uri;

    public Photo(String name, String date, long size, String path, Uri uri) {
        this.name = name;
        this.date = date;
        this.size = size;
        this.path = path;
        this.uri=uri;
    }
}

android RecyclerView pinch zoom, ScaleGestureDetector&GridLayoutManager, with BigImageViewer, kotlin(2)_zhangphil's blog-CSDN blog Android RecyclerView's StaggeredGridLayoutManager implements staggered sub-element grouping First look at the results of the implementation as shown in the figure: Design background: The current product demand for design is getting higher and higher More and more diverse, such as appendix article 2 is a typical contact group RecyclerView, the sub-elements are arranged into the same group, but sometimes, the UI requires that these elements are not vertical, but as shown in the figure at the beginning of this article Style arrangement, which requires StaggeredGridLayoutMa. In the process of browsing and viewing large images, there are often other additional things that need to be dealt with, typically WeChat. https://blog.csdn.net/zhangphil/article/details/129426905

android implement RecyclerView pinch to zoom by ScaleGestureDetector and GridLayoutManager, java (1)_zhangphil's blog-CSDN blog Android RecyclerView's StaggeredGridLayoutManager implements staggered sub-element grouping First look at the results of the implementation as shown in the figure: Design background: current product requirements for design More and more diverse, such as appendix article 2 is a typical contact group RecyclerView, the sub-elements are arranged into the same group, but sometimes, the UI requires these elements not to be vertical, but as shown in the figure at the beginning of this article Display style arrangement, which requires StaggeredGridLayoutMa. In the process of browsing and viewing large images, there are often other additional things that need to be dealt with, typically WeChat. https://blog.csdn.net/zhangphil/article/details/129307599

Guess you like

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