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的博客-CSDN博客Android RecyclerView的StaggeredGridLayoutManager实现交错排列的子元素分组先看实现的结果如图:设计背景:现在的产品对设计的需求越来越多样化,如附录文章2是典型的联系人分组RecyclerView,子元素排列到一个相同的组,但是有些时候,UI要求把这些元素不是垂直方向的,而是像本文开头的图中所示样式排列,这就需要用StaggeredGridLayoutMa。在处理大图的浏览查看动作过程中,往往还有其他额外的事情需要处理,典型的以微信。https://blog.csdn.net/zhangphil/article/details/129426905

android implement RecyclerView pinch to zoom by ScaleGestureDetector and GridLayoutManager ,java(1)_zhangphil的博客-CSDN博客Android RecyclerView的StaggeredGridLayoutManager实现交错排列的子元素分组先看实现的结果如图:设计背景:现在的产品对设计的需求越来越多样化,如附录文章2是典型的联系人分组RecyclerView,子元素排列到一个相同的组,但是有些时候,UI要求把这些元素不是垂直方向的,而是像本文开头的图中所示样式排列,这就需要用StaggeredGridLayoutMa。在处理大图的浏览查看动作过程中,往往还有其他额外的事情需要处理,典型的以微信。https://blog.csdn.net/zhangphil/article/details/129307599

猜你喜欢

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