java.lang.Object | ||||||
↳ | android.view.View | |||||
↳ | android.view.ViewGroup | |||||
↳ | android.widget.FrameLayout | |||||
↳ | android.widget.ViewAnimator | |||||
↳ | android.widget.ViewSwitcher | |||||
↳ | android.widget.ImageSwitcher |
(译者注:ImageSwitcher是Android中控制图片展示效果的一个控件,如:幻灯片效果...,颇有感觉啊,做相册一绝。)
我们在Windows 平台上要查看多张图片,最简单的办法就是通过 "Window 图片和传真查看器“在 ”下一张“ 和”上一张“之间切换,Android平台上可以通过 ImageSwitcher 类来实现这一效果。ImageSwitcher 类必须设置一个ViewFactory,主要用来将显示的图片和父窗口区分开来,因此需要实现ViewSwitcher.ViewFactory接口,通过makeView()方法来显示图片,这里会返回一个ImageView 对象,而方法 setImageResource用来指定图片资源。首先先让我们看看这个例子的运行效果。
一、重要方法
setImageURI(Uri uri):设置图片地址
setImageResource(int resid):设置图片资源库
setImageDrawable(Drawable drawable):绘制图片
=========================================================
案例一:
1.定义布局文件imageswitcher.xml
<?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/imageSwitcher_id" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" > </ImageSwitcher> <Gallery android:id="@+id/Imageswitcher_gallery_id" android:background="#55000000" android:layout_width="match_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:gravity="center_vertical" android:spacing="16dp"> </Gallery> </RelativeLayout>
2.定义java文件ImageSwitcherDemo.java
package com.test; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.Window; 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.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Gallery.LayoutParams; import android.widget.ViewSwitcher.ViewFactory; public class ImageSwitcherDemo extends Activity implements OnItemSelectedListener, ViewFactory { private ImageSwitcher is; private Gallery gallery; private Integer[] mThumbIds = { R.drawable.gallery_01,R.drawable.gallery_02,R.drawable.gallery_03, R.drawable.gallery_04,R.drawable.gallery_05,R.drawable.gallery_06, }; private Integer[] mImageIds = { R.drawable.gallery_01, R.drawable.gallery_02,R.drawable.gallery_03, R.drawable.gallery_04,R.drawable.gallery_05,R.drawable.gallery_06, }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.imageswitcher); is = (ImageSwitcher) findViewById(R.id.switcher); is.setFactory(this); is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); gallery = (Gallery) findViewById(R.id.gallery); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemSelectedListener(this); } @Override public View makeView() { ImageView i = new ImageView(this); i.setBackgroundColor(0xFF000000); i.setScaleType(ImageView.ScaleType.FIT_CENTER); i.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); return i; } public class ImageAdapter extends BaseAdapter { public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return 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.); return i; } private Context mContext; } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { is.setImageResource(mImageIds[position]); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }
3.执行效果:
===================================================
案例二:自定义:
1.自定义一个Gallery文件:
package com.test; import java.text.AttributedCharacterIterator.Attribute; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.Gallery; public class Gallery_define extends Gallery { public Gallery_define(Context context,AttributeSet attrs) { super(context,attrs); // TODO Auto-generated constructor stub } //重写onFling方法,将滑动的速率降低 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // TODO Auto-generated method stub return super.onFling(e1, e2, velocityX/3, velocityY); } }
2:接下来自定义一个gallery布局文件:gallery_define.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/item_gallery_image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:scaleType="fitXY"/> <TextView android:id="@+id/item_gallery_text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center"/> </LinearLayout>
3:定义布局文件:gallerydemo.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.test.Gallery_define android:id="@+id/gallery_define" android:background="#55000000" android:layout_width="match_parent" android:layout_height="80dp" android:gravity="center_vertical" android:spacing="2dp" > </com.test.Gallery_define> <ImageSwitcher android:id="@+id/switcher_define" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout>
4.java文件:ImageSwitcherGallery_define
package com.test; import java.util.HashMap; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.res.TypedArray; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.Window; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.TextView; import android.widget.ViewSwitcher.ViewFactory; public class ImageSwitcherGallery_define extends Activity implements OnItemSelectedListener, ViewFactory { //定义ImageSwitcher类对象 private ImageSwitcher imageSwitcher; //文本资源w private String []titles={"标题A","标题B","标题C","标题D","标题E","标题F"}; //大图资源 private int[] ThumbIds= { R.drawable.gallery_01,R.drawable.gallery_02,R.drawable.gallery_03, R.drawable.gallery_04,R.drawable.gallery_05,R.drawable.gallery_06, }; //大图资源对于的小图资源 private int[] imageIds= { R.drawable.gallery_01,R.drawable.gallery_02,R.drawable.gallery_03, R.drawable.gallery_04,R.drawable.gallery_05,R.drawable.gallery_06, }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //设置窗体无标题 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.gallerydemo); imageSwitcher = (ImageSwitcher)findViewById(R.id.switcher_define); imageSwitcher.setFactory(this); imageSwitcher=(ImageSwitcher)findViewById(R.id.switcher_define); //设置图片的滑动效果 imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); Gallery gallery=(Gallery)findViewById(R.id.gallery_define); //设置Gallery的适配器 gallery.setAdapter(new ImageAdapter(this,ThumbIds.length)); //Gallery中每个条目的点击事件监听 gallery.setOnItemClickListener(itemListener); //设置默认起始位置为第二张图片 gallery.setSelection(1); } OnItemClickListener itemListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { if (curruntPos == position) { return; } else curruntPos = position; imageSwitcher.setImageResource(ThumbIds[position % ThumbIds.length]); } }; @Override public View makeView() { ImageView imageView =new ImageView(this); imageView.setBackgroundColor(TRIM_MEMORY_BACKGROUND); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); return imageView; } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { imageSwitcher.setImageResource(ThumbIds[arg2%imageIds.length]); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } class ImageAdapter extends BaseAdapter { private int galleryItenBackgroud; //定义map存储划过的位置 private HashMap<Integer, View> viewMaps; private int count; //定义布局加载器 private LayoutInflater inflater; public ImageAdapter(Context c,int count) { this.count=count; viewMaps = new HashMap<Integer, View>(count); inflater=LayoutInflater.from(ImageSwitcherGallery_define.this); //定义图片的背景样式 TypedArray a=obtainStyledAttributes(R.styleable.Gallery); galleryItenBackgroud=a.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0); //定义可以重复使用,可回收 a.recycle(); } @SuppressLint("ParserError") @Override public int getCount() { // 设置循环的次数 return Integer.MAX_VALUE; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View viewGroup=viewMaps.get(position%count); ImageView imageView=null; TextView textView = null; if(viewGroup==null) { viewGroup =inflater.inflate(R.layout.gallery_define, null); imageView=(ImageView)viewGroup.findViewById(R.id.item_gallery_image); textView = (TextView)viewGroup.findViewById(R.id.item_gallery_text); imageView.setBackgroundResource(galleryItenBackgroud); viewMaps.put(position%count, viewGroup); imageView.setImageResource(imageIds[position%imageIds.length]); textView.setText(titles[position%imageIds.length]); } return viewGroup; } } //记录当前位置 private int curruntPos=-1; }
5:效果图: