热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

ImageView大小未正确显示-ImageViewsizeisnotdisplayedcorrectly

ImhavingaproblemofdisplayingImageViewswiththecorrectsize.AlmostallthepoststhatIve

I'm having a problem of displaying ImageViews with the correct size. Almost all the posts that I've read through involves ImageViews that are created in the layout file. However in my case, I'm creating ImageViews programmatically.

我有一个显示正确大小的ImageViews的问题。我读过的几乎所有帖子都涉及在布局文件中创建的ImageView。但是在我的情况下,我是以编程方式创建ImageViews。

I'm trying to display all images from a particular folder located in the storage. The bitmaps retrieved will be placed in ImageViews which are contained within a GridView.

我正在尝试显示位于存储中的特定文件夹中的所有图像。检索到的位图将放置在GridView中,ImageView包含在GridView中。

Here's my code:

这是我的代码:

//pass an array containing all images paths to adapter
imageGrid.setAdapter(new GridViewAdapter(getActivity(), FilePathStrings));

part of GridVewAdapter code:

GridVewAdapter代码的一部分:

public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;

            if(cOnvertView== null){
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setPadding(5, 5, 5, 5);
                imageView.setAdjustViewBounds(true);

            }else{
                imageView = (ImageView) convertView;
            }

            imageView.setTag(filepath[position]);
            new LoadImages(imageView).execute();

            return imageView;
        }

AsyncTask to retrieve bitmaps:

AsyncTask检索位图:

private class LoadImages extends AsyncTask {

        private ImageView imgView;
        private String path;

        private LoadImages(ImageView imgView) {
            this.imgView = imgView;
            this.path = imgView.getTag().toString();
        }

        @Override
        protected Bitmap doInBackground(Object... params) {
            Bitmap bitmap = null;

            BitmapFactory.Options bmOptiOns= new BitmapFactory.Options();
            bmOptions.inJustDecodeBounds = false;
            bmOptions.inSampleSize = 6;

            bitmap = BitmapFactory.decodeFile(path, bmOptions);
            try {

                int dimension = getSquareCropDimensionForBitmap(bitmap);
                bitmap = ThumbnailUtils.extractThumbnail(bitmap, dimension, dimension);

            }catch (Exception e) {
                e.printStackTrace();
            }

            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap result) {

            if (!imgView.getTag().toString().equals(path)) {
                return;
            }

            if(result != null && imgView != null){
                imgView.setVisibility(View.VISIBLE);
                imgView.setImageBitmap(result);

            }else{
                imgView.setVisibility(View.GONE);
            }
        }
    }

Method to get equal dimensions so bitmaps will be displayed as squares:

获得相同尺寸的方法使位图显示为正方形:

private int getSquareCropDimensionForBitmap(Bitmap bitmap) {

           int dimension;
            //If the bitmap is wider than it is height then use the height as the square crop dimension

            if (bitmap.getWidth() >= bitmap.getHeight())    {
                dimension = bitmap.getHeight();
            }
            //If the bitmap is taller than it is width use the width as the square crop dimension

           else    {
                dimension = bitmap.getWidth();
            }
            return dimension;

    }

GridView in the layout file:

布局文件中的GridView:

    


The result that I've gotten:

结果我得到了:

imgview_result

The circled thin "lines" are actually the ImageViews displayed.

带圆圈的细线“实际上是显示的ImageViews”。

Any help is very much appreciated. Thank you in advance!

很感谢任何形式的帮助。先感谢您!

2 个解决方案

#1


0  


#2


0  

I have other views above the GridView so I can't set its height to fill_parent/match_parent in the .xml file as it will affect the views above.

我在GridView上面有其他视图,因此我无法在.xml文件中将其高度设置为fill_parent / match_parent,因为它会影响上面的视图。

The solution to my problem is to set GridView's height and width programmatically.

我的问题的解决方案是以编程方式设置GridView的高度和宽度。

Replace:

imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

With this:

imageView.setLayoutParams(new GridView.LayoutParams(int height, int width));

Explanation:

Grid View doc

网格视图文档

Anyone who has a better answer, please post it :)

谁有更好的答案,请发布:)


推荐阅读
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • 本文介绍了如何使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换。首先解释了JSON的概念和数据格式,然后详细介绍了相关API,包括JSONObject和Gson的使用方法。接着讲解了如何将json格式的字符串转换为kotlin对象或List,以及如何将kotlin对象转换为json字符串。最后提到了使用Map封装json对象的特殊情况。文章还对JSON和XML进行了比较,指出了JSON的优势和缺点。 ... [详细]
  • 本文介绍了Android中的assets目录和raw目录的共同点和区别,包括获取资源的方法、目录结构的限制以及列出资源的能力。同时,还解释了raw目录中资源文件生成的ID,并说明了这些目录的使用方法。 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了一种解析GRE报文长度的方法,通过分析GRE报文头中的标志位来计算报文长度。具体实现步骤包括获取GRE报文头指针、提取标志位、计算报文长度等。该方法可以帮助用户准确地获取GRE报文的长度信息。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
author-avatar
郭尚刚
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有