Android實(shí)現(xiàn)圖像切換器
本文實(shí)例為大家分享了Android實(shí)現(xiàn)圖像切換器的具體代碼,供大家參考,具體內(nèi)容如下
java代碼:
private int[] imageId = new int[] { R.drawable.img01, R.drawable.img02, R.drawable.img03, R.drawable.img04, R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08, R.drawable.img09 }; // 聲明并初始化一個(gè)保存要顯示圖像ID的數(shù)組private int index = 0; // 當(dāng)前顯示圖像的索引private ImageSwitcher imageSwitcher; // 聲明一個(gè)圖像切換器對(duì)象 imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1); // 獲取圖像切換器 // 設(shè)置動(dòng)畫(huà)效果 imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); // 設(shè)置淡入動(dòng)畫(huà) imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); // 設(shè)置淡出動(dòng)畫(huà) imageSwitcher.setFactory(new ViewFactory() { @Override public View makeView() { ImageView imageView = new ImageView(MainActivity.this); // 實(shí)例化一個(gè)ImageView類的對(duì)象 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); // 設(shè)置保持縱橫比居中縮放圖像 imageView.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); return imageView; // 返回imageView對(duì)象 } }); imageSwitcher.setImageResource(imageId[index]); // 顯示默認(rèn)的圖片 Button up = (Button) findViewById(R.id.btn1); // 獲取“上一張”按鈕 Button down = (Button) findViewById(R.id.btn2); // 獲取“下一張”按鈕 up.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (index > 0) { index--; } else { index = imageId.length - 1; } imageSwitcher.setImageResource(imageId[index]); // 顯示當(dāng)前圖片 } }); down.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (index < imageId.length - 1) { index++; } else { index = 0; } imageSwitcher.setImageResource(imageId[index]); // 顯示當(dāng)前圖片 } });
xml代碼:
<?xml version='1.0' encoding='utf-8'?><LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' android:orientation='horizontal' android:layout_width='fill_parent' android:layout_height='fill_parent' android: android:gravity='center' > <Button android:text='上一張' android: android:layout_width='wrap_content' android:layout_height='wrap_content'/> <ImageSwitcher android: android:layout_gravity='center' android:layout_width='wrap_content' android:layout_height='wrap_content'/> <Button android:text='下一張' android: android:layout_width='wrap_content' android:layout_height='wrap_content'/></LinearLayout>
說(shuō)明:
drawable中,加入下列圖片img01~img09
效果圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. 解析原生JS getComputedStyle3. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))4. xpath簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理5. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)6. css代碼優(yōu)化的12個(gè)技巧7. jsp EL表達(dá)式詳解8. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法9. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器10. 輕松學(xué)習(xí)XML教程
