MainActivity.java
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView.ScaleType;
public class MainActivity extends Activity {
private ArrayList<Drawable> imgList = new ArrayList<Drawable>();
private Gallery gallery = null;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InitImgList();
gallery = (Gallery) findViewById(R.id.banner_gallery);
gallery.setAdapter(new ImageAdapter(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void InitImgList() {
// 加载图片数据(本demo仅获取本地资源,实际应用中,可异步加载网络数据)
imgList.add(this.getResources().getDrawable(R.drawable.img1));
imgList.add(this.getResources().getDrawable(R.drawable.img2));
imgList.add(this.getResources().getDrawable(R.drawable.img3));
imgList.add(this.getResources().getDrawable(R.drawable.img4));
imgList.add(this.getResources().getDrawable(R.drawable.img5));
imgList.add(this.getResources().getDrawable(R.drawable.img6));
}
public class ImageAdapter extends BaseAdapter {
private Context _context;
public ImageAdapter(Context context) {
_context = context;
}
public int getCount() {
return imgList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
@SuppressWarnings("deprecation")
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(_context);
imageView.setImageDrawable(imgList.get(position));
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return imageView;
}
}
}
MyGallery.java
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Gallery;
@SuppressWarnings("deprecation")
public class MyGallery extends Gallery {
private final int THRESHOLD = 600;
public MyGallery(Context paramContext) {
super(paramContext);
}
public MyGallery(Context paramContext, AttributeSet paramAttributeSet) {
super(paramContext, paramAttributeSet);
}
public MyGallery(Context paramContext, AttributeSet paramAttributeSet,
int paramInt) {
super(paramContext, paramAttributeSet, paramInt);
}
private boolean isScrollingLeft(MotionEvent paramMotionEvent1,
MotionEvent paramMotionEvent2) {
float f2 = paramMotionEvent2.getX();
float f1 = paramMotionEvent1.getX();
if (f2 > f1)
return true;
return false;
}
public boolean onFling(MotionEvent paramMotionEvent1,
MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) {
float f;
if (isScrollingLeft(paramMotionEvent1, paramMotionEvent2)) {
f = THRESHOLD;
} else {
f = THRESHOLD * -1;
}
return super.onFling(paramMotionEvent1, paramMotionEvent2, f,
paramFloat2);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.example.android_1500_gallery.MyGallery
android:id="@+id/banner_gallery"
android:layout_width="fill_parent"
android:layout_height="257px"
android:background="#000"
android:fadingEdgeLength="0.0dp"
android:spacing="1.0px" />
</LinearLayout>