WindowManager wm = this.getWindowManager(); int width = wm.getDefaultDisplay().getWidth(); ImageView imageView = new ImageView(mContext); imageView.setAdjustViewBounds(true);//设置图片自适应,只是这句话必须结合下面的setMaxWidth和setMaxHeight才能有效果。 //下面必须使用LinearLayout,如果使用ViewGroup的LayoutParams,则会报空指针异常。 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( width, LinearLayout.LayoutParams.WRAP_CONTENT); imageView.setLayoutParams(layoutParams); imageView.setMaxWidth(width); imageView.setMaxHeight(width * 3);// 这里其实可以根据需求而定,我这里测试为最大宽度的5倍 Glide.with(mContext).load(picPath.get(x)).into(imageView); ll_canshu_pic.setOrientation(1); ll_canshu_pic.addView(imageView);//ll_canshu_pic就是LinearLayout控件
动态的创建ImageView,并设置图片自适应
最新推荐文章于 2024-01-06 11:44:29 发布