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

Posted zhangphil

tags:

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

android RecyclerView pinch zoom,ScaleGestureDetector&GridLayoutManager,with BigImageViewer,kotlin(2)

 

package com.example.myapplication

import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ScaleGestureDetector
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.AnyRes
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
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


class MainActivity : AppCompatActivity() 
    private var mRecyclerView: RecyclerView? = null
    private var mPhotoAdapter: PhotoAdapter? = null
    private var mLayoutManager: GridLayoutManager? = null
    private var mScaleGestureDetector: ScaleGestureDetector? = null

    private val TAG = "zhangphil"

    private val ZOOM = intArrayOf(2, 3, 5, 9)

    override fun onCreate(savedInstanceState: Bundle?) 
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mRecyclerView = findViewById(R.id.recycler_view)
        mLayoutManager = GridLayoutManager(this, ZOOM[2])
        mLayoutManager!!.orientation = GridLayoutManager.VERTICAL
        mRecyclerView?.setLayoutManager(mLayoutManager)

        mScaleGestureDetector = ScaleGestureDetector(this, object : SimpleOnScaleGestureListener() 
            override fun onScaleBegin(detector: ScaleGestureDetector): Boolean 
                Log.d(TAG, "--onScaleBegin--")
                return super.onScaleBegin(detector)
            

            override fun onScale(detector: ScaleGestureDetector): Boolean 
                Log.d(TAG, "--onScale--")
                return super.onScale(detector)
            

            override fun onScaleEnd(detector: ScaleGestureDetector) 
                super.onScaleEnd(detector)
                Log.d(TAG, "--onScaleEnd--")
                Log.d(TAG, "TimeDelta-" + detector.timeDelta)
                Log.d(TAG, "ScaleFactor-" + detector.scaleFactor)
                Log.d(TAG, "currentSpan-" + detector.currentSpan)
                scale(detector)
            

            private fun scale(detector: ScaleGestureDetector): Boolean 
                if (detector.currentSpan > 50 && detector.timeDelta > 10) 
                    val scaleFactor = detector.scaleFactor
                    val curSpanCount = mLayoutManager!!.spanCount
                    Log.d(TAG, "LayoutManager SpanCount()-$curSpanCount")
                    val curIndex = getSpanCountIndex(curSpanCount)
                    if (scaleFactor < 1) 
                        if (curSpanCount == ZOOM[ZOOM.size - 1]) 
                            return true
                         else 
                            mLayoutManager!!.spanCount = ZOOM[curIndex + 1]
                        
                     else 
                        if (curSpanCount == ZOOM[0]) 
                            return true
                         else 
                            mLayoutManager!!.spanCount = ZOOM[curIndex - 1]
                        
                    
                    mRecyclerView?.setLayoutManager(mLayoutManager)
                    mPhotoAdapter?.dataSetChanged()
                    return true
                
                return false
            
        )

        mRecyclerView?.setOnTouchListener( v, event ->
            mScaleGestureDetector!!.onTouchEvent(event)
            false
        )

        mPhotoAdapter = PhotoAdapter(this, 1000)
        mRecyclerView?.setAdapter(mPhotoAdapter)
        mPhotoAdapter?.dataSetChanged()
    

    private fun getSpanCountIndex(spanCount: Int): Int 
        var index = 0
        for (i in ZOOM.indices) 
            if (spanCount == ZOOM[i]) 
                index = i
            
        
        return index
    



class PhotoAdapter(val context: Context?, private val itemCount: Int) :
    RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder>() 

    init 
        BigImageViewer.initialize(GlideImageLoader.with(context))
    

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): PhotoViewHolder 
        val view: View =
            LayoutInflater.from(parent.context).inflate(R.layout.photo, parent, false)
        return PhotoViewHolder(view)
    

    override fun onBindViewHolder(holder: PhotoViewHolder, position: Int) 
        holder.textView.text = position.toString()
        holder.imageView.showImage(getUriFromResId(context, R.mipmap.android))
    

    override fun getItemCount(): Int 
        return itemCount
    

    class PhotoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) 
        var imageView: BigImageView
        var textView: TextView

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

    fun dataSetChanged() 
        notifyDataSetChanged()
    


fun getUriFromResId(
    context: Context?,
    @AnyRes drawableId: Int
): Uri 
    return Uri.parse(
        ContentResolver.SCHEME_ANDROID_RESOURCE
                + "://" + context?.getResources()?.getResourcePackageName(drawableId)
                + '/' + context?.getResources()?.getResourceTypeName(drawableId)
                + '/' + context?.getResources()?.getResourceEntryName(drawableId)
    )

 

 

activity_main.xml:

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

 

 

photo.xml:

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

 

 

 

dependencies

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

// load with glide
    implementation 'com.github.piasy:GlideImageLoader:1.8.1'

// support thumbnail and gif with Glide
    implementation 'com.github.piasy:GlideImageViewFactory:1.8.1'

 

 

 

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,kotli的主要内容,如果未能解决你的问题,请参考以下文章

Android之RecyclerView介绍

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

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

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

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

android之recyclerview的基本使用