0

I have a Relative Layout within an XML file with an ImageView element which contains both width and height for an image.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/tankHeight"
    android:layout_width="210dp"
    android:layout_height="350dp"
    android:layout_centerInParent="true"
    app:srcCompat="@drawable/tank_progress" />

I then try and dynamically change the height (only height, not width) of the image when the method is called in its class, as follows...

public void updateTank() {
    ImageView myTank = (ImageView)findViewById(R.id.tankHeight);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) 
    myTank.getLayoutParams();
    params.height = 350 - 35;
    myTank.setLayoutParams(params);
    }

However, when this method is called, the image appears to scale by the specified amount in both height and width, when I only want to reduce the height by 35 pixels. My understanding is that this may be due to using a relative layout, but I am not sure how to overcome the issue, so that I can programmatically change the height only. From reading this and this, I had thought the method was set up correctly, so am unsure why it is displaying differently than intended?

esap01
  • 15
  • 5

3 Answers3

0

Changing a view height and/or width requires it to be redrawn, try calling requestLayout(). Hope it helps.

 public void updateTank() {
    ImageView myTank = (ImageView)findViewById(R.id.tankHeight);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) 
    myTank.getLayoutParams();
    params.height = 350 - 35;
    myTank.setLayoutParams(params);

    myTank.requestLayout();
 }
Aalap Patel
  • 2,058
  • 2
  • 17
  • 31
0

I believe you should first convert dp to pixel.

public int convertDpToPixel(float dp) {
    Context context = getContext();
    if (context == null) {
        return 0; // context should never be a null
    }
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return (int) (dp * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));
}

and then you can call

myTank.getLayoutParams();
params.height = convertDpToPixel(350) - convertDpToPixel(35);
myTank.setLayoutParams(params);
C. Alen
  • 184
  • 1
  • 10
0

The default ScaleType for an ImageView is FIT_CENTER. What this means is that the source image will be scaled so that you can see the entire thing, regardless of the view's aspect ratio... so even though your ImageView is probably only changing its height, the image content is being scaled in both dimensions despite only having the view height change.

Try setting android:scaleType="centerCrop" on your ImageView tag. This will allow you to resize the view without re-scaling the image contents.

More info on scale types: https://robots.thoughtbot.com/android-imageview-scaletype-a-visual-guide

Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • Aha! This appears to be having the desired effect, however, the height is being reduced from the bottom of the image up, instead of 'top-down'. Do you know if there is a way to set preference for which 'end' of the image, the pixels are removed from? – esap01 Aug 08 '18 at 14:56
  • Hm, I would have guessed that you'd see it shrink evenly on both ends. I'm not sure how to solve this new problem, sorry. – Ben P. Aug 08 '18 at 15:00