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

308 篇文章 0 订阅

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值