溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android實現(xiàn)幻燈片式圖片瀏覽器的方法

發(fā)布時間:2021-04-17 10:03:54 來源:億速云 閱讀:356 作者:小新 欄目:移動開發(fā)

這篇文章給大家分享的是有關(guān)Android實現(xiàn)幻燈片式圖片瀏覽器的方法的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

我們來實現(xiàn)一個幻燈片式圖片瀏覽器:

最下面一個畫廊視圖,選中畫廊中的圖片,會在上面的ImageSwitcher控件中顯示大圖。

效果圖如圖

Android實現(xiàn)幻燈片式圖片瀏覽器的方法

實現(xiàn)方法:

在布局文件中添加圖片切換控件ImageSwitcher和畫廊視圖控件Gallery
res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  android:orientation="vertical"  
  android:layout_width="fill_parent"  
  android:layout_height="fill_parent"  
  android:id="@+id/layout1" 
  android:gravity="center_horizontal" 
  >  
  <ImageSwitcher 
    android:id="@+id/imageSwitcher" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingTop="30px" 
    android:layout_weight="2"/> 
  <Gallery 
    android:id="@+id/gallery1" 
    android:spacing="5px" 
    android:layout_weight="1" 
    android:unselectedAlpha="0.6" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 
</LinearLayout>

在res/values目錄中,創(chuàng)建一個名為attr.xml的文件,在該文件中定義一個styleable對象,用于組合多個屬性。這里只指定了一個系統(tǒng)自帶的android:galleryItemBackground屬性,用于設(shè)置各選項的背景。具體代碼如下:
res/values/attr.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
  <declare-styleable name="Gallery"> 
    <attr name="android:galleryItemBackground"/> 
  </declare-styleable> 
</resources>

MainActivity:

package com.example.test;  
  
import android.app.Activity; 
import android.content.res.TypedArray; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.ViewGroup.LayoutParams; 
import android.view.animation.AnimationUtils; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageSwitcher; 
import android.widget.ImageView; 
import android.widget.ViewSwitcher.ViewFactory; 
  
public class MainActivity extends Activity {  
  //定義并初始化保存圖片id的數(shù)組 
   private int[] imageId=new int[]{R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4, 
       R.drawable.img5,R.drawable.img6,R.drawable.img7,R.drawable.img8,R.drawable.img9}; 
   //聲明一個圖像切換器對象 
   private ImageSwitcher imageSwitcher; 
  @Override  
  public void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  
      
    Gallery gallery=(Gallery)findViewById(R.id.gallery1);//獲取gallery組件 
     
    imageSwitcher=(ImageSwitcher)findViewById(R.id.imageSwitcher);//獲取圖像切換器 
    //設(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);//實例化一個imageView類的對象 
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);//設(shè)置保持縱橫比居中縮放圖像 
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams( 
            LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
        return imageView;//返回imageView對象 
      } 
    }); 
     
    BaseAdapter adapter=new BaseAdapter(){ 
 
 
      @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 
        ImageView imageView;//聲明ImageView的對象 
        if(convertView==null){ 
          imageView=new ImageView(MainActivity.this);//實例化ImageView的對象 
          imageView.setScaleType(ImageView.ScaleType.FIT_XY);//設(shè)置縮放方式 
          imageView.setLayoutParams(new Gallery.LayoutParams(180,135)); 
          //設(shè)置gallery每一項圖片的背景資源(使用的是attr.xml的自定義樣式) 
          TypedArray typedArray=obtainStyledAttributes(R.styleable.Gallery); 
          imageView.setBackgroundResource(typedArray.getResourceId( 
              R.styleable.Gallery_android_galleryItemBackground, 0)); 
           
          imageView.setPadding(5, 0, 5, 0);//設(shè)置imageView的內(nèi)邊距 
        }else{ 
          imageView=(ImageView)convertView; 
        } 
        imageView.setImageResource(imageId[position]);//為imageView設(shè)置要顯示的圖片 
        return imageView;//返回ImageView 
      } 
       
      //功能:獲得當前選項的id 
      @Override 
      public long getItemId(int position) { 
        return position; 
      } 
       
      //功能:獲得當前選項 
      @Override 
      public Object getItem(int position) { 
        return position; 
      } 
       
      //獲得數(shù)量 
      @Override 
      public int getCount() { 
        return imageId.length; 
      } 
    }; 
     
    gallery.setAdapter(adapter);//將適配器與Gallery關(guān)聯(lián) 
    gallery.setSelection(imageId.length/2);//選中中間的圖片 
    gallery.setOnItemSelectedListener(new OnItemSelectedListener() { 
 
 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View v, 
          int position, long id) { 
        imageSwitcher.setImageResource(imageId[position]);//顯示選中的圖片 
      } 
 
 
      @Override 
      public void onNothingSelected(AdapterView<?> arg0) { 
        // TODO Auto-generated method stub 
      } 
    }); 
  }  
}

這樣單擊某張圖片,可以選中該圖片,并且讓其居中顯示,也可以用手指拖動圖片來移動圖片,并且讓選中的圖片在上方顯示,如圖是切換瞬間的效果

Android實現(xiàn)幻燈片式圖片瀏覽器的方法

感謝各位的閱讀!關(guān)于“Android實現(xiàn)幻燈片式圖片瀏覽器的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI