如何使用可绘制的图像设置活动的背景并为其自定义大小?
背景图片的宽度应与父图片的宽度匹配,但背景图片的高度应比父图片的高度小x dp(s),在这里我可以调整x的值,因此,如果我的屏幕尺寸为1920 x 1080
和x = 200
,则背景图片应为1920 x 880
在尺寸方面。
可以使用drawable或canvas完成吗?
以编程方式获取实际图像尺寸并设置自定义尺寸,请看这里
Display display = getWindowManager().getDefaultDisplay();
ImageView iv = (LinearLayout) findViewById(R.id.imageview);
int width = display.getWidth(); // return image width
int height = display.getHeight();// return actual height
//now set custom size
int actual_size = height - 200;
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width,actual_size );
iv.setLayoutParams(parms);
//for constraint layout
ConstraintLayout.LayoutParams newParams = new ConstraintLayout.LayoutParams(width,actual_size);
iv.setLayoutParams(newParams );
希望对您有帮助!!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句