国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Android實(shí)現(xiàn)圖像切換器

瀏覽:97日期:2022-09-22 08:48:13

本文實(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

效果圖:

Android實(shí)現(xiàn)圖像切換器

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 缙云县| 凤凰县| 双鸭山市| 大渡口区| 兴仁县| 大荔县| 海丰县| 拜泉县| 白沙| 鹿泉市| 云霄县| 太仆寺旗| 扎赉特旗| 姚安县| 吴堡县| 和平县| 屏南县| 中卫市| 仁怀市| 孟州市| 五常市| 平舆县| 和田市| 承德市| 轮台县| 左权县| 吉水县| 永善县| 呼伦贝尔市| 调兵山市| 宝坻区| 右玉县| 额尔古纳市| 华容县| 通许县| 新余市| 三都| 滁州市| 西宁市| 定南县| 子长县|