码迷,mamicode.com
首页 > 其他好文 > 详细

gridview在最后默认一个添加更多, 将String的路径转为bitmap

时间:2015-08-31 19:43:40      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:android   gridview   

项目遇到,gridview有一个item是点击后选择的,但又新的item加如后这个就不断向后移动


在adapter中将返回的项数加一

if(position<mSelectPath.size()){
    //添加list中的数据
}else {
    image.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.biaoqing));
}

  class MyGridAdapter extends BaseAdapter{
        private Context mContext;
        public MyGridAdapter(Context context) {
            this.mContext=context;
        }
        public void setData(){
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return mSelectPath.size()+1;
        }

        @Override
        public Object getItem(int position) {
                return mSelectPath.get(position);

        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView image;
            if (convertView == null) {
                convertView = View.inflate(mContext, R.layout.gridview_view, null);
                image = (ImageView) convertView.findViewById(R.id.image_gridview);
            } else {
                image = (ImageView) convertView.findViewById(R.id.image_gridview);
            }
            if(position<mSelectPath.size()){
                String path = mSelectPath.get(position);
                Bitmap bitmap = convertToBitmap(path, 500, 400);
                image.setImageBitmap(bitmap);
            }else {
                image.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.biaoqing));
            }
            return convertView;
        }

点击事件:else的时候就是点击的默认的呢一项

       mGridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                if (position < parent.getCount() - 1) {
                    String path = (String) parent.getAdapter().getItem(position);
                    for (int i = 0; i < mSelectPath.size(); i++) {
                        if (path.equals(mSelectPath.get(i))) {
                            mSelectPath.remove(mSelectPath.get(i));
                            myGridAdapter.setData();
                            break;
                        }
                    }
                } else {
                    Intent intent = new Intent(MyTest.this, MultiImageSelectorActivity.class);
                    // 是否显示拍摄图片
                    intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, false);
                    // 最大可选择图片数量
                    intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 9);
                    // 选择模式
                    intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, 1);
                    // 默认选择
                    if (mSelectPath != null && mSelectPath.size() > 0) {
                        intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST, mSelectPath);
                    }
                    startActivityForResult(intent, REQUEST_IMAGE);
                }
                return true;
            }
        });
String路径转成Bitmap
        //String转换成bitmap
        public Bitmap convertToBitmap(String path, int w, int h) {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            // 设置为ture只获取图片大小
            opts.inJustDecodeBounds = true;
            opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
            // 返回为空
            BitmapFactory.decodeFile(path, opts);
            int width = opts.outWidth;
            int height = opts.outHeight;
            float scaleWidth = 0.f, scaleHeight = 0.f;
            if (width > w || height > h) {
                // 缩放
                scaleWidth = ((float) width) / w;
                scaleHeight = ((float) height) / h;
            }
            opts.inJustDecodeBounds = false;
            float scale = Math.max(scaleWidth, scaleHeight);
            opts.inSampleSize = (int)scale;
            WeakReference<Bitmap> weak = new WeakReference<Bitmap>(BitmapFactory.decodeFile(path, opts));
            return Bitmap.createScaledBitmap(weak.get(), w, h, true);
        }



版权声明:本文为博主原创文章,未经博主允许不得转载。

gridview在最后默认一个添加更多, 将String的路径转为bitmap

标签:android   gridview   

原文地址:http://blog.csdn.net/sinat_23134455/article/details/48137567

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!