频道栏目
首页 > 资讯 > Android > 正文

Android入门第十三篇之Gallery + ImageSwitcher

11-01-10        来源:[db:作者]  
收藏   我要投稿

上次讲了如何使用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,  
  &

相关TAG标签
上一篇:Android入门第十四篇之画图
下一篇:Android入门第十二篇之Gallery
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站