7.1.1 ImageSwitcher案例分析详解

ImageSwitcher图像切换器,可以切换器图像。ImageSwitcher类的继承图如下:
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.widget.ViewAnimator
           ↳ android.widget.ViewSwitcher
             ↳ android.widget.ImageSwitcher
android.widget.ImageSwitcher继承了android.widget.FrameLayout框架布局类。为了便于学习,我们把官方的ImageSwitcher例子(ApiDemos/src/com/example/android/apis/view/ImageSwitcher1.java)做了一些修改,例子运行如图7-6屏幕,点击屏幕下面的缩图,实现图片的切换。
 


图7-6 ImageSwitcher
请参考代码清单7-4,完整代码请参考chapter7_1工程中MyImageSwitcher代码部分。
【代码清单7-4】
public class MyImageSwitcher extends Activity {

private ImageSwitcher mSwitcher;

private Integer[] mThumbIds = { R.drawable.photo1_thumb,
R.drawable.photo2_thumb, R.drawable.photo3_thumb,
R.drawable.photo4_thumb, R.drawable.photo5_thumb,
R.drawable.photo6_thumb };

private Integer[] mImageIds = { R.drawable.photo1, R.drawable.photo2,
R.drawable.photo3, R.drawable.photo4, R.drawable.photo5,
R.drawable.photo6 };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.image_switcher);

mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(new ViewSwitcher.ViewFactory() {

@Override
public View makeView() {
ImageView i = new ImageView(MyImageSwitcher.this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return i;
}
});

Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView parent, View v,
int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

… …

}
… …
}
在ImageSwitcher图像切换器中需要提供缩图数组集合mThumbIds和正常图片集合mImageIds。requestWindowFeature(Window.FEATURE_NO_TITLE)方法是设置没有标题的屏幕。
实现图像切换关键代码是mSwitcher.setFactory(new ViewSwitcher.ViewFactory() {…}),需要提供一个ViewSwitcher.ViewFactory接口为图像切换的时候创建一个View对象,由于是图像切换器,这个View是一个ImageView类的实例,i.setBackgroundColor(0xFF000000)指定背景颜色,i.setScaleType(ImageView.ScaleType.FIT_CENTER)指定排列方式为居中。下面的代码就是设置图片的布局与父容器匹配模式:
i.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
在屏幕下面的缩图是Gallery类型,点击缩图触发事件AdapterView.OnItemSelectedListener()时改变图片切换器的图片源mSwitcher.setImageResource(mImageIds[position]),从而实现图片的切换。
缩图的Gallery还必须实现一个BaseAdapter适配器,代码请参考代码清单7-5,完整代码请参考chapter7_1工程中MyImageSwitcher代码部分。
【代码清单7-5】
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}

public int getCount() {
return mThumbIds.length;
}

public Object getItem(int position) {
return mThumbIds[position];
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mThumbIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.picture_frame);
return i;
}

private Context mContext;

}
getView()方法中返回缩图的对象,i.setImageResource(mThumbIds[position])是设置图片源,i.setAdjustViewBounds(true)设置图片对象边框自动调节,i.setLayoutParams()设置图片控件的布局,i.setBackgroundResource设置背景图片。
                                               出自《Android开发案例驱动教程》第七章
posted @ 2011-07-21 10:50  516inc  阅读(194)  评论(0编辑  收藏  举报