oathkeeper oathkeeper - 22 days ago 12
Android Question

ImageView width = 0.75x screen width & height = 1.5 x new ImageView width XML

I'm loading the images into an ImageView using Glide.

Before the images are loaded from internet, I'm adding a placeholder image until the image finishes loading.

The images that I'm loading fortunately have a fixed aspect ratio, i.e. height/width = 1.5

I have the following requirements:
The width of the image view should occupy 75% of the screen width.
The height should be precisely 1.5 times the NEW screen width,
i.e.

width = 0.75 * screenWidth;
height = 1.5 * width;


Curretly I'm achieving this programatically this way

mImageViewPic = (ImageView) view.findViewById(R.id.networkImageView);
mImageViewPic.requestLayout();

// 75% width of the screen taken by the image
mImageViewPic.getLayoutParams().width = (int) (getContext().getResources()
.getDisplayMetrics().widthPixels * 0.75);

// height = 1.5 * width
mImageViewPic.getLayoutParams().height = (int) (mImageViewPic
.getLayoutParams().width * 1.5);


What I'm looking for is an efficient solution to these requirements through XML not programtically.
I know that you can set the width using layout weights as 75% but how do you set height after that?


I hope my requirements are clear. Thanks in Advance.

Answer

you can use PercentRelativeLayout. for example if you want 16:9 aspect ratio you should add something like this to your PercentRelativeLayout :

 android:layout_width="300dp"
 app:layout_aspectRatio="178%"
Comments