android implement RecyclerView pinch to zoom by ScaleGestureDetector and GridLayoutManager ,java

Posted zhangphil

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android implement RecyclerView pinch to zoom by ScaleGestureDetector and GridLayoutManager ,java相关的知识,希望对你有一定的参考价值。

android implement RecyclerView pinch to zoom by ScaleGestureDetector and GridLayoutManager ,java(1)

a simple implements , use ScaleGestureDetector and GridLayoutManager.

 

MainActivy:

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

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

public class MainActivity extends AppCompatActivity 
    private RecyclerView mRecyclerView;

    private PhotoAdapter mPhotoAdapter;

    private GridLayoutManager mLayoutManager;

    private ScaleGestureDetector mScaleGestureDetector;

    private String TAG = "ZHANGPHIL";

    private final int[] ZOOM = 1, 2, 3, 4, 6, 8, 10, 15, 20;

    @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) 
                return super.onScaleBegin(detector);
            

            @Override
            public boolean onScale(ScaleGestureDetector detector) 
                return super.onScale(detector);
            

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

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

                scale(detector);
            

            private boolean scale(ScaleGestureDetector detector) 
                if (detector.getCurrentSpan() > 20 && detector.getTimeDelta() > 50) 
                    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;
            
        );

        mPhotoAdapter = new PhotoAdapter(this, 1000);
        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;
    

 

MainActivity's layout:

<?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>

 

RecyclerView's adapter,PhotosAdapter:

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;

public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder> 
    private String TAG = "ZHANGPHIL";

    private int itemCount;

    public PhotoAdapter(FragmentActivity activity, int itemCount) 
        this.itemCount = itemCount;
    

    @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.setImage(ImageSource.resource(R.mipmap.android));
        holder.imageView.setZoomEnabled(false);
    

    @Override
    public int getItemCount() 
        return itemCount;
    

    public class PhotoViewHolder extends RecyclerView.ViewHolder 
        public SubsamplingScaleImageView imageView;
        public AppCompatTextView textView;

        public PhotoViewHolder(View itemView) 
            super(itemView);
            imageView = itemView.findViewById(R.id.image);
            textView = itemView.findViewById(R.id.text);
        
    

    public void dataSetChanged() 
        notifyDataSetChanged();
    


 

 

PhotosAdapter's layout:

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

    <com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/image"
        android:autoSizeMinTextSize="5dp"
        android:autoSizeTextType="uniform"
        android:gravity="center_horizontal"
        android:maxLines="1"
        android:text="-.-"
        android:textColor="@android:color/black" />
</RelativeLayout>

 

android.png

 

 

引用:

    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.davemorrissey.labs:subsampling-scale-image-view-androidx:3.10.0'

 

 

 

pinch to zoom 2 columns:

 

 

 

pinch to zoom 3 columns:

 

 

 

pinch to zoom 8 columns:

 

 

 

pinch to zoom 15 columns:

 

 

 

 

 

Android SubsamplingScaleImageView(subsampling-scale-image-view)单双击长按事件【系列2】_zhangphil的博客-CSDN博客Android SubsamplingScaleImageView(subsampling-scale-image-view)单双击长按事件【系列2】附录参考文章1介绍了Android SubsamplingScaleImageView(subsampling-scale-image-view)的简单使用方法。在处理大图的浏览查看动作过程中,往往还有其他额外的事情需要处理,典型的以微信https://blog.csdn.net/zhangphil/article/details/49586907

 

Android加载/处理超大图片神器!SubsamplingScaleImageView(subsampling-scale-image-view)【系列1】_zhangphil的博客-CSDN博客Android加载/处理超大图片神器!SubsamplingScaleImageView(subsampling-scale-image-view)【系列1】Android在加载或者处理超大巨型图片(x MB量级起步的图片)是一件非常需要小心谨慎的活儿,除了常规的OOM,其他需要处理的细节地方还很多,幸好开源社区github贡献了一套神器:SubsamplingScaleImahttps://blog.csdn.net/zhangphil/article/details/49557549

 

Android RecyclerView的StaggeredGridLayoutManager实现交错排列的子元素分组_zhangphil的博客-CSDN博客Android RecyclerView的StaggeredGridLayoutManager实现交错排列的子元素分组先看实现的结果如图:设计背景:现在的产品对设计的需求越来越多样化,如附录文章2是典型的联系人分组RecyclerView,子元素排列到一个相同的组,但是有些时候,UI要求把这些元素不是垂直方向的,而是像本文开头的图中所示样式排列,这就需要用StaggeredGridLayoutMahttps://blog.csdn.net/zhangphil/article/details/79759206

 

Android StaggeredGridLayoutManager布局RecyclerView在滚动状态可见范围刷新数据_zhangphil的博客-CSDN博客Android StaggeredGridLayoutManager布局RecyclerView在滚动状态可见范围刷新数据之所以把StaggeredGridLayoutManager布局的RecyclerView单列出来处理滚动状态下可见(可视)范围内的数据更新问题,是因为在StaggeredGridLayoutManager布局下的RecyclerView,对于第一个可视item和最后一个可视i...https://blog.csdn.net/zhangphil/article/details/79963955

 

Android RecyclerView的StaggeredGridLayoutManager和CardView_zhangphil的博客-CSDN博客《Android RecyclerView的StaggeredGridLayoutManager和CardView》StaggeredGridLayoutManager,顾名思义,”交错排列的Grid”,在RecyclerView中可以将元素分列编排下去。我在之前的文章中分别介绍了RecyclerView(参考文章:http://blog.csdn.net/zhangphil/arhttps://blog.csdn.net/zhangphil/article/details/47604581

 

Android RecyclerView(瀑布流)水平/垂直方向分割线_recyclerview水平_zhangphil的博客-CSDN博客Android RecyclerView(瀑布流)水平/垂直方向分割线Android RecyclerView不像过去的ListView那样随意的设置水平方向的分割线,如果要实现RecyclerView的水平/垂直分割线,则需要继承自RecyclerView.ItemDecoration重写getItemOffsets方法,从而增加水平/垂直分割线。写一个例子。MainActivity.javhttps://blog.csdn.net/zhangphil/article/details/52757862

 

HorizontalScrollView包裹RecyclerView,使用StaggeredGridLayoutManager均分网格形成表格状列表,不固定列,每次刷新数据列位置异常错乱变动问题_zhangphil的博客-CSDN博客HorizontalScrollView包裹RecyclerView,使用StaggeredGridLayoutManager均分网格形成表格状列表,不固定列,每次刷新数据列位置异常错乱变动问题问题描述:用HorizontalScrollView包裹RecyclerView,使用StaggeredGridLayoutManager均分网格形成表格状列表,由于列不固定,具体列数来自服务器,这样每次刷...https://blog.csdn.net/zhangphil/article/details/80293657

 

以上是关于android implement RecyclerView pinch to zoom by ScaleGestureDetector and GridLayoutManager ,java的主要内容,如果未能解决你的问题,请参考以下文章

Bitmap.recycle引发的血案

Android系统中Bitmap是不是有调用recycle方法的必要性

1.Android recycleView万能分隔线 GridLayoutManager布局item左右间距均等(最易懂)

Bitmap、Bitmap.recycle()、WeakReferences 和垃圾回收

Android compile、implementation和api的区别

找不到 Gradle DSL 方法:'implementation()' (Android React Native)