Gallery + ImageSwitcher 实现简单的电子相册
学习了一下android中的Gallery + ImageSwitcher,在网上找了点资料,做了个简单的电子相册。
并实现了自动播放效果,通过双击的手势来开启关闭自动播放。上图才是王道。
主要实现代码
package com.eebbk.elealbum.activity;import android.app.Activity;import android.content.Context;import android.content.res.TypedArray;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.GestureDetector;import android.view.KeyEvent;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.view.GestureDetector.SimpleOnGestureListener;import android.view.ViewGroup.LayoutParams;import android.view.Window;import android.view.WindowManager;import android.view.animation.AnimationUtils;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher.ViewFactory;public class MainActivity extends Activity implements ViewFactory, OnItemSelectedListener, Runnable{//自动播放的标志位private boolean isPlay = false; //自动播放更新消息private final static int UPDATE = 1;//当前播放索引值private int cur_index = 0;// GestureDetector为手势识别类private GestureDetector mGestureDetector;private Gallery gallery = null;private ImageSwitcher imageSwitcher = null;private int[] imageIds =new int[]{ R.drawable.pic1 , R.drawable.pic2, R.drawable.pic3 , R.drawable.pic4, R.drawable.pic5 , R.drawable.pic6, R.drawable.pic7 , R.drawable.pic8, };private Handler handler = new Handler( ) {@Overridepublic void handleMessage( Message msg ){// TODO Auto-generated method stubif ( msg.what == UPDATE ){gallery.setSelection( msg.arg1 );}super.handleMessage( msg );}};/** Called when the activity is first created. */@Overridepublic void onCreate( Bundle savedInstanceState ){super.onCreate( savedInstanceState );//设置无标题 requestWindowFeature(Window.FEATURE_NO_TITLE); //设置全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView( R.layout.main );init( );}private void init( ){mGestureDetector = new GestureDetector( new MySimpleGesture( ));gallery = ( Gallery ) findViewById( R.id.gallery_id );gallery.setAdapter(new ImageAdaper( this )); gallery.setOnItemSelectedListener(this); imageSwitcher = ( ImageSwitcher ) findViewById( R.id.imageSwitcher_id );imageSwitcher.setFactory( this );//设置淡入淡出切换效果imageSwitcher.setInAnimation( AnimationUtils.loadAnimation( this, android.R.anim.fade_in ) );imageSwitcher.setOutAnimation( AnimationUtils.loadAnimation( this, android.R.anim.fade_out ) );}@Overridepublic boolean onTouchEvent( MotionEvent event ){// TODO Auto-generated method stubmGestureDetector.onTouchEvent( event );return super.onTouchEvent( event );}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if(keyCode == KeyEvent.KEYCODE_BACK){isPlay = false;}return super.onKeyDown(keyCode, event);}/* (non-Javadoc) * @see android.widget.ViewSwitcher.ViewFactory#makeView() */@Overridepublic View makeView( ){// TODO Auto-generated method stubImageView imageView = new ImageView(this); imageView.setScaleType(imageView.getScaleType().FIT_CENTER); imageView.setBackgroundColor(0xFF000000); imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT)); return imageView;} public class ImageAdaper extends BaseAdapter{ Context context; int mGalleryItemBackground; public ImageAdaper(Context context){ this.context = context; TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery); mGalleryItemBackground = typedArray.getResourceId( R.styleable.Gallery_android_galleryItemBackground, 0); typedArray.recycle(); } public int getCount() { // TODO Auto-generated method stub return Integer.MAX_VALUE; } public Object getItem(int position) { // TODO Auto-generated method stub return imageIds[position]; } public long getItemId(int position) { // TODO Auto-generated method stub return position; } public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView imageview = new ImageView(this.context); imageview.setImageResource(imageIds[position % imageIds.length]); imageview.setScaleType(ImageView.ScaleType.FIT_XY); imageview.setLayoutParams(new Gallery.LayoutParams(136 , 88)); imageview.setBackgroundResource(mGalleryItemBackground); return imageview; } }/* (non-Javadoc) * @see android.widget.AdapterView.OnItemSelectedListener#onItemSelected(android.widget.AdapterView, android.view.View, int, long) */@Overridepublic void onItemSelected( AdapterView< ? > parent, View view, int position, long id ){//gallery选中一项之后,切换图片System.out.println("position: " + position + "cur_index: " + cur_index);imageSwitcher.setImageResource(imageIds[position%imageIds.length]); }/* (non-Javadoc) * @see android.widget.AdapterView.OnItemSelectedListener#onNothingSelected(android.widget.AdapterView) */@Overridepublic void onNothingSelected( AdapterView< ? > parent ){// TODO Auto-generated method stub}/* (non-Javadoc) * @see java.lang.Runnable#run() */@Overridepublic void run( ){// TODO Auto-generated method stubwhile(isPlay) { //cur_index = cur_index % imageIds.length; Message msg = handler.obtainMessage(UPDATE, cur_index, 0); handler.sendMessage(msg); //更新时间间隔为 2s try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } cur_index++; }}private class MySimpleGesture extends SimpleOnGestureListener{// 按两下的第二下Touch down时触发public boolean onDoubleTap( MotionEvent e ){//双击触发切换自动播放if ( isPlay == false ){isPlay = true;new Thread( MainActivity.this).start( );}else {isPlay = false;}return true;}} }
源码附上:http://download.csdn.net/detail/tianmi1988/4914807