本文实例为大家分享了android仿微信发表说说、心情功能,供大家参考,具体内容如下
既能实现拍照,选图库,多图案上传的案例,目前好多app都有类似微信朋友圈的功能,能过发表说说等附带图片上传。下面的就是实现该功能的过程:大家还没有看过android retrofit 2.0框架上传图片解决方案这篇文章,在看今天的就很容易,接在本项目中用到了一个library:photopicker,封装了图片的选择功能,是否选相机,还有选中图片后可以查看图片的功能。
一、 首先:将photopicker到工程中
(1)、先简单讲解一下photopickerintent的用法:
1
2
3
4
5
6
|
photopickerintent intent = new photopickerintent(mainactivity. this ); intent.setselectmodel(selectmodel.multi); // intent.setshowcarema( true ); // 是否显示拍照 intent.setmaxtotal( 6 ); // 最多选择照片数量,默认为6 intent.setselectedpaths(imagepaths); // 已选中的照片地址, 用于回显选中状态 startactivityforresult(intent, request_camera_code); |
(2)、设置好之后,重写onactivityresult方法处理选中图片和预览加载适配器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@override protected void onactivityresult( int requestcode, int resultcode, intent data) { super .onactivityresult(requestcode, resultcode, data); if (resultcode == result_ok) { switch (requestcode) { // 选择照片 case request_camera_code: loadadpater(data.getstringarraylistextra(photopickeractivity.extra_result)); break ; // 预览 case request_preview_code: loadadpater(data.getstringarraylistextra(photopreviewactivity.extra_result)); break ; } } } |
二、重点在gridadapter
(1)、定义一个变量 mmaxposition,
(2)、写一个获取最大位置的方法:用来获取item的最大位置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//重点在这里,当position==mmaxposition-1让"+"选择图片的默认图片显示。 //当目前最多6中图片,则最大7时,图片影藏。 if (position==mmaxposition- 1 ) { holder.image.setimageresource(r.mipmap.ic_launcher); holder.image.setvisibility(view.visible); if (position== 6 &&mmaxposition== 7 ){ holder.image.setimageresource(r.mipmap.ic_launcher); holder.image.setvisibility(view.gone); } } else { final string path=listurls.get(position); glide.with(mainactivity. this ) .load( new file(path)) .placeholder(r.mipmap.default_error) .error(r.mipmap.default_error) .centercrop() .crossfade() .into(holder.image); } |
(3)、关于图片的显示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
private class gridadapter extends baseadapter{ private arraylist<string> listurls; private int mmaxposition; private layoutinflater inflater; public gridadapter(arraylist<string> listurls) { this .listurls = listurls; inflater = layoutinflater.from(mainactivity. this ); } public int getcount() { mmaxposition = listurls.size()+ 1 ; return mmaxposition; } public int getmaxposition(){ return mmaxposition; } @override public string getitem( int position) { return listurls.get(position); } @override public long getitemid( int position) { return position; } @override public view getview( int position, view convertview, viewgroup parent) { viewholder holder = null ; if (convertview == null ) { holder = new viewholder(); convertview = inflater.inflate(r.layout.item_image, parent, false ); holder.image = (imageview) convertview.findviewbyid(r.id.imageview); convertview.settag(holder); } else { holder = (viewholder)convertview.gettag(); } log.d( "" , "position:" +position+ " mmaxposition:" +mmaxposition); //重点在这里,当position==mmaxposition-1让"+"选择图片的默认图片显示。 //当目前最多6中图片,则最大7时,图片影藏。 if (position==mmaxposition- 1 ) { // holder.image.settag("default"); holder.image.setimageresource(r.mipmap.ic_launcher); holder.image.setvisibility(view.visible); if (position== 6 &&mmaxposition== 7 ){ holder.image.setimageresource(r.mipmap.ic_launcher); holder.image.setvisibility(view.gone); } } else { final string path=listurls.get(position); glide.with(mainactivity. this ) .load( new file(path)) .placeholder(r.mipmap.default_error) .error(r.mipmap.default_error) .centercrop() .crossfade() .into(holder.image); } |
三、gridview中item的点击事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
gridview.setonitemclicklistener( new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { if (position == gridadapter.getmaxposition() - 1 ) { photopickerintent intent = new photopickerintent(mainactivity. this ); intent.setselectmodel(selectmodel.multi); intent.setshowcarema( true ); // 是否显示拍照 intent.setmaxtotal( 6 ); // 最多选择照片数量,默认为9 intent.setselectedpaths(imagepaths); // 已选中的照片地址, 用于回显选中状态 startactivityforresult(intent, request_camera_code); } else { photopreviewintent intent = new photopreviewintent(mainactivity. this ); intent.setcurrentitem(position); intent.setphotopaths(imagepaths); startactivityforresult(intent, request_preview_code); } } }); |
整体源码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
package com.lidong.photopickersample; import android.content.intent; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.baseadapter; import android.widget.button; import android.widget.edittext; import android.widget.gridview; import android.widget.imageview; import com.bumptech.glide.glide; import com.lidong.photopicker.imagecapturemanager; import com.lidong.photopicker.photopickeractivity; import com.lidong.photopicker.photopreviewactivity; import com.lidong.photopicker.selectmodel; import com.lidong.photopicker.intent.photopickerintent; import com.lidong.photopicker.intent.photopreviewintent; import org.json.jsonarray; import java.io.file; import java.util.arraylist; /** * @ * @author lidong * @date 2016-02-29 */ public class mainactivity extends appcompatactivity { private static final int request_camera_code = 10 ; private static final int request_preview_code = 20 ; private arraylist<string> imagepaths = new arraylist<>(); private imagecapturemanager capturemanager; // 相机拍照处理类 private gridview gridview; private int columnwidth; private gridadapter gridadapter; private button mbutton; private string depp; private edittext textview; @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.activity_main); gridview = (gridview) findviewbyid(r.id.gridview); mbutton = (button) findviewbyid(r.id.button); textview= (edittext)findviewbyid(r.id.et_context); int cols = getresources().getdisplaymetrics().widthpixels / getresources().getdisplaymetrics().densitydpi; cols = cols < 3 ? 3 : cols; gridview.setnumcolumns(cols); // preview gridview.setonitemclicklistener( new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { if (position == gridadapter.getmaxposition() - 1 ) { photopickerintent intent = new photopickerintent(mainactivity. this ); intent.setselectmodel(selectmodel.multi); intent.setshowcarema( true ); // 是否显示拍照 intent.setmaxtotal( 6 ); // 最多选择照片数量,默认为9 intent.setselectedpaths(imagepaths); // 已选中的照片地址, 用于回显选中状态 startactivityforresult(intent, request_camera_code); } else { photopreviewintent intent = new photopreviewintent(mainactivity. this ); intent.setcurrentitem(position); intent.setphotopaths(imagepaths); startactivityforresult(intent, request_preview_code); } } }); gridadapter = new gridadapter(imagepaths); gridview.setadapter(gridadapter); mbutton.setonclicklistener( new view.onclicklistener() { @override public void onclick(view v) { depp =textview.gettext().tostring().trim()!= null ?textview.gettext().tostring().trim(): "woowoeo" ; new thread(){ @override public void run() { super .run(); fileuploadmanager.upload(imagepaths,depp); } }.start(); } }); } @override protected void onactivityresult( int requestcode, int resultcode, intent data) { super .onactivityresult(requestcode, resultcode, data); if (resultcode == result_ok) { switch (requestcode) { // 选择照片 case request_camera_code: loadadpater(data.getstringarraylistextra(photopickeractivity.extra_result)); break ; // 预览 case request_preview_code: loadadpater(data.getstringarraylistextra(photopreviewactivity.extra_result)); break ; } } } private void loadadpater(arraylist<string> paths){ if (imagepaths == null ){ imagepaths = new arraylist<>(); } imagepaths.clear(); imagepaths.addall(paths); try { jsonarray obj = new jsonarray(imagepaths); log.e( "--" , obj.tostring()); } catch (exception e){ e.printstacktrace(); } gridadapter.notifydatasetchanged(); } private class gridadapter extends baseadapter{ private arraylist<string> listurls; private int mmaxposition; private layoutinflater inflater; public gridadapter(arraylist<string> listurls) { this .listurls = listurls; inflater = layoutinflater.from(mainactivity. this ); } public int getcount() { if (listurls.size() == 9 ) { mmaxposition = listurls.size()+ 1 ; } else { mmaxposition = listurls.size()+ 1 ; } return mmaxposition; } public int getmaxposition(){ return mmaxposition; } @override public string getitem( int position) { return listurls.get(position); } @override public long getitemid( int position) { return position; } @override public view getview( int position, view convertview, viewgroup parent) { viewholder holder = null ; if (convertview == null ) { holder = new viewholder(); convertview = inflater.inflate(r.layout.item_image, parent, false ); holder.image = (imageview) convertview.findviewbyid(r.id.imageview); convertview.settag(holder); } else { holder = (viewholder)convertview.gettag(); } log.d( "" , "position:" +position+ " mmaxposition:" +mmaxposition); if (position==mmaxposition- 1 ) { // holder.image.settag("default"); holder.image.setimageresource(r.mipmap.ic_launcher); holder.image.setvisibility(view.visible); if (position== 6 &&mmaxposition== 7 ){ holder.image.setimageresource(r.mipmap.ic_launcher); holder.image.setvisibility(view.gone); } } else { final string path=listurls.get(position); glide.with(mainactivity. this ) .load( new file(path)) .placeholder(r.mipmap.default_error) .error(r.mipmap.default_error) .centercrop() .crossfade() .into(holder.image); } return convertview; } public class viewholder { public imageview image; } } } |
上传的类:fileuploadmanager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
package com.lidong.photopickersample; import android.util.log; import com.squareup.okhttp.mediatype; import com.squareup.okhttp.requestbody; import java.io.file; import java.util.arraylist; import retrofit.call; import retrofit.callback; import retrofit.gsonconverterfactory; import retrofit.response; import retrofit.retrofit; import retrofit.http.multipart; import retrofit.http.post; import retrofit.http.part; /** * created by lidong on 2016/2/29 */ public class fileuploadmanager { private static final string endpoint = "http://192.168.1.122:8080" ; public interface fileuploadservice { /** * 上传一张图片 * @param description * @param imgs * @return */ @multipart @post ( "/upload" ) call<string> uploadimage( @part ( "filename" ) string description, @part ( "file\"; filename=\"image.png\"" ) requestbody imgs); /** * 上传6张图片 * @param description * @param imgs1 * @param imgs2 * @param imgs3 * @param imgs4 * @param imgs5 * @param imgs6 * @return */ @multipart @post ( "/upload" ) call<string> uploadimage( @part ( "description" ) string description, @part ( "file\"; filename=\"image.png\"" ) requestbody imgs1, @part ( "file\"; filename=\"image.png\"" ) requestbody imgs2, @part ( "file\"; filename=\"image.png\"" ) requestbody imgs3, @part ( "file\"; filename=\"image.png\"" ) requestbody imgs4, @part ( "file\"; filename=\"image.png\"" ) requestbody imgs5, @part ( "file\"; filename=\"image.png\"" ) requestbody imgs6); } private static final retrofit sretrofit = new retrofit .builder() .baseurl(endpoint) .addconverterfactory(gsonconverterfactory.create()) // .addcalladapterfactory(rxjavacalladapterfactory.create()) // 使用rxjava作为回调适配器 .build(); private static final fileuploadservice apimanager = sretrofit.create(fileuploadservice. class ); /** * 发说说 * @param paths * @param desp */ public static void upload(arraylist<string> paths,string desp){ requestbody[] requestbody= new requestbody[ 6 ]; if (paths.size()> 0 ) { for ( int i= 0 ;i<paths.size();i++) { requestbody[i] = requestbody.create(mediatype.parse( "multipart/form-data" ), new file(paths.get(i))); } } call<string> call = apimanager.uploadimage( desp,requestbody[ 0 ],requestbody[ 1 ],requestbody[ 2 ],requestbody[ 3 ],requestbody[ 4 ],requestbody[ 5 ]); call.enqueue( new callback<string>() { @override public void onresponse(response<string> response, retrofit retrofit) { log.v( "upload" , response.message()); log.v( "upload" , "success" ); } @override public void onfailure(throwable t) { log.e( "upload" , t.tostring()); } }); } } |
效果:
以上就是本文的全部内容,希望对大家的学习有所帮助。