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è)圖像切換器對象 imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1); // 獲取圖像切換器 // 設(shè)置動畫效果 imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); // 設(shè)置淡入動畫 imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); // 設(shè)置淡出動畫 imageSwitcher.setFactory(new ViewFactory() { @Override public View makeView() { ImageView imageView = new ImageView(MainActivity.this); // 實(shí)例化一個(gè)ImageView類的對象 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); // 設(shè)置保持縱橫比居中縮放圖像 imageView.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); return imageView; // 返回imageView對象 } }); 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>
說明:
drawable中,加入下列圖片img01~img09
效果圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python GUI庫圖形界面開發(fā)之PyQt5動態(tài)(可拖動控件大小)布局控件QSplitter詳細(xì)使用方法與實(shí)例2. vue跳轉(zhuǎn)頁面常用的幾種方法匯總3. 父div高度不能自適應(yīng)子div高度的解決方案4. react拖拽組件react-sortable-hoc的使用5. CSS清除浮動方法匯總6. 不要在HTML中濫用div7. js開發(fā)中的頁面、屏幕、瀏覽器的位置原理(高度寬度)說明講解(附圖)8. XML 非法字符(轉(zhuǎn)義字符)9. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)10. ASP動態(tài)include文件
