ImageSwitcher picture switch

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">
    <!-- 定义一个GridView组件 -->
    <GridView
        android:id="@+id/grid01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:horizontalSpacing="2dp"
        android:verticalSpacing="2dp"
        android:numColumns="4"
        android:gravity="center"/>
    <!-- 定义一个ImageSwitcher组件 -->
    <ImageSwitcher android:id="@+id/switcher"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_gravity="center_horizontal"
        android:inAnimation="@android:anim/fade_in"
        android:outAnimation="@android:anim/fade_out"/>
</LinearLayout>
Main interface
package com.example.viewswitcher

import android.app.Activity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup.LayoutParams
import android.widget.*
import android.widget.ViewSwitcher.ViewFactory
import android.widget.AdapterView.OnItemClickListener
import android.widget.AdapterView.OnItemSelectedListener


class MainActivity : Activity()
{
    internal var imageIds = intArrayOf(R.drawable.bomb5, R.drawable.bomb6,
        R.drawable.bomb7, R.drawable.bomb8, R.drawable.bomb9,
        R.drawable.bomb10, R.drawable.bomb11, R.drawable.bomb12,
        R.drawable.bomb13, R.drawable.bomb14, R.drawable.bomb15, 
        R.drawable.bomb16) 
    the override Fun the onCreate (savedInstanceState: Bundle ? ) 
    { 
        Super .onCreate (savedInstanceState) 
        setContentView (R.layout.activity_main) 
        // creating a List object, List object elements is the Map 
        Val = the listItems the ArrayList <the Map <String, the Any >> ()
         for (I in imageIds.indices) 
        { 
            Val listItem = the HashMap <String, the Any> () 
            listItem [ "Image" ] =  imageIds[i]
            listItems.add (listItem) 
        } 
        // Get display picture ImageSwitcher 
        Val Switcher the findViewById = <ImageSwitcher> (R.id.switcher)
         // set ImageSwitcher picture switch animation
         // Create ViewFactory using a Lambda expression, expression is makeView method () of method body 
        switcher.setFactory {
             // create objects ImageView 
            Val ImageView the imageView = ( the this @MainActivity) 
            imageView.scaleType = ImageView.ScaleType.FIT_CENTER 
            imageView.layoutParams = FrameLayout.LayoutParams (LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT) 
            // returns the object ImageView
            imageView 
        } 
        // create a SimpleAdapter 
        Val simpleAdapter = SimpleAdapter ( the this , the listItems, R.layout.cell, 
            ArrayOf ( "Image"), intArrayOf (R.id.image1)) // use as an interface file /layout/cell.xml layout 
        Val Grid the findViewById = <the GridView> (R.id.grid01)
         // set Adapter to the GridView 
        grid.adapter = simpleAdapter
         // add list item is selected listener 
        grid.onItemSelectedListener = Object: OnItemSelectedListener 
        { 
            the override Fun onItemSelected (parent: AdapterView <*>, View: View, position: Int, ID: Long) 
            { 
                // display the currently selected image 
                switcher.setImageResource (imageIds [position]) 
            } 

            the override Fun onNothingSelected (parent: The AdapterView <*> ) 
            { 
            } 
        } 
        // Add a listing item clicked listener 
        grid.setOnItemClickListener {parent, View, position, ID -> // display the clicked image             switcher.setImageResource (imageIds [position]) 
        } 
    } 
}
            
The main program

 

Guess you like

Origin www.cnblogs.com/superxuezhazha/p/11519508.html