博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android ImageSwitcher案例
阅读量:5974 次
发布时间:2019-06-19

本文共 9694 字,大约阅读时间需要 32 分钟。

  hot3.png

public class

ImageSwitcher

extends 
   ↳
     ↳
       ↳
         ↳
           ↳
             ↳ android.widget.ImageSwitcher

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

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

一、重要方法

    setImageURI(Uri uri):设置图片地址

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

    setImageDrawable(Drawable drawable):绘制图片

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

案例一:

1.定义布局文件imageswitcher.xml

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

3:定义布局文件:gallerydemo.xml

 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
viewMaps; private int count; //定义布局加载器 private LayoutInflater inflater; public ImageAdapter(Context c,int count) { this.count=count; viewMaps = new HashMap
(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:效果图:

 

 

转载于:https://my.oschina.net/amigos/blog/65986

你可能感兴趣的文章
Python 模块笔记
查看>>
System Center Service Manager中Runbook的状态
查看>>
windows平台下gvim中文支持
查看>>
一种windows***方法
查看>>
2011年9大破产科技预言
查看>>
Fragment 在Android SDK1.6上实现
查看>>
jquery实现本地图片上传预览和限流处理
查看>>
Vim编辑器-Shell脚本
查看>>
Windows7 无法创建文件夹
查看>>
flex拖拽
查看>>
我的友情链接
查看>>
tomcat设置httpOnly
查看>>
部署SQL server 2008 R2三节点多实例故障转移群集全过程
查看>>
NOIP 2015[D2 T1] 跳石头
查看>>
我的友情链接
查看>>
Nginx(一)安装配置
查看>>
java uploadify上传图片并预览
查看>>
Nginx反向代理proxy_store配置
查看>>
域环境users提权调用lsrunase.exe 批处理完成安装字体
查看>>
python实现按创建时间对文件排序
查看>>