本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


前面的一节,介绍了如何组合使用Gallery和ImageView。但是,有的时候当你在gallery中点击一个图片,你可能不希望一个图片“突然地”在imageview中显示出来。例如,你可能希望给某个图片设置一些切换动画。此时,就需要使用ImageSwitcher和Gallery一起使用。下面展示如何使用ImageSwitcher。

1. 创建一个工程,ImageSwitcher。

2. main.xml中的代码。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >        <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Images of San Francisco" />        <Gallery        android:id="@+id/gallery1"        android:layout_width="fill_parent"        android:layout_height="wrap_content" />    <ImageSwitcher        android:id="@+id/switcher1"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_alignParentLeft="true"        android:layout_alignParentRight="true"        android:layout_alignParentBottom="true" /></LinearLayout>
3. 在res/values文件夹下面新建一个文件,attrs.xml。

4. attrs.xml中的代码。

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

5. 在res/drawable-mdpi中放置一些图片。


6. ImageSwitcherActivity.java中的代码。

public class ImageSwitcherActivity extends Activity implements ViewFactory {    //---the images to display---    Integer[] imageIDs = {            R.drawable.pic1,            R.drawable.pic2,            R.drawable.pic3,            R.drawable.pic4,            R.drawable.pic5,            R.drawable.pic6,            R.drawable.pic7    };    private ImageSwitcher imageSwitcher;        /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);        imageSwitcher.setFactory(this);                /*       imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,                android.R.anim.fade_in));        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,                android.R.anim.fade_out));        */                imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,                android.R.anim.slide_in_left));        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,                android.R.anim.slide_out_right));        Gallery gallery = (Gallery) findViewById(R.id.gallery1);        gallery.setAdapter(new ImageAdapter(this));        gallery.setOnItemClickListener(new OnItemClickListener()        {            public void onItemClick(AdapterView<?> parent,            View v, int position, long id)            {                imageSwitcher.setImageResource(imageIDs[position]);            }        });    }        public View makeView()    {        ImageView imageView = new ImageView(this);        imageView.setBackgroundColor(0xFF000000);        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);        imageView.setLayoutParams(new                ImageSwitcher.LayoutParams(                        LayoutParams.FILL_PARENT,                        LayoutParams.FILL_PARENT));        return imageView;    }    public class ImageAdapter extends BaseAdapter    {        private Context context;        private int itemBackground;        public ImageAdapter(Context c)        {            context = c;            //---setting the style---            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);            itemBackground = a.getResourceId(                    R.styleable.Gallery1_android_galleryItemBackground, 0);            a.recycle();        }        //---returns the number of images---        public int getCount()        {            return imageIDs.length;        }        //---returns the item---        public Object getItem(int position)        {            return position;        }        //---returns the ID of an item---        public long getItemId(int position)        {            return position;        }        //---returns an ImageView view---        public View getView(int position, View convertView, ViewGroup parent)        {            ImageView imageView = new ImageView(context);                        imageView.setImageResource(imageIDs[position]);            imageView.setScaleType(ImageView.ScaleType.FIT_XY);            imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));            imageView.setBackgroundResource(itemBackground);                        return imageView;        }    }}
7. 按F11在模拟器上面调试。会看见Gallery和ImageSwitcher。


首先,要注意,ImageSwitcherActivity不紧继承了Activity,而且实现了ViewFactory接口。要使用ImageSwitcher,就需要实现ViewFactory接口,这个接口为ImageSwitcher创建视图。只需实现makeView()方法就可以了。

public View makeView()    {        ImageView imageView = new ImageView(this);        imageView.setBackgroundColor(0xFF000000);        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);        imageView.setLayoutParams(new                ImageSwitcher.LayoutParams(                        LayoutParams.FILL_PARENT,                        LayoutParams.FILL_PARENT));        return imageView;    }
这个方法返回一个新的view,这个view将会被添加到ImageSwitcher中,在本例中,此方法返回一个ImageView。

像上一节的例子一样,我们要实现一个ImageAdapter子类,目的是把Gallery和ImageView绑定在一起。

在onCreate()方法中,我们获得ImageSwitcher实例的引用,同时设定了动画animation,这个动画在图片切换的时候被使用。

最后,当一个图片在Gallery中被选择的时候,这个图片就在ImageSwitcher中显示出来的。

    private ImageSwitcher imageSwitcher;        /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);        imageSwitcher.setFactory(this);                /*        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,                android.R.anim.fade_in));        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,                android.R.anim.fade_out));        */                imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,                android.R.anim.slide_in_left));        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,                android.R.anim.slide_out_right));        Gallery gallery = (Gallery) findViewById(R.id.gallery1);        gallery.setAdapter(new ImageAdapter(this));        gallery.setOnItemClickListener(new OnItemClickListener()        {            public void onItemClick(AdapterView<?> parent,            View v, int position, long id)            {                imageSwitcher.setImageResource(imageIDs[position]);            }        });    }
在这个例子中,通过以下的方法设置动画。

        /*        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,                android.R.anim.fade_in));        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,                android.R.anim.fade_out));        */                imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,                android.R.anim.slide_in_left));        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,                android.R.anim.slide_out_right));


更多相关文章

  1. android 很多应用中用到的 listView + viewPager
  2. android模拟器接收不到UDP数据包解决方法
  3. android,java知识点总结(一)
  4. 【Android】 字体适配——不跟随系统字体大小、动态设置字体大小
  5. Android(安卓)启动画面跳转和去掉标题栏
  6. android studio引用java8后编译报错解决方法
  7. android 获取视频,图片缩略图
  8. Android-Activity介绍
  9. Android(安卓)实现轮播图效果(二) 底部圆点布局实现

随机推荐

  1. 详解PHP中的数据库连接持久化
  2. PHP缓存系统APCu扩展的使用
  3. PHP中include和require的使用详解
  4. 详解iOS中跨页面状态同步方案比较
  5. css权重和伪类选择器
  6. PHP递归函数,MySql建简单用户表,进行增删查
  7. 解决iOS13 无法获取WiFi名称(SSID)问题
  8. iOS 13适配汇总(推荐)
  9. iOS导航栏对控制器view的影响详解
  10. js常用函数类型和数据类型