简体   繁体   English

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

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

How can I set the background of my activity with a drawable image and give it a custom size? 如何使用可绘制的图像设置活动背景并为其自定义大小?

The width of the background image should match the parent's but the height of background image should be x dp(s) smaller than the parent's, where I can adjust the value of x, so if my screen size is 1920 x 1080 and x = 200 , the background image should be 1920 x 880 in size. 背景图像的宽度应与父图像的宽度匹配,但背景图像的高度应比父图像的高度小x dp(s),在这里我可以调整x的值,因此,如果我的屏幕尺寸为1920 x 1080x = 200 ,背景图片的尺寸应为1920 x 880

Can this be done using drawable or canvas? 可以使用drawable或canvas完成吗?

Programmatically get the actual image size and set the custom size, have look this 以编程方式获取实际图像尺寸并设置自定义尺寸,请看这里

  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 );

Hope it will help you!! 希望对你有帮助!!

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

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