android RecyclerView pinch zoom,ScaleGestureDetector&GridLayoutManager,with BigImageViewer,java

Posted zhangphil

tags:

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

android RecyclerView pinch zoom,ScaleGestureDetector&GridLayoutManager,with BigImageViewer,java(3)

 

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 = null;
    private PhotoAdapter mPhotoAdapter = null;
    private GridLayoutManager mLayoutManager = null;
    private ScaleGestureDetector mScaleGestureDetector = null;

    private String TAG = "zhangphil";
    private int[] ZOOM = 2, 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;
            
        );

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

 

 

<?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"
        android:layoutAnimation="@anim/anim_layout" />

</RelativeLayout>

 

res/anim/anim_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/animation_popup_enter"
    android:animationOrder="normal"
    android:delay="0.1" />

 

res/anim/animation_popup_enter.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--
    <translate
        android:duration="500"
        android:fromXDelta="-100%"
        android:fromYDelta="0"
        android:toXDelta="0"
        android:toYDelta="0" />
        -->
    <alpha
        android:duration="500"
        android:fromAlpha="0"
        android:toAlpha="1" />
</set>

 

 

 

import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.AnyRes;
import androidx.annotation.NonNull;
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;

public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder> 
    private int itemCount;
    private Context context;

    public PhotoAdapter(Context context, int itemCount) 
        this.context = context;
        this.itemCount = itemCount;

        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(getUriFromResId(context, R.mipmap.android));
    

    @Override
    public int getItemCount() 
        return itemCount;
    

    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();
    

    private Uri getUriFromResId(@NonNull Context context, @AnyRes int drawableId) 
        return Uri.parse(
                ContentResolver.SCHEME_ANDROID_RESOURCE
                        + "://" + context.getResources().getResourcePackageName(drawableId)
                        + '/' + context.getResources().getResourceTypeName(drawableId)
                        + '/' + context.getResources().getResourceEntryName(drawableId)
        );
    

 

 

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

 

 

 

 

 

 

 

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

 

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

Android之RecyclerView介绍

Android:RecyclerView里面的RecyclerView,滚动到底部

Android教程2020 - RecyclerView获取滑动距离

android recyclerview怎么设置每次只能滑动一页

Android的import android.support.v7.widget.RecyclerView的RecyclerView标红问题

android之recyclerview的基本使用