如何在Android应用中实现一个图片添加功能?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
1、首先这是用GridView实现的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:orientation="vertical"> <!--展示上传的图片--> <GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:columnWidth="60dp" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:verticalSpacing="10dp" /> </LinearLayout>
Adapter
package com.dearxy.wxcircleaddpic; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import com.bumptech.glide.Glide; import java.util.List; /** * 展示上传的图片的GridView的适配器 * 作者: 周旭 on 2017/6/21/0021. */ public class GridViewAdapter extends android.widget.BaseAdapter { private Context mContext; private List<String> mList; private LayoutInflater inflater; public GridViewAdapter(Context mContext, List<String> mList) { this.mContext = mContext; this.mList = mList; inflater = LayoutInflater.from(mContext); } @Override public int getCount() { //return mList.size() + 1;//因为最后多了一个添加图片的ImageView int count = mList == null ? 1 : mList.size() + 1; if (count > MainConstant.MAX_SELECT_PIC_NUM) { return mList.size(); } else { return count; } } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { convertView = inflater.inflate(R.layout.grid_item, parent,false); ImageView iv = (ImageView) convertView.findViewById(R.id.pic_iv); if (position < mList.size()) { //代表+号之前的需要正常显示图片 String picUrl = mList.get(position); //图片路径 Glide.with(mContext).load(picUrl).into(iv); } else { iv.setImageResource(R.mipmap.zj);//最后一个显示加号图片 } return convertView; } }
MainActivity
这里只贴出了拿到相册返回的图片的路径后的处理
//初始化展示上传图片的GridView private void initGridView() { mPicList = new ArrayList<>(); mGridViewAdapter = new GridViewAdapter(mContext, mPicList); gridView.setAdapter(mGridViewAdapter); gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (position == parent.getChildCount() - 1) { //添加凭证图片 checkPhotoPermission(); } else { //查看大图 Intent intent = new Intent(mContext, PlusImageActivity.class); //图片的路径 intent.putExtra(MainConstant.PIC_PATH, mPicList.get(position)); intent.putExtra(MainConstant.POSITION, position); startActivityForResult(intent, MainConstant.REQUEST_CODE_MAIN); } } }); } // 处理返回照片地址 private void refreshAdapter(final ArrayList<String> paths) { for (int i = 0; i < paths.size(); i++) { Log.i(TAG, "path:---->" + paths.get(i)); proofPicCount++; if (proofPicCount <= MainConstant.MAX_SELECT_PIC_NUM) { //添加图片到GridView mPicList.add(paths.get(i)); } } mGridViewAdapter.notifyDataSetChanged(); }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。