欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年10月23日 15:01:07   作者:magiz19  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖像切換器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

效果圖:

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

相關(guān)文章

最新評(píng)論