Android ImageView控件的MaxWidth、MaxHeight不起作用

在使用imageview控件时,由于图片大小不一致,需要给图片设置个最大宽高。设置后的代码如下:


LinearLayout ll = new LinearLayout(ConsumeInfo.this);  
ll.setOrientation(LinearLayout.HORIZONTAL);          
//设置小图标  
ImageView imageView = new ImageView(ConsumeInfo.this);              
Bitmap bitmap = BitmapFactory.decodeFile((String)mData.get(position).get("imgpath")); 
imageView.setImageBitmap(bitmap);  
imageView.setMaxHeight(50);
imageView.setMaxWidth(50);
ll.addView(imageView);     

              运行之后,Imageview  仍然被撑开,难道是MaxWidth,MaxHeight不起作用,抓紧翻阅api文档,找到下面的解释:

                       An optional argument to supply a maximum width for this view. Only valid if setAdjustViewBounds(boolean) has been set to true. 

文档中说得很清楚,抓紧修改如下:


LinearLayout ll = new LinearLayout(ConsumeInfo.this);  
ll.setOrientation(LinearLayout.HORIZONTAL);          
//设置小图标  
ImageView imageView = new ImageView(ConsumeInfo.this);              
Bitmap bitmap = BitmapFactory.decodeFile((String)mData.get(position).get("imgpath")); 
imageView.setImageBitmap(bitmap);  
imageView.setAdjustViewBounds(true);  
imageView.setMaxHeight(50);
imageView.setMaxWidth(50);
ll.addView(imageView);     

再次运行,竟然可以了!     
发布了248 篇原创文章 · 获赞 142 · 访问量 197万+
展开阅读全文

imageView设置最小尺寸无效

04-25

int = imageWith = (ScreenUtil.getScreenWidth() - ScreenUtil.dip2px(120)) / 3 private void addImageViewToContainer(String url, final int index, final PostModel data, int totalCount) { ImageView imageView = new ImageView(mContext); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); if (index == 0) {//第一个image不要左边距 params.leftMargin = 0; } else {//其余的左边距为4dp params.leftMargin = ScreenUtil.dip2px(4); } if (totalCount == 1) {//只有一张图时,设置图片的最大宽度和高度为200dp imageView.setMaxHeight(ScreenUtil.dip2px(200)); imageView.setMaxWidth(ScreenUtil.dip2px(200)); //*****当图片宽高小于imageWith时,我本来想让它宽高都按imageWith显示,可是这里设置imageView.setMinimumHeight(imageWith)是没有用的。现在///解决不了这个问题,我看微信朋友圈,当图片宽高小于imageWith时,让图片以最大尺寸显示,也就是以ScreenUtil.dip2px(200)作为宽高显示,怎么实///现呢 imageView.setMinimumHeight(imageWith); imageView.setMinimumWidth(imageWith); LogUtil.e("imageWith", imageWith+""); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setAdjustViewBounds(true); imageContainer.addView(imageView, index); ImageLoader.getInstance().displayImage(url, new ImageViewAware(imageView), imageOptions, new ImageSize(ScreenUtil.dip2px(200), ScreenUtil.dip2px(200)), null, null); } else { params.height = imageWith; params.width = imageWith; imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setLayoutParams(params); if (totalCount == index + 1 && data.getPostViewModel().getImageList().size() > 3) {//超过3张图时,最后一张图,添加Relate布局,和共几张图 RelativeLayout relativeLayout = new RelativeLayout(mContext); // RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(imageWith, imageWith); //// layoutParams.leftMargin = ScreenUtil.dip2px(4); relativeLayout.setLayoutParams(params); TTTextView imageCount = new TTTextView(mContext); imageCount.setBackgroundColor(Color.BLACK); imageCount.setAlpha(0.6f); imageCount.setPadding(ScreenUtil.dip2px(2), ScreenUtil.dip2px(2), ScreenUtil.dip2px(2), ScreenUtil.dip2px(2)); imageCount.setGravity(Gravity.CENTER_VERTICAL); imageCount.setTextColor(mContext.getResources().getColor(R.color.color_white)); imageCount.setTextSize(TypedValue.COMPLEX_UNIT_PX, mContext.getResources().getDimension(R.dimen.default_font_size_very_smallest)); imageCount.setText("共" + data.getPostViewModel().getImageList().size() + "张"); RelativeLayout.LayoutParams textLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); textLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); textLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE); imageCount.setLayoutParams(textLayoutParams); relativeLayout.addView(imageView); relativeLayout.addView(imageCount); imageContainer.addView(relativeLayout, index); } else { imageContainer.addView(imageView, index); } ImageLoader.getInstance().displayImage(url, new ImageViewAware(imageView), imageOptions, new ImageSize(imageWith, imageWith), null, null); } imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(mContext, PreviewActivity.class); intent.putExtra("data", (Serializable) data.getPostViewModel().getImageList()); intent.putExtra("index", index); mContext.startActivity(intent); } }); } 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览