繁体   English   中英

如何设置自定义尺寸的背景图片?

[英]How can I set a background image with a custom size?

如何使用可绘制的图像设置活动背景并为其自定义大小?

背景图像的宽度应与父图像的宽度匹配,但背景图像的高度应比父图像的高度小x dp(s),在这里我可以调整x的值,因此,如果我的屏幕尺寸为1920 x 1080x = 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 );

希望对你有帮助!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM