Android高級組件ImageSwitcher圖像切換器使用方法詳解
圖像切換器(ImageSwitcher),用于實(shí)現(xiàn)類似于Windows操作系統(tǒng)的“Windows照片查看器”中的上一張、下一張切換圖片的功能。在使用ImageSwitcher時(shí),必須實(shí)現(xiàn)ViewSwitcher.ViewFactory接口,并通過makeView()方法來創(chuàng)建用于顯示圖片的ImageView。makeView()方法將返回一個(gè)顯示圖片的ImageView。在使用圖像切換器時(shí),還有一個(gè)方法非常重要,那就是setImageResource方法,該方法用于指定要在ImageSwitcher中顯示的圖片資源。
下面通過一個(gè)實(shí)例來說明圖像切換器的用法。
res/layout/main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:id="@+id/layout" android:gravity="center"> <Button android:text="上一張" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1"/> <ImageSwitcher android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:id="@+id/imageSwitcher1"/> <Button android:text="下一張" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2"/> </LinearLayout>
MainActivity:
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity{
//聲明并初始化一個(gè)保存要顯示圖像id的數(shù)組
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};
private int index=0;//當(dāng)前顯示圖像的索引
private ImageSwitcher imageSwitcher;//聲明一個(gè)圖像切換器對象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
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() {//設(shè)置View工廠
@Override
public View makeView() {
ImageView imageView=null;
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;
}
});
imageSwitcher.setImageResource(imageId[index]);//顯示默認(rèn)的圖片
//“上一張”和“下一張”按鈕的控制
Button up=(Button)findViewById(R.id.button1);
Button down=(Button)findViewById(R.id.button2);
up.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(index>0){
index--;//圖片索引后退一個(gè)
}else{
index=imageId.length-1;//圖片達(dá)到最前面一張之后,循環(huán)至最后一張
}
imageSwitcher.setImageResource(imageId[index]);//顯示當(dāng)前圖片
}
});
down.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(index<imageId.length-1){
index++;//圖片索引前進(jìn)一個(gè)
}else{
index=0;//圖片達(dá)到最后面一張之后,循環(huán)至第一張
}
imageSwitcher.setImageResource(imageId[index]);//顯示當(dāng)前圖片
}
});
}
}
效果如圖所示:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Studio設(shè)置、改變字體和主題的方法
這篇文章主要介紹了Android Studio設(shè)置、改變字體和主題的方法,需要的朋友可以參考下2018-03-03
Flutter之Timer實(shí)現(xiàn)短信驗(yàn)證碼獲取60s倒計(jì)時(shí)功能的代碼
這篇文章主要介紹了Flutter之Timer實(shí)現(xiàn)短信驗(yàn)證碼獲取60s倒計(jì)時(shí)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Android自定義View展開菜單功能的實(shí)現(xiàn)
這篇文章主要介紹了Android自定義View展開菜單功能的實(shí)現(xiàn),需要的朋友可以參考下2017-06-06
android layout XML解析錯(cuò)誤的解決方法
從別的地方復(fù)制過來XML時(shí),layout預(yù)覽時(shí)提示解析錯(cuò)誤。2013-04-04
Android?Camera+SurfaceView自動(dòng)聚焦防止變形拉伸
這篇文章主要為大家介紹了Android自定義相機(jī)Camera+SurfaceView實(shí)現(xiàn)自動(dòng)聚焦防止變形拉伸詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

