Android 高仿微信朋友圈动态支持双击手势放大并滑动查看图片效果

Android 高仿微信朋友圈动态支持双击手势放大并滑动查看图片效果,第1张

概述最近参与了开发一款旅行APP,其中包含实时聊天和动态评论功能,终于耗时几个月几个伙伴完成了,今天就小结一下至于实时聊天功能如果用户不多的情况可以scoket实现,如果用户万级就可以采用开源的smack+opnefile实现,

最近参与了开发一款旅行APP,其中包含实时聊天和动态评论功能,终于耗时几个月几个伙伴完成了,今天就小结一下至于实时聊天功能如果用户不多的情况可以scoket实现,如果用户万级就可以采用开源的smack + opnefile实现,也可以用mina开源+XMMP,至于怎么搭建和实现,估计目前github上一搜一大把,至于即时通讯怕误人子弟,暂且不做介绍,现就把实现的一个微信朋友圈的小功能介绍一下。

   先上效果图:

一拿到主流的UI需求,大致分析下,需要我ListVIEw嵌套GrIDvIEw,而grIDVIEw的行数也和图片总数有关系,因此通过个数我们可以动态设置条目的宽高,而点击图片放大我们可一跳转到另一界面,图片左右滑动可以用vIEwpager实现,双击图片放大和手指缩放图片也可以用就监听手势进行不断放大,对于安卓事件不熟悉的朋友可以直接使用一个著名的photoVIEw开源项目,支持手势缩放图片和滑动图片实现画廊功能,也很好的解决了内存溢出问题。

      一 配置ImageLoader       

       本项目中加载网络图片我就直接使用imageLoader,但建议还是去看下源码,因为开源项目本身自带缓存机制,有很好的缓存技巧,有很多东西值得我们借鉴。其不仅可以加载本地图片(文件path),也支持加载网络图片(url),并且自带防止内存溢出功能。

public class MyApplication extends Application {   @OverrIDe   public voID onCreate() {     super.onCreate();     displayImageOptions defaultoptions = new displayImageOptions         .Builder()         .showImageForEmptyUri(R.drawable.empty_photo)          .showImageOnFail(R.drawable.empty_photo)          .cacheInMemory(true)         .cacheOndisc(true)         .build();     ImageLoaderConfiguration config = new ImageLoaderConfiguration         .Builder(getApplicationContext())         .defaultdisplayImageOptions(defaultoptions)         .discCacheSize(50 * 1024 * 1024)//         .discCachefileCount(100)//缓存一百张图片         .writeDeBUGLogs()         .build();     ImageLoader.getInstance().init(config);   } } 

 二 准备主界面和需要的基础类

  1  listadapter

public class FrIDlistadapter extends BaseAdapter{   private ArrayList<MyBean> mList;   private LayoutInflater mInflater;   private Context mContext;   public FrIDlistadapter(Context context,ArrayList<MyBean> List) {     mInflater = LayoutInflater.from(context);     mContext=context;     this.mList=List;   }   @OverrIDe   public int getCount() {     return mList==null?0:mList.size();   }   @OverrIDe   public MyBean getItem(int position) {     return mList.get(position);   }   @OverrIDe   public long getItemID(int position) {     return getItem(position).ID;   }   @OverrIDe   public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {     VIEwHolder holder;     if (convertVIEw == null) {       holder = new VIEwHolder();       convertVIEw = mInflater.inflate(R.layout.List_item,null);       holder.avator=(ImageVIEw)convertVIEw.findVIEwByID(R.ID.avator);       holder.name=(TextVIEw)convertVIEw.findVIEwByID(R.ID.name);       holder.content = (TextVIEw) convertVIEw.findVIEwByID(R.ID.content);       holder.grIDVIEw=(NoScrollGrIDVIEw)convertVIEw.findVIEwByID(R.ID.grIDVIEw);       convertVIEw.setTag(holder);     } else {       holder = (VIEwHolder) convertVIEw.getTag();     }     final MyBean bean = getItem(position);     //加载网络图片     ImageLoader.getInstance().displayImage(bean.avator,holder.avator);     holder.name.setText(bean.name);     holder.content.setText(bean.content);     if(bean.urls!=null&&bean.urls.length>0){       holder.grIDVIEw.setVisibility(VIEw.VISIBLE);       holder.grIDVIEw.setAdapter(new DynamicGrIDAdapter(bean.urls,mContext));       holder.grIDVIEw.setonItemClickListener(new AdapterVIEw.OnItemClickListener() {         @OverrIDe         public voID onItemClick(AdapterVIEw<?> parent,VIEw vIEw,int position,long ID) {           imagebrower(position,bean.urls);         }       });     }else{       holder.grIDVIEw.setVisibility(VIEw.GONE);     }     return convertVIEw;   }   private voID imagebrower(int position,String[] urls) {     Intent intent = new Intent(mContext,ImagePagerActivity.class);     // 图片url,为了演示这里使用常量,一般从数据库中或网络中获取     intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS,urls);     intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX,position);     mContext.startActivity(intent);   }   // 优化ListvIEw   private static class VIEwHolder {     public TextVIEw name;     public ImageVIEw avator;     TextVIEw content;     NoScrollGrIDVIEw grIDVIEw;   } }

  2  主界面

   实际项目中数据是数据是从服务器获取的,本次就只将图片从网络获取,

public class MainActivity extends ListActivity {   public static final String TAG = "MainActivity";   private FrIDlistadapter mAdapter;   @OverrIDe   public voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentVIEw(R.layout.activity_main);     new LoderDataTask().execute();   }   class LoderDataTask extends AsyncTask<VoID,VoID,MessageModle> {     @OverrIDe     protected MessageModle doInBackground(VoID... params) {       Gson gson = new Gson();       MessageModle msg = gson.fromJson(getData(),MessageModle.class);       return msg;     }     @OverrIDe     protected voID onPostExecute(MessageModle result) {       mAdapter = new FrIDlistadapter(MainActivity.this,result.List);       setlistadapter(mAdapter);     }   }   private String getData() {     // 模拟网络获取数据     String Json = "{\"code\":200,\"msg\":\"ok\",List:["         + "{\"ID\":110,\"avator\":\"http://img0.bdstatic.com/img/image/shouye/leimu/mingxing.jpg\",\"name\":\"赵薇\",\"content\":\"今天不开心!\",\"urls\":[]},"         + "{\"ID\":111,\"avator\":\"http://image.cnwest.com/attachement/jpg/site1/20110507/001372d8a36f0f2f4c953a.jpg\",\"name\":\"李晨\",\"content\":\"我们\","         + " \"urls\":[\"http://guangdong.sinaimg.cn/2015/0530/U11307P693DT20150530094310.jpg\"]},"         + "{\"ID\":114,\"avator\":\"http://img.hexun.com/2009-05-01/117287830.jpg\",\"name\":\"小马哥\",\"content\":\"今天淘宝了吗\",\"urls\":["         + "\"http://g.hiphotos.bdimg.com/album/s%3D680%3Bq%3D90/sign=ccd33b46d53f8794d7ff4b26e2207fc9/0d338744ebf81a4c0f993437d62a6059242da6a1.jpg\","         + "\"http://f.hiphotos.bdimg.com/album/s%3D680%3Bq%3D90/sign=6b62f61bac6eddc422e7b7f309e0c7c0/6159252dd42a2834510deef55ab5c9ea14cebfa1.jpg\","         + "\"http://g.hiphotos.bdimg.com/album/s%3D680%3Bq%3D90/sign=e58fb67bc8ea15ce45eee301863b4bce/a5c27d1ed21b0ef4fd6140a0dcc451da80cb3e47.jpg\","         + "\"http://c.hiphotos.bdimg.com/album/s%3D680%3Bq%3D90/sign=cdab1512d000baa1be2c44b3772bc82f/91529822720e0cf3855c96050b46f21fbf09aaa1.jpg\"]},"         + "{\"ID\":112,\"avator\":\"http://img3.yxlady.com/yl/Uploadfiles_5361/20150528/20150528050208705.jpg\",\"name\":\"邓超\",\"content\":\"奔跑吧兄弟! 欢迎收看!\",\"urls\":[\"http://upload.cbg.cn/2015/0305/1425518659246.jpg\","         + "\"http://www.people.com.cn/mediafile/pic/20150619/30/4179219540177204330.jpg\"]},"         + "{\"ID\":113,\"avator\":\"http://img4.imgtn.bdimg.com/it/u=945108765,1070109457&fm=21&gp=0.jpg\",\"name\":\"奥巴马\",\"content\":\"holle\",\"urls\":[\"http://f.hiphotos.bdimg.com/album/s%3D680%3Bq%3D90/sign=6b62f61bac6eddc422e7b7f309e0c7c0/6159252dd42a2834510deef55ab5c9ea14cebfa1.jpg\",\"http://g.hiphotos.bdimg.com/album/s%3D680%3Bq%3D90/sign=e58fb67bc8ea15ce45eee301863b4bce/a5c27d1ed21b0ef4fd6140a0dcc451da80cb3e47.jpg\",\"http://c.hiphotos.bdimg.com/album/s%3D680%3Bq%3D90/sign=cdab1512d000baa1be2c44b3772bc82f/91529822720e0cf3855c96050b46f21fbf09aaa1.jpg\"]}]}";     return Json;   } 

  3 GrIDVIEw的Adapter

     因为ListvIEw的条目中包含GrIDvIEw,在这里还需要为它创建atapter

    由于adapter没太多技术含量,因此重点部分列出,在这里我们需要判断下适配的数据眼总数,微信最大数是9张,显示一张的时候,图片比较大,两张的时候稍微减少,四张的时候两列两行和两张的大小一致,其他张数的时候都是三行三列的九宫格。

@OverrIDe   public VIEw getVIEw(int position,VIEwGroup parent) {     MyGrIDVIEwHolder vIEwHolder;     if (convertVIEw == null) {       vIEwHolder = new MyGrIDVIEwHolder();       convertVIEw = mLayoutInflater.inflate(R.layout.grIDvIEw_item,parent,false);       vIEwHolder.imageVIEw = (ImageVIEw) convertVIEw           .findVIEwByID(R.ID.album_image);       convertVIEw.setTag(vIEwHolder);     } else {       vIEwHolder = (MyGrIDVIEwHolder) convertVIEw.getTag();     }     String url = getItem(position);     if (getCount() == 1) {       vIEwHolder.imageVIEw.setLayoutParams(new androID.Widget.AbsListVIEw.LayoutParams(300,250));     }     if (getCount() == 2 ||getCount() == 4) {       vIEwHolder.imageVIEw.setLayoutParams(new androID.Widget.AbsListVIEw.LayoutParams(200,200));     }     ImageLoader.getInstance().displayImage(url,vIEwHolder.imageVIEw);     return convertVIEw;   } 

   4  新建用于支持九宫格自定义的GrIDvIEw

public class NoScrollGrIDVIEw extends GrIDVIEw {   public NoScrollGrIDVIEw(Context context) {     super(context);   }   public NoScrollGrIDVIEw(Context context,AttributeSet attrs) {     super(context,attrs);   }   @OverrIDe   protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) {     int expandSpec = 0;     int size = getAdapter().getCount();     if (size == 1) {       setNumColumns(1);     }      if ( size==2 || size == 4 ) {       setNumColumns(2);     }     else {       setNumColumns(3);     }     expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);     super.onMeasure(wIDthMeasureSpec,expandSpec );   } } 

三 点击图片后的基础类

   1 建立大图查看器vIEwpaer

public class ImagePagerActivity extends FragmentActivity {   private static final String STATE_position = "STATE_position";   public static final String EXTRA_IMAGE_INDEX = "image_index";   public static final String EXTRA_IMAGE_URLS = "image_urls";   private HackyVIEwPager mPager;   private int pagerposition;   private TextVIEw indicator;   @OverrIDe   public voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentVIEw(R.layout.image_detail_pager);     pagerposition = getIntent().getIntExtra(EXTRA_IMAGE_INDEX,0);     String[] urls = getIntent().getStringArrayExtra(EXTRA_IMAGE_URLS);     mPager = (HackyVIEwPager) findVIEwByID(R.ID.pager);     ImagePagerAdapter mAdapter = new ImagePagerAdapter(         getSupportFragmentManager(),urls);     mPager.setAdapter(mAdapter);     indicator = (TextVIEw) findVIEwByID(R.ID.indicator);     CharSequence text = getString(R.string.vIEwpager_indicator,1,mPager         .getAdapter().getCount());     indicator.setText(text);     // 更新下标     mPager.setonPagechangelistener(new OnPagechangelistener() {       @OverrIDe       public voID onPageScrollStateChanged(int arg0) {       }       @OverrIDe       public voID onPageScrolled(int arg0,float arg1,int arg2) {       }       @OverrIDe       public voID onPageSelected(int arg0) {         CharSequence text = getString(R.string.vIEwpager_indicator,arg0 + 1,mPager.getAdapter().getCount());         indicator.setText(text);       }     });     if (savedInstanceState != null) {       pagerposition = savedInstanceState.getInt(STATE_position);     }     mPager.setCurrentItem(pagerposition);   }   @OverrIDe   public voID onSaveInstanceState(Bundle outState) {     outState.putInt(STATE_position,mPager.getCurrentItem());   }   private class ImagePagerAdapter extends FragmentStatePagerAdapter {     public String[] fileList;     public ImagePagerAdapter(FragmentManager fm,String[] fileList) {       super(fm);       this.fileList = fileList;     }     @OverrIDe     public int getCount() {       return fileList == null ? 0 : fileList.length;     }     @OverrIDe     public Fragment getItem(int position) {       String url = fileList[position];       return ImageDetailFragment.newInstance(url);     }   } 

2 查看大图界面

public class ImageDetailFragment extends Fragment {   private String mImageUrl;   private ImageVIEw mImageVIEw;   private Progressbar progressbar;   private PhotoVIEwAttacher mAttacher;   public static ImageDetailFragment newInstance(String imageUrl) {     final ImageDetailFragment f = new ImageDetailFragment();     final Bundle args = new Bundle();     args.putString("url",imageUrl);     f.setArguments(args);     return f;   }   @OverrIDe   public voID onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     mImageUrl = getArguments() != null ? getArguments().getString("url") : null;   }   @OverrIDe   public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) {     final VIEw v = inflater.inflate(R.layout.image_detail_fragment,container,false);     mImageVIEw = (ImageVIEw) v.findVIEwByID(R.ID.image);     mAttacher = new PhotoVIEwAttacher(mImageVIEw);     mAttacher.setonPhotoTapListener(new OnPhotoTapListener() {       @OverrIDe       public voID onPhotoTap(VIEw arg0,float arg2) {         getActivity().finish();       }     });     progressbar = (Progressbar) v.findVIEwByID(R.ID.loading);     return v;   }   @OverrIDe   public voID onActivityCreated(Bundle savedInstanceState) {     super.onActivityCreated(savedInstanceState);     ImageLoader.getInstance().displayImage(mImageUrl,mImageVIEw,new SimpleImageLoadingListener() {       @OverrIDe       public voID onLoadingStarted(String imageUri,VIEw vIEw) {         progressbar.setVisibility(VIEw.VISIBLE);       }       @OverrIDe       public voID onLoadingFailed(String imageUri,FailReason failReason) {         String message = null;         switch (failReason.getType()) {         case IO_ERROR:           message = "下载错误";           break;         case DECoding_ERROR:           message = "图片无法显示";           break;         case NETWORK_DENIED:           message = "网络有问题,无法下载";           break;         case OUT_OF_MEMORY:           message = "图片太大无法显示";           break;         case UNKNowN:           message = "未知的错误";           break;         }         Toast.makeText(getActivity(),message,Toast.LENGTH_SHORT).show();         progressbar.setVisibility(VIEw.GONE);       }       @OverrIDe       public voID onLoadingComplete(String imageUri,Bitmap loadedImage) {         progressbar.setVisibility(VIEw.GONE);         mAttacher.update();       }     });   } 

     四  界面的头像圆形

      圆形头像用主流的circleimagevIEw.jar的框架,但是有兴趣的朋友也可以自定义ImagvIEw采用重写onDrawI()画圆形的方式将bitmap画上去,由于此demo整体功能较复杂,因此使用第三方的东西,ListVIEw条目布局如下:

 <relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"   xmlns:tools="http://schemas.androID.com/tools"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:padding="6dp" >   <de.hdodenhof.circleimagevIEw.circleimageVIEw     androID:ID="@+ID/avator"     androID:layout_wIDth="48dp"     androID:layout_height="48dp"     androID:src="@drawable/empty_photo" />   <TextVIEw     androID:ID="@+ID/name"     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:layout_marginleft="10dp"     androID:layout_toRightOf="@ID/avator"     androID:textcolor="#576B95"     androID:textSize="16sp"     androID:text="name" />   <TextVIEw     androID:ID="@+ID/content"     androID:layout_wIDth="match_parent"     androID:layout_height="wrap_content"     androID:layout_below="@+ID/name"     androID:layout_marginleft="10dp"     androID:textSize="12sp"     androID:layout_toRightOf="@ID/avator"     androID:text="content" />   <com.loveplusplus.demo.image.NoScrollGrIDVIEw     androID:ID="@+ID/grIDVIEw"     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:paddingtop="5dp"     androID:layout_below="@ID/content"     androID:layout_marginleft="10dp"     androID:layout_toRightOf="@ID/avator"     androID:horizontalSpacing="1dp"     androID:numColumns="3"     androID:visibility="gone"     androID:verticalSpacing="1dp" /> </relativeLayout> 

   接下来我们还需要将主流的photoVIEw.jar加入到工程中,

   总结一下实现以上功能我们使用了第三的imagloader,支持手势缩放的PhotoVIEw,圆形图像的circleimageVIEw,熟悉安卓vIEw绘制机制加载过程,事件传递和分发的朋友是不需要第三方开源项目的支持的,但是对于入门不久的同学,学会怎样使用开源框架就可以,但是想要提高开源项目的的核心还是需要了解的,欢迎阅读

  运行效果图:

 

  有兴趣的朋友建议阅读下:

     安卓事件机制(一)和上篇关于VIEw的博文。谢谢交流和分享。

demo源码下载地址:https://github.com/Tamicer/CHatMomentDemo

以上所述是小编给大家介绍的AndroID 高仿微信朋友圈动态支持双击手势放大并滑动查看图片效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

总结

以上是内存溢出为你收集整理的Android 高仿微信朋友圈动态支持双击手势放大并滑动查看图片效果全部内容,希望文章能够帮你解决Android 高仿微信朋友圈动态支持双击手势放大并滑动查看图片效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147204.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存