Android 之 Galley, 手指滑动翻看图片

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ethan.activity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
       	>
        <activity android:name=".MyActivity"
            android:label="@string/app_name">
        
          <intent-filter>
			<action android:name="android.intent.action.MAIN"/>
			<category android:name="android.intent.category.LAUNCHER"/>              
          </intent-filter>
        </activity>
        
    </application>

</manifest>


布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true">
    </ImageSwitcher>
	 <Gallery android:id="@+id/gallery" 
	     android:layout_width="fill_parent"  
        android:layout_height="60dp" 
        android:background="#55000000"  
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:gravity="center_vertical"  
        android:spacing="16dp" />  
</RelativeLayout>

package com.ethan.activity;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
/**
 * 供Gallery调用
 * @author ETHAN
 *
 */
public class ImageAdapter extends BaseAdapter {
	
	private Context context;
	/*public static Integer[] imageIDs = {
		
	};*/
	
	public static List<Integer> imgs1 = new ArrayList<Integer>();
	public static List<Integer> imgs2= new ArrayList<Integer>();
	//利用反射 读取 res/drawable 下边的图片ID
	static {
		/*
		 * public final class R {
			    public static final class attr {
			    }
			    public static final class drawable {
				        public static final int ic_launcher=0x7f020000;
				        public static final int image1=0x7f020001;
				        public static final int image2=0x7f020002;
				        public static final int image3=0x7f020003;
				        public static final int image4=0x7f020004;
				        public static final int image5=0x7f020005;
    			}
		 */
		
		Pattern p1= Pattern.compile("sample_thumb_\\d");
		Pattern p2 = Pattern.compile("sample_\\d");
	
		Field[] fields = R.drawable.class.getDeclaredFields();
		
		for(Field field:fields) {
			if(p1.matcher(field.getName()).matches()) {
				try {
					//保存图片ID
					imgs1.add( field.getInt(R.drawable.class));
				} catch (Exception e) {
					e.printStackTrace();
				}
				//保存图片ID
			} else if(p2.matcher(field.getName()).matches()) {
				try {
					imgs2.add( field.getInt(R.drawable.class));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public ImageAdapter(Context context) {
		super();
		this.context = context;
	}
	
	@Override
	public int getCount() {
		//小图片
		return imgs1.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

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

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView image = new ImageView(context);
		//小图片
		image.setImageResource(imgs1.get(position));
		image.setAdjustViewBounds(true);
		image.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		image.setScaleType(ImageView.ScaleType.FIT_CENTER);
		return image;
	}

}

package com.ethan.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

/**
 * viewFactory ---->makeView()
 * OnItemSelectedListener------->onItemSelected()
 * @author ETHAN
 *
 */
public class MyActivity extends Activity implements ViewFactory, OnItemSelectedListener {
	private ImageSwitcher is;
	private Gallery g;
	
	//选中的索引
	private int selected = 0;
	//手势向上向下 落点 x 坐标
    private int upX,downX;
    
	@Override
	protected 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);
		
		//得到ImageSwitcher对象
		is = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
		//实现并设置工厂内部接口的makeView方法,用来显示视图。
		is.setFactory(MyActivity.this);
		
		is.setOnTouchListener(touchListener);
		//设置图片来源
		g = (Gallery) findViewById(R.id.gallery);
		g.setAdapter(new ImageAdapter(MyActivity.this));
		g.setOnItemSelectedListener(this);
		
		//设置切入动画
		is.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_in));
		//设置切出动画
		is.setAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_out));
		
		
		
	}

	OnTouchListener touchListener = new OnTouchListener() {
		
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			
			if(event.getAction()==MotionEvent.ACTION_DOWN) {
				//取得 按下时的 x坐标
				downX = (int) event.getX();
				//True if the listener has consumed the event, false otherwise.
				//我的理解是:如果event销毁了,结束了一个时间,就返回true;下一个event is new
				return true;
			} else if(event.getAction()==MotionEvent.ACTION_UP) {
				//取得 松开时的 x坐标
				upX = (int)event.getX();
				//手指 向右划, 看前一张
				if(upX-downX>100) {
					//如果是第一张,则给出 最后一张照片(前一张)
					if(g.getSelectedItemPosition()==0) {
						selected = g.getCount()-1;
					} else {
						selected = g.getSelectedItemPosition() - 1; // 上一张
					}
				} else if(downX - upX>100) {
					//手指 向左划, 看下一张
					
					if(g.getSelectedItemPosition()==g.getCount()-1) {
						selected = 0;
					} else {
						selected = g.getSelectedItemPosition() + 1;// 下一张
					}
				}
				
				//int position, boolean animate
				g.setSelection(selected,true);
				return true;
			}
			return false;
		}
	};
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		is.setImageResource(ImageAdapter.imgs2.get(position));
		selected = position;
	}

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

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值