1. 程式人生 > >畫廊選中放大圖片效果(Gallery實現)

畫廊選中放大圖片效果(Gallery實現)

自己的專案需要做一個圖片瀏覽效果,滑動到當前圖片,則放大,其餘都縮小。雖然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
        }

    }
}

雖然後面不採用該程式碼,畢竟是自己寫的,存個備份。