<?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;
}
}