public class

ImageSwitcher

extends ViewSwitcher
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.FrameLayout
android.widget.ViewAnimator
android.widget.ViewSwitcher
android.widget.ImageSwitcher

(译者注:ImageSwitcherAndroid中控制图片展示效果的一个控件,如:幻灯片效果...,颇有感觉啊,做相册一绝。)

我们在Windows 平台上要查看多张图片,最简单的办法就是通过 "Window 图片和传真查看器“在 ”下一张“ 和”上一张“之间切换,Android平台上可以通过 ImageSwitcher 类来实现这一效果。ImageSwitcher 类必须设置一个ViewFactory,主要用来将显示的图片和父窗口区分开来,因此需要实现ViewSwitcher.ViewFactory接口,通过makeView()方法来显示图片,这里会返回一个ImageView 对象,而方法 setImageResource用来指定图片资源。首先先让我们看看这个例子的运行效果。

一、重要方法

    setImageURI(Uriuri):设置图片地址

    setImageResource(int resid):设置图片资源库

    setImageDrawable(Drawabledrawable):绘制图片

=========================================================

案例一:

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 implementsOnItemSelectedListener, 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,};@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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);}     @Overridepublic 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;}    @Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position,long id) {is.setImageResource(mImageIds[position]);}    @Overridepublic 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方法,将滑动的速率降低@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {// TODO Auto-generated method stubreturn 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 implementsOnItemSelectedListener, ViewFactory {  //定义ImageSwitcher类对象private  ImageSwitcher  imageSwitcher;//文本资源wprivate   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,};@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {if (curruntPos == position) {            return;        } else            curruntPos = position;         imageSwitcher.setImageResource(ThumbIds[position % ThumbIds.length]);}};@Overridepublic 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;}@Overridepublic void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {imageSwitcher.setImageResource(ThumbIds[arg2%imageIds.length]);}@Overridepublic 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")@Overridepublic int getCount() {// 设置循环的次数return Integer.MAX_VALUE;}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubView 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:效果图:

更多相关文章

  1. Android图片上传工具类
  2. android例子分析-1
  3. android访问网络图片
  4. android ndk 常用宏定义
  5. android 获取网络图片
  6. Android(安卓)ProgressBar 进度条颜色和背景颜色
  7. android MediaRecorder 视频录制
  8. Android(安卓)自定义控件之图片裁剪
  9. android EditText组件

随机推荐

  1. 禁止Android中的返回键
  2. BottomNavigationView+ViewPager打造底部
  3. 内存优化二
  4. 根据IP地址定位城市
  5. ExoPlayer2.5 的简单使用
  6. smartrefreshlayout 只开启纯滚动模式
  7. Android-AsyncTask源码学习
  8. android 监听app进入后台以及从后台进入
  9. Android拆分Bitmap完整示例
  10. 模拟ProgressBar下载进度显示