日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 滑动效果基础篇(四)—— Gallery + GridView

發(fā)布時(shí)間:2024/4/14 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 滑动效果基础篇(四)—— Gallery + GridView 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Android系統(tǒng)自帶一個GridView和Gallery兩個控件,GridView網(wǎng)格顯示,Gallery單個瀏覽,兩者結(jié)合起來可以真正實(shí)現(xiàn)Gallery瀏覽圖片效果。


?

本示例通過GridView和Gallery兩個控件,模仿實(shí)現(xiàn)一個完整的仿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);?//?設(shè)置點(diǎn)擊監(jiān)聽事件??
  • 最后,設(shè)置GridView控件的點(diǎn)擊監(jiān)聽事件

    ?

    [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的圖片顯示、監(jiān)聽事件后,現(xiàn)在點(diǎn)擊圖片,會啟動一個Activity來顯示當(dāng)前點(diǎn)擊的圖片,此時(shí)顯示圖片的控件便是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);???//?獲取GridViewActivity傳來的圖片位置position???
  • ImageAdapter?imgAdapter=new?ImageAdapter(this);???????
  • galllery.setAdapter(imgAdapter);????????//?設(shè)置圖片ImageAdapter??
  • galllery.setSelection(position);????????//?設(shè)置當(dāng)前顯示圖片??
  • ??????
  • ????Animation?an=?AnimationUtils.loadAnimation(this,R.anim.scale?);?????//?Gallery動畫??
  • ????galllery.setAnimation(an);???
  • ?

    此時(shí),如果細(xì)心可以注意到,我們的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);??
  • ????}??
  • ??
  • ????/**?是否向左滑動(true?-?向左滑動;?false?-?向右滑動)?*/??
  • ????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();????//?獲取當(dāng)前圖片的position??
  • ????????int?count?=?ia.getCount();??????//?獲取全部圖片的總數(shù)count??
  • ????????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>?
  • 轉(zhuǎn)載于:https://www.cnblogs.com/Free-Thinker/p/3585801.html

    總結(jié)

    以上是生活随笔為你收集整理的Android 滑动效果基础篇(四)—— Gallery + GridView的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。