安卓类似于微信朋友圈功能,集合了Gson,PhotoView,android-universal-image-loader这些用法

安卓类似于微信朋友圈功能,集合了Gson,PhotoView,android-universal-image-loader这些用法。

最近刚学了这些,就用这些做了个小例子,大家可以去下载源码,里面还是有很多知识是可以学到的。PhotoView还是网上那个开源PhotoView,不过我打成jar包了,Gson的话可以看我上一篇文章,里面有一些常用的用法,imageLoader的话,大家可以去 http://blog.csdn.net/vipzjyno1/article/details/23206387 这里看,很详细。接下来就讲里面用到的一些方法。

忘了要点,要先看图才有感觉!

一.Gson模仿后台数据

private void initData() {
		// 这里只做5份数据,此时数据由服务器返回的json
		String jsonData = "[{\"name\":\"小明\",\"content\":\"今天天气不错啊\",\"headUrl\":\"http://img4.duitang.com/uploads/item/201411/01/20141101172619_5sz2Y.jpeg\",\"imageUrl\":[]},"
				+ "{\"name\":\"小红\",\"content\":\"是啊,真的很不错呢\",\"headUrl\":\"http://img4q.duitang.com/uploads/item/201411/01/20141101171342_xHRH2.jpeg\",\"imageUrl\":[\"http://g.hiphotos.baidu.com/image/h%3D200/sign\u003d55c721bb29dda3cc14e4bf2031e83905/32fa828ba61ea8d3e3eecc20900a304e241f58c1.jpg\"]},"
				+ "{\"name\":\"小胖\",\"content\":\"快出来晒太阳啊啊啊啊啊a \",\"headUrl\":\"http://img5q.duitang.com/uploads/item/201505/15/20150515140254_3tZSc.jpeg\",\"imageUrl\":[\"http://g.hiphotos.baidu.com/image/h%3D200/sign\u003d55c721bb29dda3cc14e4bf2031e83905/32fa828ba61ea8d3e3eecc20900a304e241f58c1.jpg\",\"http://pic.nipic.com/2008-07-05/20087584237954_2.jpg\"]},"
				+ "{\"name\":\"小妞\",\"content\":\"不晒太阳哪行啊\",\"headUrl\":\"http://p2.gexing.com/G1/M00/CD/EA/rBABFFHgw7Si8OQhAAAeKVpPAio937_200x200_3.jpg\",\"imageUrl\":[\"http://pic4.nipic.com/20090903/2125404_132352014851_2.jpg\",\"http://img.kutoo8.com//upload/image/78018037/201305280911_960x540.jpg\",\"http://www.xiaoxiongbizhi.com/wallpapers/__85/2/f/2fg40v2zs.jpg\"]},"
				+ "{\"name\":\"帅哥\",\"content\":\"晒完赶紧来盘Lol , 五黑啦~~\",\"headUrl\":\"http://v1.qzone.cc/avatar/201405/31/17/00/53899a499103b640.jpg%21200x200.jpg\",\"imageUrl\":[\"http://www.xiaoxiongbizhi.com/wallpapers/__85/1/9/19r0an0jm.jpg\",\"http://i2.sinaimg.cn/gm/2014/1231/U8776P115DT20141231160820.jpg\",\"http://img.kejixun.com/2014/0128/20140128091127259.jpg\",\"http://img4q.duitang.com/uploads/item/201411/01/20141101171342_xHRH2.jpeg\",\"http://img.kejixun.com/2013/0608/20130608095102642.jpg\"]}]";
		lists = new ArrayList<Person>();
		Type type = new TypeToken<List<Person>>(){}.getType();
		Gson gson = new Gson();
		lists = gson.fromJson(jsonData, type);
		
		adapter = new ListViewAdapter(lists , this);
		listView.setAdapter(adapter);
	}
Gson这里的话是模仿后台服务器给过来的JSON数据,然后解析成相应的对象,是挺长的。这个string是我先把对象转为json才弄出来的,要自己写json的话你懂的!这里也没什么好说,不懂Gson用法可以参考前篇文章。哦对了,字符串中\" 才表示一个双引号的,所以里面要自己补充\

二.ListView与GridView冲突的问题

可以看到,ListView里面的Item带有GridView,必然会引起滑动冲突,导致GridView的Item只能显示一部分而已,网上也有很多关于ListView , ScrollView , GridView 之间的嵌套问题,大有多这样的共同方法,就是重写嵌套的那个view 的 onMeasure 方法,好让它getView的时候知道它的高度

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, expandSpec);
	}
关于滑动冲突的问题大家看去这里看看  http://bbs.anzhuo.cn/thread-982250-1-1.html

三.图片缓存android-universal-image-loader

在ImageLoader的配置参数中,我们来看看

	// 显示的图片的各种格式DisplayImageOptions 的设置
		DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_stub_gray) // 设置图片在下载期间显示的图片
				.showImageForEmptyUri(R.drawable.ic_error) // 设置图片Uri为空或是错误的时候显示的图片
				.showImageOnFail(R.drawable.ic_error) // 设置图片加载/解码过程中错误时候显示的图片
				.cacheInMemory(true) // 设置下载的图片是否缓存在内存中
				.cacheOnDisc(true) // 设置下载的图片是否缓存在SD卡中
				.considerExifParams(true) // 是否考虑JPEG图像EXIF参数(旋转,翻转)
				.bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型
				.build();
		//自定义缓存目录
		File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "imageloader/Cache");

		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
				.threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory()
				.discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.discCache(new UnlimitedDiscCache(cacheDir)) // 缓存目录
				.defaultDisplayImageOptions(options).discCacheSize(50 * 1024 * 1024)//
				.discCacheFileCount(100)// 缓存文件数
				.writeDebugLogs().build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);


里面我开了图片缓存,缓存目录我自定义在imageLoader中了。这些配置代码通常是在Application中的,然后配置到AndroidManifest.xml中

具体用法可到  http://blog.csdn.net/vipzjyno1/article/details/23206387 这里去看下

四.关于图片界面上的指示器,如显示图片数目那里 , 2/5 。

在xml中是用<TextView @string/tv_indicator  ,在string 中我们的tv_indicator 是

	<string name="tv_indicator">%1$d/%2$d</string>
比如“我今年23岁了”,这个23是整型的。在string.xml中可以这样写,<string name="old">我今年%1$d岁了</string>。 %1$d表达的意思是整个name=”old”中,第一个整型的替代。如果一个name中有两个需要替换的整型内容,则第二个写为:%2$d,以此类推;当然%1$d是整型的,还有%1$s, %1$f ,分别表示字符串和浮点数。

定义好后在代码中代用getString方法

	String textString = getString(R.string.tv_indicator, num+1 , lists.size());//后面两个参数对应%1$d和%2$d以此类推
	tvIndicator.setText(textString);

源码下载 http://download.csdn.net/detail/hohohong/9452118




 





  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值