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:id="@+id/llayout" android:gravity="center" > <Button android:text="上一張" android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageSwitcher android:id="@+id/imageSwitcher1" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:text="下一張" android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
說明:
drawable中,加入下列圖片img01~img09
效果圖:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)戰(zhàn)教程第七篇之如何在內(nèi)存中存儲用戶名和密碼
這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)在內(nèi)存中存儲用戶名和密碼的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android編程實(shí)現(xiàn)在自定義對話框中獲取EditText中數(shù)據(jù)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)在自定義對話框中獲取EditText中數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了Android對話框數(shù)據(jù)傳遞相關(guān)操作技巧,需要的朋友可以參考下2018-01-01
RecyclerView實(shí)現(xiàn)仿支付寶應(yīng)用管理
這篇文章主要為大家詳細(xì)介紹了RecyclerView實(shí)現(xiàn)仿支付寶應(yīng)用管理的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
使用Eclipse配置android開發(fā)環(huán)境教程
這篇文章主要介紹了使用Eclipse配置android開發(fā)環(huán)境教程,本文講解了下載需要用到的工具、下載完需要的工具之后開始安裝、讓Ecplise自動安裝Android開發(fā)插件(ADT- plugin)、配置Andiord SDK路徑、測試開發(fā)一個(gè)Android項(xiàng)目等內(nèi)容,需要的朋友可以參考下2015-04-04
Android實(shí)現(xiàn)系統(tǒng)消息推送
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)系統(tǒng)消息推送,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Android RecyclerView上拉加載更多功能回彈實(shí)現(xiàn)代碼
這篇文章主要介紹了Android RecyclerView上拉加載更多功能回彈實(shí)現(xiàn)代碼,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
SDL2和OpenGL使用踩坑筆記經(jīng)驗(yàn)分享
今天小編就為大家分享一篇關(guān)于SDL2和OpenGL使用踩坑筆記經(jīng)驗(yàn)分享,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12

