Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android學習之--網格視圖(GridView)和圖像切換器(ImageSwitcher)

android學習之--網格視圖(GridView)和圖像切換器(ImageSwitcher)

日期:2017/2/23 16:45:10      編輯:關於Android編程


GridView用於在界面上按行、列分布顯示多個組件。GridView和ListView有共同父類:AbsListView。GridView與ListView的區別在於:ListView只在一個方向上分布,GridView在兩個方向上分布。所以使用GridView時一般都指定numColumns大於1,否則該屬性默認值為1,就意味著改GridView只有一列,那就變成了ListView


GridView的xml屬性

\


<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+YW5kcm9pZDpzdHJ0Y2hNb2RlILXEJiMyMDU0MDvOqqO6PC9wPgo8cD4gICA8aW1nIHNyYz0="/uploadfile/Collfiles/20140812/2014081208380875.png" alt="\">



ImageSwitcher由FrameLayout派生而出,ImageSwitcher組件與ImageView很相似,但是ImageSwitcher多一個功能:它所顯示的圖片切換時可以設置動畫效果。

使用ImageSwitcher時往往需要為它設置一個ImageSwitcher.ViewFactory的makeView()方法來負責顯示ImageView


下面結合GridView和ImageSwitcher來實現一個例子


(1)activity_main.xml 布局GridView和ImageSwitcher


    
    
    


(2)img.xml 布局imageView 用於顯示GridView的圖片



    
    

(3)MainActivity.java

package com.example.viewimage;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.SimpleAdapter;
import android.widget.ViewSwitcher.ViewFactory;

public class MainActivity extends Activity {
	
	//定義組件
	private GridView gredView = null;
	private ImageSwitcher imageSwt =null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//獲取布局組件
		gredView = (GridView) findViewById(R.id.grid);
		imageSwt = (ImageSwitcher) findViewById(R.id.imageSwitch);
		//圖片
		final int images[] = new int[]{
				R.drawable.name01,R.drawable.name02,R.drawable.name03,R.drawable.name04,
				R.drawable.name05,R.drawable.name06,R.drawable.name07,R.drawable.name08,
				R.drawable.name09,R.drawable.name10,R.drawable.name11,R.drawable.name12,
				R.drawable.name13,R.drawable.name14,R.drawable.name15,R.drawable.name16
		};
		//創建list集合,存儲圖片
		List> list = new ArrayList>();
		for(int i=0;i map = new HashMap();
			map.put("img", images[i]);
			list.add(map);
		}
		//設置圖片更換效果
		//淡入效果
		imageSwt.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
		//淡出效果
		imageSwt.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
		//設置圖片轉換效果
		imageSwt.setFactory(new ViewFactory() {
			
			@Override
			public View makeView() {
				ImageView imageView = new ImageView(MainActivity.this);
				imageView.setLayoutParams(new ImageSwitcher.LayoutParams(300, 300));
				imageView.setScaleType(ScaleType.FIT_CENTER);
				return imageView;
			}
		});
		//設置適配器
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.img, new String[]{"img"}, new int[]{R.id.img});
		//適配器
		gredView.setAdapter(simpleAdapter);
		//選中事件監聽
		gredView.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView parent, View view,
					int position, long id) {
				Log.i("onItemSelected", "onItemSelected");
				imageSwt.setImageResource(images[position]);
			}

			@Override
			public void onNothingSelected(AdapterView parent) {
				// TODO Auto-generated method stub
				
			}
		});
		//點擊事件監聽
		gredView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView parent, View view,
					int position, long id) {
				Log.i("onItemClick", "onItemClick");
				imageSwt.setImageResource(images[position]);
				
			}
		});
	}


}

運行效果如下:


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved