Android入門第十三篇之Gallery + ImageSwitcher

來源:互聯網
上載者:User

上次講了如何使用Gallery控制項,這次就講Gallery 與ImageSwitcher的結合使用,本文實現一個簡單的瀏覽圖片的功能。先貼出程式運行:

除了Gallery可以拖拉切換圖片,我在ImageSwitcher控制項加入了setOnTouchListener事件實現,使得ImageSwitcher也可以在拖拉中切換圖片。本例子依然使用JAVA的反射機制來自動讀取資源中的圖片。

 

main.xml的源碼如下:

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:layout_width="match_parent"   
    android:layout_height="match_parent">   
      
    <ImageSwitcher android:id="@+id/switcher" 
        android:layout_width="match_parent" android:layout_height="match_parent"/> 
      
    <Gallery android:id="@+id/gallery" 
        android:background="#55000000" 
        android:layout_width="match_parent" 
        android:layout_alignParentBottom="true" 
        android:layout_alignParentLeft="true" 
          
        android:gravity="center_vertical" 
        android:spacing="16dp" android:layout_height="100dp"/> 
</RelativeLayout> 
    
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   
    <ImageSwitcher android:id="@+id/switcher"
        android:layout_width="match_parent" android:layout_height="match_parent"/>
   
    <Gallery android:id="@+id/gallery"
        android:background="#55000000"
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
       
        android:gravity="center_vertical"
        android:spacing="16dp" android:layout_height="100dp"/>
</RelativeLayout>
  
 

 

程式的源碼如下:

view plaincopy to clipboardprint?
package com.testImageView;  
import java.lang.reflect.Field;  
import java.util.ArrayList;  
import android.app.Activity;  
import android.content.Context;  
import android.os.Bundle;  
import android.view.MotionEvent;  
import android.view.View;  
import android.view.View.OnTouchListener;  
import android.view.ViewGroup;  
import android.view.animation.AnimationUtils;  
import android.widget.AdapterView;  
import android.widget.BaseAdapter;  
import android.widget.Gallery;  
import android.widget.ImageSwitcher;  
import android.widget.ImageView;  
import android.widget.AdapterView.OnItemSelectedListener;  
import android.widget.Gallery.LayoutParams;  
import android.widget.ViewSwitcher.ViewFactory;  
public class testImageView extends Activity implements ViewFactory {  
    private ImageSwitcher is;  
    private Gallery gallery;  
    private int downX,upX;  
    private ArrayList<Integer> imgList=new ArrayList<Integer>();//映像ID  
      
    @Override 
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        //用反射機制來擷取資源中的圖片ID  
        Field[] fields = R.drawable.class.getDeclaredFields();  
        for (Field field : fields)  
        {  
            if (!"icon".equals(field.getName()))//除了icon之外的圖片  
            {     
                int index = 0;  
                try {  
                    index = field.getInt(R.drawable.class);  
                } catch (IllegalArgumentException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                } catch (IllegalAccessException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
                //儲存圖片ID  
                imgList.add(index);  
            }  
        }  
          
        //設定ImageSwitcher控制項  
        is = (ImageSwitcher) findViewById(R.id.switcher);  
        is.setFactory(this);  
        is.setInAnimation(AnimationUtils.loadAnimation(this,  
  &

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.