本文實(shí)例講述了Android開發(fā)之ImageSwitcher相冊(cè)功能。分享給大家供大家參考,具體如下:
簡(jiǎn)介:
1.ImageSwitcher是viewSwitcher的子類,所以ImageSwitcher繼承了ViewSwitcher素有的特性
2.作為ViewSwitcher的子類,它比ViewSwitcher使用更加方便,主要體現(xiàn)在:①. 重寫了setNext() ②. 重寫了showPrevious()方法。所以其實(shí)用起來,要比ViewSwitcher更為方便。
3.中重要的是:ImageSwitcher增加了圖片切換動(dòng)畫,使得圖片的切換更加自然
這里看下運(yùn)行效果:

這個(gè)例子中,看考了瘋狂Android講義,采用ImageSwicher結(jié)合Gridview實(shí)現(xiàn)的,一下問布局文件:
<?xml version="1.0" encoding="utf-8" ?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal"> <!--定義一個(gè)GridView組件--> <GridView android:id="@+id/grid01" android:layout_width="match_parent" android:layout_height="match_parent" android:listSelector="@null" android:numColumns="3" android:horizontalSpacing="2dp" android:verticalSpacing="2dp" android:gravity="center"> </GridView> <!--定義一個(gè)ImageSwitcher組件--> <ImageSwitcher android:id="@+id/switcher" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:padding="50dp" android:inAnimation="@android:anim/fade_in" android:outAnimation="@android:anim/fade_out" android:background="@color/colorPrimaryDark" android:visibility="gone"> </ImageSwitcher></RelativeLayout>
關(guān)于GridView 有兩種常用的監(jiān)聽事件:
gridView.setOnItemSelectedListener 和 gridView.setOnItemClickListener
關(guān)于ImageSwitcher 設(shè)置ImageSwitcher 采用了imageSwitcher.setFactory 方法:
public class MainActivity extends Activity { int[] imageId = new int[]{ R.drawable.a0,R.drawable.a1,R.drawable.a2,R.drawable.a4, R.drawable.a5,R.drawable.a6,R.drawable.a7,R.drawable.a8, R.drawable.a9,R.drawable.a00,R.drawable.a02,R.drawable.a02, }; ImageSwitcher imageSwitcher ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //創(chuàng)建一個(gè)List對(duì)象,list對(duì)象的元素是Map List<Map<String,Object>> listitems = new ArrayList<Map<String, Object>>(); for (int i = 0 ; i < imageId.length ; i++ ){ Map<String,Object> listitem = new HashMap<String, Object>(); listitem.put("image",imageId[i]); listitems.add(listitem); } //獲取顯示圖片的ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher); //為ImageSwitcher設(shè)置動(dòng)畫效果 imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() { @Override public View makeView() { //創(chuàng)建ImageView對(duì)象 ImageView imageView = new ImageView(MainActivity.this); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setLayoutParams(new ImageSwitcher.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)); //返回ImageView對(duì)象 return imageView; } }); //創(chuàng)建一個(gè)SimpleAdapter SimpleAdapter simpleAdapter = new SimpleAdapter(this,listitems,R.layout.cell,new String[]{"image"},new int[]{R.id.image1}); GridView gridView = (GridView) findViewById(R.id.grid01); //為gridView設(shè)置adapter gridView.setAdapter(simpleAdapter); //添加列表選中監(jiān)聽事件 gridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { imageSwitcher.setVisibility(View.VISIBLE); imageSwitcher.setClickable(true); //顯示當(dāng)前選中圖片 imageSwitcher.setImageResource(imageId[position]); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); //添加列表被單擊的監(jiān)聽器 gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { imageSwitcher.setVisibility(View.VISIBLE); imageSwitcher.setClickable(true); //顯示被單擊圖片 imageSwitcher.setImageResource(imageId[position]); } }); //為imageSwitcher添加監(jiān)聽事件 imageSwitcher.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { imageSwitcher.setVisibility(View.GONE); imageSwitcher.setClickable(false); } }); imageSwitcher.setClickable(false); }}幾點(diǎn)值得注意的:
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選