读书人

Gallery自动循环滚动手动滚动的平滑

发布时间: 2012-11-15 15:16:14 作者: rapoo

Gallery自动循环滚动,手动滚动的平滑切换及存在问题

来自:http://blog.csdn.net/lenghun00/article/details/7635374

?

?

@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();}@Overridepublic 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;}@Overridepublic int getCount() {return Integer.MAX_VALUE;//用于循环滚动}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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);}

?

[java] view plaincopyprint?
  1. mGallery.setOnItemSelectedListener(new?OnItemSelectedListener()?{????
  2. ????????????@Override??????????????public?void?onItemSelected(AdapterView<?>?arg0,?View?view,??
  3. ????????????????????int?position,?long?arg3)?{??????????????????mDotPosition?=?position?%?ids.length;??
  4. ????????????????mDots[mDotPosition]??????????????????????????.setBackgroundResource(R.drawable.banner_tab_selected);??
  5. ????????????????if?(mDotPosition?!=?mPreDotPosition)??????????????????????mDots[mPreDotPosition]??
  6. ????????????????????????????.setBackgroundResource(R.drawable.banner_tab_unselected);??????????????????mPreDotPosition?=?mDotPosition;??
  7. ????????????}????
  8. ????????????@Override??????????????public?void?onNothingSelected(AdapterView<?>?arg0)?{??
  9. ??????????????????????????????????????????????}??
  10. ??????????????????????});??

读书人网 >移动开发

热点推荐