画廊选中放大图片效果(Gallery实现)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010844304/article/details/83348113

自己的项目需要做一个图片浏览效果,滑动到当前图片,则放大,其余都缩小。虽然Gallery已经过时了,但是以前一直没有使用过,所以特地尝试一下。后续还打算使用ViewPager,还有RecyclerView实现一把。
先上个效果图
在这里插入图片描述

布局代码

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestActivity">

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animationDuration="2000"
        android:spacing="10dp">
    </Gallery>

</RelativeLayout>

功能代码

package com.wujie.scanall

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.BaseAdapter
import android.widget.Gallery
import android.widget.ImageView



class TestActivity : AppCompatActivity() , View.OnClickListener{
    lateinit var gallery: Gallery
    var imageIds = arrayOf(R.mipmap.pc1, R.mipmap.pc2, R.mipmap.pc3, R.mipmap.pc4, R.mipmap.pc5)
    lateinit var mAdapter : ImageAdapter
    var lastPostion = -1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
        initView()
    }
    private fun initView() {
        gallery = findViewById(R.id.gallery)
        mAdapter = ImageAdapter(this, imageIds)
        gallery.adapter = mAdapter
         var lastView: View? = null

        gallery.setCallbackDuringFling(false)//停止时返回位置


        gallery.onItemSelectedListener = object: AdapterView.OnItemSelectedListener{
            override fun onNothingSelected(parent: AdapterView<*>?) {
                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                if (lastPostion != position) {
                    view?.let {
                        val animator1 = ObjectAnimator.ofFloat(it, "scaleX", 0.8f, 1f)
                        val animator2 = ObjectAnimator.ofFloat(it, "scaleY", 0.8f, 1f)
                        val animatorSet = AnimatorSet()
                        animatorSet.play(animator1).with(animator2)
                        animatorSet.start()
                    }
                    lastView?.let {
                        val animator1 = ObjectAnimator.ofFloat(it, "scaleX", 1f, 0.8f)
                        val animator2 = ObjectAnimator.ofFloat(it, "scaleY", 1f, 0.8f)
                        val animatorSet = AnimatorSet()
                        animatorSet.play(animator1).with(animator2)
                        animatorSet.duration = 200
                        animatorSet.start()
                    }
                    lastView = view
                    lastPostion = position
                }

            }

        }
    }


    override fun onClick(v: View?) {

    }

    inner class ImageAdapter(val mContext: Context, val images: Array<Int>): BaseAdapter(){

        override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
            var imageView : ImageView
            if (convertView == null) {
                imageView = ImageView(mContext)
                imageView.layoutParams = Gallery.LayoutParams(600,1022)
                imageView.scaleType = ImageView.ScaleType.FIT_XY
            } else {
                imageView = convertView as ImageView
            }
            imageView.setImageResource(imageIds[position])

                imageView.scaleX = 0.8f
                imageView.scaleY = 0.8f

            return imageView

        }

        override fun getItem(position: Int): Any {
            return imageIds[position]
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getCount(): Int {
            return imageIds.size
        }

    }
}

虽然后面不采用该代码,毕竟是自己写的,存个备份。

猜你喜欢

转载自blog.csdn.net/u010844304/article/details/83348113