简体   繁体   中英

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.

Can this be done using drawable or 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!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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