先看一下效果吧:
下面就来说一下具体怎么实现的:
实现思路
1.首先我们要获取数据源,数据源就是我们的每条说说(包括姓名、标题、图片数组) 2.自定义适配器(ListVIEw嵌套着GrIDVIEw) 3.图片点击浏览图片(Fragment+VIEwPager)具体实现
1.初始化数据源,设置适配器,看一下代码:
public class MyActivity extends Activity { /*图片显示列表*/ private ListVIEw ListVIEw; /*图片URL数组*/ private List<ContentBean> contentBeans; /*说说适配器*/ private MyAdapter adapter; /** * Called when the activity is first created. */ @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); initData(); initVIEws(); } /** * 初始化数据 */ private voID initData(){ contentBeans = new ArrayList<ContentBean>(); ArrayList<String> imgurls1 = new ArrayList<String>(); imgurls1.add("http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8"); ContentBean cb1 = new ContentBean(1,"java","Sun Microsystems",imgurls1); contentBeans.add(cb1); ArrayList<String> imgurls2 = new ArrayList<String>(); imgurls2.add("http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8"); imgurls2.add("http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8"); imgurls2.add("http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8"); ContentBean cb2 = new ContentBean(2,"OC","Stepstone",imgurls2); contentBeans.add(cb2); ArrayList<String> imgurls3 = new ArrayList<String>(); imgurls3.add("http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8"); imgurls3.add("http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8"); imgurls3.add("http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8"); imgurls3.add("http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8"); imgurls3.add("http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8"); imgurls3.add("http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8"); imgurls3.add("http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8"); imgurls3.add("http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8"); imgurls3.add("http://7xojuc.com1.z0.glb.clouddn.com/110H2E40-6.jpg?attname=&e=1448288962&token=KDsCqUAWz3P-YT6In6oPnT56Eh2cig4zgQd12SJ_:AlTjfYD9SBFOdB4jmmZuKXAMOp8"); ContentBean cb3 = new ContentBean(3,"python","GuIDo van Rossum",imgurls3); contentBeans.add(cb3); } private voID initVIEws(){ ListVIEw = (ListVIEw) findVIEwByID(R.ID.lv_my); adapter = new MyAdapter(MyActivity.this,contentBeans); ListVIEw.setAdapter(adapter); }}
这里面的图片是我上传到七牛的网络图片,加载图片是用ImageLoader,下面也有具体的ImageLoader配置。
2.看一下适配器内容
在说说列表适配器中去设置图片的适配器,图片的GrIDVIEw是重写了一个不能滑动的GrIDVIEw,重写一下OnMeasure();
public class MyAdapter extends BaseAdapter { private Context context; private List<ContentBean> data; public MyAdapter(Context context,List<ContentBean> data) { this.context = context; this.data = data; } @OverrIDe public int getCount() { return data.size(); } @OverrIDe public Object getItem(int i) { return data.get(i); } @OverrIDe public long getItemID(int i) { return i; } @OverrIDe public VIEw getVIEw(int i,VIEw vIEw,VIEwGroup vIEwGroup) { VIEwHolder holder; if (vIEw == null) { holder = new VIEwHolder(); vIEw = VIEw.inflate(context,R.layout.item,null); holder.grIDVIEw = (NoScrollGrIDVIEw) vIEw.findVIEwByID(R.ID.grIDvIEw); holder.tvname = (TextVIEw) vIEw.findVIEwByID(R.ID.tv_name); holder.tvTitle = (TextVIEw) vIEw.findVIEwByID(R.ID.tv_Title); vIEw.setTag(holder); } else { holder = (VIEwHolder) vIEw.getTag(); } final ContentBean bean = data.get(i); holder.tvname.setText(bean.getname()); holder.tvTitle.setText(bean.getTitle()); if (data != null && data.size() > 0) { holder.grIDVIEw.setAdapter(new ImageGrIDAdapter(context,bean.getimgurls())); } /** * 图片列表点击事件 */ holder.grIDVIEw.setonItemClickListener(new AdapterVIEw.OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> adapterVIEw,int i,long l) { Intent intent = new Intent(context,ImagePagerActivity.class); intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS,(Serializable) bean.getimgurls()); intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX,i); context.startActivity(intent); } }); return vIEw; } class VIEwHolder { TextVIEw tvname,tvTitle; NoScrollGrIDVIEw grIDVIEw; }}
3.然后就是图片浏览,这个网上也有好多的Demo,也有详细的讲解,直接拽过来用就可以了,下面的图片数量是监听setonPagechangelistener()来改变下面的图片索引值
/** * 图片查看器 */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); ArrayList<String> urls = getIntent().getStringArrayListExtra(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 ArrayList<String> fileList; public ImagePagerAdapter(FragmentManager fm,ArrayList<String> fileList) { super(fm); this.fileList = fileList; } @OverrIDe public int getCount() { return fileList == null ? 0 : fileList.size(); } @OverrIDe public Fragment getItem(int position) { String url = fileList.get(position); return ImageDetailFragment.newInstance(url); } }}
图片Fragment的详细界面,里面有长按点击事件,和图片加载的状态
package com.hankkin.WeiXinLookimgsDemo.activty;import androID.graphics.Bitmap;import androID.os.Bundle;import androID.support.v4.app.Fragment;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ImageVIEw;import androID.Widget.Progressbar;import androID.Widget.Toast;import com.hankkin.WeiXinLookimgsDemo.R;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.assist.FailReason;import com.nostra13.universalimageloader.core.Listener.SimpleImageLoadingListener;import com.others.PhotoVIEwAttacher;/** * 单张图片显示Fragment */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 PhotoVIEwAttacher.OnPhotoTapListener() { @OverrIDe public voID onPhotoTap(VIEw arg0,float arg2) { getActivity().finish(); } }); mAttacher.setonLongClickListener(new VIEw.OnLongClickListener() { @OverrIDe public boolean onLongClick(VIEw vIEw) { Toast.makeText(getActivity().getApplicationContext(),"保存",Toast.LENGTH_SHORT).show(); return false; } }); 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(); } }); }}
忘了ImageLoader的初始化工作了,给大家加上吧,我写到Application里了。
private MyApplication context; @OverrIDe public voID onCreate() { super.onCreate(); context = this; initimageLoader(context); } /** * 初始化Imageloader * by Hankkin at:2015-11-22 23:20:29 * @param context */ public static voID initimageLoader(Context context){ displayImageOptions options = new displayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_launcher) .showImageOnFail(R.drawable.ic_launcher) .resetVIEwBeforeLoading(false) // default .delayBeforeLoading(0) .cacheInMemory(true) // default .cacheOndisk(true) // default .consIDerExifParams(true) // default .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default .bitmapConfig(Bitmap.Config.ARGB_8888) // default .displayer(new SimpleBitmapdisplayer()) // default .handler(new Handler()) // default .build(); file picPath = new file(Environment.getExternalStorageDirectory().getPath() + file.separator + "weixinlookimgdemo" + file.separator + "files"); ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context) .memoryCacheExtraOptions(480,800) // default = device screen dimensions .diskCacheExtraOptions(480,800,null) .threadPoolSize(3) // default .threadPriority(Thread.norM_PRIORITY - 1) // default .tasksProcessingOrder(QueueProcessingType.FIFO) // default .denyCacheImageMultipleSizesInMemory() .memoryCache(new LruMemoryCache(2 * 1024 * 1024)) .memoryCacheSize(2 * 1024 * 1024) .memoryCacheSizePercentage(13) // default .diskCache(new UnlimiteddiscCache(picPath)) // default .diskCacheSize(50 * 1024 * 1024) .diskCachefileCount(1000) .diskCachefilenameGenerator(new HashCodefilenameGenerator()) // default .imageDownloader(new BaseImageDownloader(context)) // default .imagedecoder(new Baseimagedecoder(true)) // default .defaultdisplayImageOptions(options) // default .writeDeBUGLogs() .build(); // Initialize ImageLoader with configuration. ImageLoader.getInstance().init(config); }
以上就是关于AndroID仿微信发朋友圈浏览图片效果的全部内容,希望对大家学习AndroID软件编程有所帮助。
总结以上是内存溢出为你收集整理的Android仿微信发朋友圈浏览图片效果全部内容,希望文章能够帮你解决Android仿微信发朋友圈浏览图片效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)