Android之仿微信发朋友圈图片选择功能
最近项目中需要用到发表评论选择多张图片和拍照的功能,于是就仿照微信发表朋友圈的选择图片和拍照做了一个这样的案例,经过查找资料终于完成了此功能,
最近有时间就写出来和大家分享一下,希望对大家有所帮助。
效果如下图:
代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.zq.weixinselectpicture.MainActivity"> <EditText android:id="@+id/et_context" android:layout_width="match_parent" android:layout_height="166dp" android:background="@android:color/transparent" android:hint="| 输入要发布的内容" android:padding="5dp" android:textColorHint="@android:color/black" android:gravity="top" android:textColor="@android:color/black" /> <GridView android:id="@+id/gridView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:horizontalSpacing="@dimen/space_size" android:numColumns="3" android:verticalSpacing="@dimen/space_size" /> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="@color/colorGray" android:layout_marginTop="25px" ></View> <TextView android:id="@+id/find_comment_submit" android:layout_width="match_parent" android:layout_height="45dp" android:background="@drawable/border_red_roval_sign" android:gravity="center" android:textColor="@android:color/white" android:layout_marginLeft="25dp" android:layout_marginRight="25dp" android:layout_marginTop="25dp" android:text="确定" /> </LinearLayout>
Activity.java
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 GridView gridView; private GridAdapter gridAdapter; private TextView tv_click; private EditText textView; private String TAG =MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = (GridView) findViewById(R.id.gridView); tv_click = (TextView) findViewById(R.id.find_comment_submit); textView= (EditText)findViewById(R.id.et_context); int cols = getResources().getDisplayMetrics().widthPixels / getResources().getDisplayMetrics().densityDpi; cols = cols < 3 ? 3 : cols; gridView.setNumColumns(cols); gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String imgs = (String) parent.getItemAtPosition(position); if ("paizhao".equals(imgs) ){ 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); }else{ PhotoPreviewIntent intent = new PhotoPreviewIntent(MainActivity.this); intent.setCurrentItem(position); intent.setPhotoPaths(imagePaths); startActivityForResult(intent, REQUEST_PREVIEW_CODE); } } }); imagePaths.add("paizhao"); gridAdapter = new GridAdapter(imagePaths); gridView.setAdapter(gridAdapter); tv_click.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } @Override protected void onResume() { super.onResume(); } @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: ArrayList<String> list = data.getStringArrayListExtra(PhotoPickerActivity.EXTRA_RESULT); Log.d(TAG, "数量:"+list.size()); loadAdpater(list); break; // 预览 case REQUEST_PREVIEW_CODE: ArrayList<String> ListExtra = data.getStringArrayListExtra(PhotoPreviewActivity.EXTRA_RESULT); loadAdpater(ListExtra); break; } } } private void loadAdpater(ArrayList<String> paths){ if (imagePaths!=null&& imagePaths.size()>0){ imagePaths.clear(); } if (paths.contains("paizhao")){ paths.remove("paizhao"); } paths.add("paizhao"); imagePaths.addAll(paths); gridAdapter = new GridAdapter(imagePaths); gridView.setAdapter(gridAdapter); try{ JSONArray obj = new JSONArray(imagePaths); }catch (Exception e){ e.printStackTrace(); } } private class GridAdapter extends BaseAdapter { private ArrayList<String> listUrls; private LayoutInflater inflater; public GridAdapter(ArrayList<String> listUrls) { this.listUrls = listUrls; if(listUrls.size() == 7){ listUrls.remove(listUrls.size()-1); } inflater = LayoutInflater.from(MainActivity.this); } public int getCount(){ return listUrls.size(); } @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, parent,false); holder.image = (ImageView) convertView.findViewById(R.id.imageView); convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } final String path=listUrls.get(position); if (path.equals("paizhao")){ holder.image.setImageResource(R.mipmap.find_add_img); }else { Glide.with(MainActivity.this) .load(path) .placeholder(R.mipmap.default_error) .error(R.mipmap.default_error) .centerCrop() .crossFade() .into(holder.image); } return convertView; } class ViewHolder { ImageView image; } } }
权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" />
在此希望大家可以通过该案例,能够去其糟粕,取其精华,能够帮助到大家就我就满足了。
修复版: