Android系統自帶一個GridView和Gallery兩個控件,GridView網格顯示,Gallery單個瀏覽,兩者結合起來可以真正實現Gallery瀏覽圖片效果。
?
本示例通過GridView和Gallery兩個控件,模仿實現一個完整的仿Gallery圖像集的圖片瀏覽效果。效果圖如下:
?
1、GridView
首先,自定義一個GridImageAdapter圖片適配器,用于填充GridView控件的圖片
?
[java] view plaincopyprint? public?class?GridImageAdapter?extends?BaseAdapter?{??????private?Context?mContext;??????Drawable?btnDrawable;????????public?GridImageAdapter(Context?context)?{??????????mContext?=?context;??????????Resources?resources?=?context.getResources();??????????btnDrawable?=?resources.getDrawable(R.drawable.bg);??????}????????@Override??????public?int?getCount()?{??????????return?ImageSource.mThumbIds.length;??????}????????@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)?{??????????ImageViewExt?imageView;??????????int?space;????????????if?(convertView?==?null)?{??????????????imageView?=?new?ImageViewExt(mContext);??????????????if?(imageCol?==?5)?{??????????????????space?=?dm.heightPixels?/?imageCol?-?6;??????????????????imageView.setLayoutParams(new?GridView.LayoutParams(space,?space));??????????????}?else?{??????????????????space?=?dm.widthPixels?/?imageCol?-?6;??????????????????imageView.setLayoutParams(new?GridView.LayoutParams(?space,?space));??????????????}??????????????imageView.setAdjustViewBounds(true);??????????????imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);?????????????????????imageView.setPadding(3,?3,?3,?3);??????????}?else?{??????????????imageView?=?(ImageViewExt)?convertView;??????????}??????????imageView.setImageResource(ImageSource.mThumbIds[position]);????????????return?imageView;??????}??}?? 然后,用GridImageAdapter填充GridView
?
[java] view plaincopyprint? gridView?=?(GridView)?findViewById(R.id.myGrid);??gridImageAdapter?=?new?GridImageAdapter(this);??gridView.setAdapter(gridImageAdapter);??gridView.setOnItemClickListener(listener);??? 最后,設置GridView控件的點擊監聽事件
?
[java] view plaincopyprint? AdapterView.OnItemClickListener?listener?=?new?AdapterView.OnItemClickListener()?{??????@Override??????public?void?onItemClick(AdapterView<?>?arg0,?View?arg1,?int?position,?long?id)?{??????????Intent?intent?=?new?Intent();??????????intent.setClass(GridViewActivity.this,?GalleryActivity.class);??????????intent.putExtra("position",?position);??????????startActivity(intent);??????}??};??
2、Gallery
完成了GridView的圖片顯示、監聽事件后,現在點擊圖片,會啟動一個Activity來顯示當前點擊的圖片,此時顯示圖片的控件便是Gallery
首先,同GridView一樣,自定義一個ImageAdapter圖片適配器,用來填充Gallery
[java] view plaincopyprint? public?class?ImageAdapter?extends?BaseAdapter?{??????private?Context?mContext;???????private?int?mPos;????????public?ImageAdapter(Context?context)?{??????????mContext?=?context;??????}????????public?void?setOwnposition(int?ownposition)?{??????????this.mPos?=?ownposition;??????}????????public?int?getOwnposition()?{??????????return?mPos;??????}????????@Override??????public?int?getCount()?{??????????return?ImageSource.mThumbIds.length;??????}????????@Override??????public?Object?getItem(int?position)?{???????????mPos=position;??????????return?position;??????}????????@Override??????public?long?getItemId(int?position)?{??????????mPos=position;???????????return?position;??????}????????@Override??????public?View?getView(int?position,?View?convertView,?ViewGroup?parent)?{??????????mPos=position;??????????ImageView?imageview?=?new?ImageView(mContext);??????????imageview.setBackgroundColor(0xFF000000);??????????imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);??????????imageview.setLayoutParams(new?myGallery.LayoutParams(LayoutParams.MATCH_PARENT,?LayoutParams.MATCH_PARENT));??????????imageview.setImageResource(ImageSource.mThumbIds[position]);????????????????????return?imageview;??????}??}??
然后,用ImageAdapter填充Gallery
[java] view plaincopyprint? myGallery??galllery?=?(myGallery)?findViewById(R.id.mygallery);??Intent?intent?=?getIntent();??position?=?intent.getIntExtra("position",?0);?????ImageAdapter?imgAdapter=new?ImageAdapter(this);???????galllery.setAdapter(imgAdapter);??????????galllery.setSelection(position);????????????????????Animation?an=?AnimationUtils.loadAnimation(this,R.anim.scale?);???????????galllery.setAnimation(an);??? ?
此時,如果細心可以注意到,我們的Gallery也是自己定義的myGallery,具體定義如下:
[java] view plaincopyprint? public?class?myGallery?extends?Gallery?{??????boolean?isFirst?=?false;??????boolean?isLast?=?false;????????public?myGallery(Context?context)?{??????????super(context);??????}????????public?myGallery(Context?context,?AttributeSet?paramAttributeSet)?{??????????super(context,?paramAttributeSet);??????}??????????????private?boolean?isScrollingLeft(MotionEvent?e1,?MotionEvent?e2)?{?????????????return?e2.getX()?>?e1.getX();??????}????????@Override??????public?boolean?onFling(MotionEvent?e1,?MotionEvent?e2,?float?distanceX,?float?distanceY)?{??????????ImageAdapter?ia?=?(ImageAdapter)?this.getAdapter();??????????int?p?=?ia.getOwnposition();??????????????int?count?=?ia.getCount();????????????????int?kEvent;??????????if?(isScrollingLeft(e1,?e2))?{??????????????if?(p?==?0?&&?isFirst)?{??????????????????Toast.makeText(this.getContext(),?"已是第一頁",?Toast.LENGTH_SHORT).show();??????????????}?else?if?(p?==?0)?{??????????????????isFirst?=?true;??????????????}?else?{??????????????????isLast?=?false;??????????????}????????????????kEvent?=?KeyEvent.KEYCODE_DPAD_LEFT;??????????}?else?{??????????????if?(p?==?count?-?1?&&?isLast)?{??????????????????Toast.makeText(this.getContext(),?"已到最后一頁",?Toast.LENGTH_SHORT).show();??????????????}?else?if?(p?==?count?-?1)?{??????????????????isLast?=?true;??????????????}?else?{??????????????????isFirst?=?false;??????????????}????????????????kEvent?=?KeyEvent.KEYCODE_DPAD_RIGHT;??????????}??????????onKeyDown(kEvent,?null);??????????return?true;??????}??}??
GalleryActivity的布局文件gallery.xml
?
[html] view plaincopyprint? <?xml?version="1.0"?encoding="utf-8"?>??<LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??????android:layout_width="match_parent"??????android:layout_height="match_parent"??????android:gravity="center"??????android:orientation="horizontal"??????android:padding="10dip"?>??????<RelativeLayout??????????android:layout_width="wrap_content"??????????android:layout_height="wrap_content"??????????android:background="#000000"??????????android:padding="2dip"?>??????????<com.homer.gridgallery.myGallery??????????????android:id="@+id/mygallery"??????????????android:layout_width="fill_parent"??????????????android:layout_height="fill_parent"??????????????android:spacing="16dp"?/>??????</RelativeLayout>??</LinearLayout>?
轉載于:https://www.cnblogs.com/Free-Thinker/p/3585801.html
總結
以上是生活随笔為你收集整理的Android 滑动效果基础篇(四)—— Gallery + GridView的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。