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

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

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

瀏覽:74日期: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è)圖像切換器對(duì)象 imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1); // 獲取圖像切換器 // 設(shè)置動(dòng)畫效果 imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); // 設(shè)置淡入動(dòng)畫 imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); // 設(shè)置淡出動(dòng)畫 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>

說明:

drawable中,加入下列圖片img01~img09

效果圖:

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

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

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 桂东县| 内黄县| 兴安盟| 镇赉县| 邳州市| 当涂县| 西青区| 扬州市| 西贡区| 张家口市| 华阴市| 德州市| 南川市| 吉林省| 永仁县| 高安市| 定安县| 西和县| 邵武市| 尼玛县| 桦南县| 九江县| 晋宁县| 吴川市| 大渡口区| 图片| 辉南县| 柳林县| 太白县| 伽师县| 太白县| 周宁县| 南宫市| 泌阳县| 无为县| 成都市| 达拉特旗| 钦州市| 江达县| 乌拉特后旗| 丰镇市|