@Gallery配合dot使用时,如果放在RelativeLayout中,则手动滑动有反弹现象,其他layout没问题,现在还没弄清原因。
首先继承Gallery重写OnFling函数,去除gallery的滚动惯性
public class MyGallery extends Gallery {
public MyGallery(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
return e2.getX() > e1.getX();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
int keyCode;
if (isScrollingLeft(e1, e2)) {
keyCode = KeyEvent.KEYCODE_DPAD_LEFT;
} else {
keyCode = KeyEvent.KEYCODE_DPAD_RIGHT;
}
onKeyDown(keyCode, null);
return true;
}
}
@注OnFling直接返回false也能实现类似效果,但那样需要滑动很大距离,图片才会切换,用户体验不好
第二步,构造adapter
要想平滑的实现循环滚动,可以让getCount返回一个很大的值,这样gallery就认为是有多个item,item之间的切换动画是平滑的
public class GalleryAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context mContext;
private int width;
private int count;
private int[] mImageIds;
public GalleryAdapter(Context context, int[] ids) {
mContext = context;
mImageIds = ids;
mInflater = LayoutInflater.from(mContext);
DisplayMetrics dm = mContext.getApplicationContext().getResources()
.getDisplayMetrics();
width = dm.widthPixels;
count = mImageIds.length;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;//用于循环滚动
}
@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) {
position = position % count;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.gallery_item, null);
}
ImageView v = (ImageView) convertView.findViewById(R.id.img);
v.setLayoutParams(new Gallery.LayoutParams(width, 200));
v.setScaleType(ImageView.ScaleType.FIT_XY);
v.setBackgroundResource(mImageIds[position]);
return v;
}
}
第三,实现自动滚动
由于我们还要手动滚动,所以自动滚动用单独一个进程来实现
private void startAutoScroll() {
new Thread() {
@Override
public void run() {
int count = 0;
while (mAutoScroll) {
count = 0;
while (count < 30) {
count++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (mOnTouch) {// 用戶手动滑动时,停止自动滚动
count = 0;
}
}
mPosition++;
Message msg = mHandler.obtainMessage(SCROLL, mPosition, 0);
mHandler.sendMessage(msg);
}
}
}.start();
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SCROLL:
mGallery.setSelection(msg.arg1);
break;
}
}
};
第四实现手动滚动
手动滚动时,要停止自动滚动,监听gallery的onTouch事件,DOWN时mOnTouch置为true,UP时mOnTouch置为false即可
mGallery.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
mOnTouch = true;
} else if (action == MotionEvent.ACTION_UP) {
mOnTouch = false;
}
return false;
}
});
到现在我们已经可以自动滚动,手动滚动时自动滚动也会停止。
我们也许还需要加上dot提示图片滚动的位置
LinearLayout layout = (LinearLayout) findViewById(R.id.dot);
if (mDots == null) {
mDots = new ImageView[ids.length];
for (int i = 0; i < ids.length; i++) {
if (mDots[i] == null)
mDots[i] = new ImageView(this);
mDots[i].setBackgroundResource(R.drawable.banner_tab_unselected);
layout.addView(mDots[i], new LinearLayout.LayoutParams(mWidth
/ ids.length + 1, LayoutParams.WRAP_CONTENT));
}
mDots[0].setBackgroundResource(R.drawable.banner_tab_selected);
}
mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long arg3) {
mDotPosition = position % ids.length;
mDots[mDotPosition]
.setBackgroundResource(R.drawable.banner_tab_selected);
if (mDotPosition != mPreDotPosition)
mDots[mPreDotPosition]
.setBackgroundResource(R.drawable.banner_tab_unselected);
mPreDotPosition = mDotPosition;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});