I have setup my ImageView like the following:
<ImageView android:id="@+id/dl_image"
android:layout_width="60dp"
android:background="@drawable/pictureframe"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter"/>
Notice that layout_width is fixed to 60dp. Depending on the content that I acquired online, I want to resize this width to 90dp, or 120dp (while maintaining the aspect ratio of the image).
I tried using setLayoutParams, but passing LayoutParams(120, LayoutParams.WRAP_CONTENT) throws an exception. It doesn't seem to like it.
If possible, I am trying to avoid making another ImageView for larger sizes.
Tap the image you want to adjust. You can adjust the size of an image or rotate it: Resize: Touch and drag the squares along the edges. Rotate: Touch and drag the circle attached to the image.
Assuming that you are referring to the layout in storyboard/IB. To get the native size of the image just select the image and press Command + = on the keyboard. the to re-size it proportionally select the corner and hold down the shift key when you re-size it.
Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.
If you're working with an existing view it can be a lot of work creating a new set of LayoutParams
from scratch. Instead - you can grab the view's existing LayoutParams, edit these, and then apply them to the view to update its LayoutParams using setLayoutParams()
ImageView imageView = findViewById(R.id.dl_image);
LayoutParams params = (LayoutParams) imageView.getLayoutParams();
params.width = 120;
// existing height is ok as is, no need to edit it
imageView.setLayoutParams(params);
Make sure you import the correct type of LayoutParams
. For this case, as you commented, you can just use the LayoutParams
of a ViewGroup
. If you were setting parameters specific to a certain type of view (e.g. alignments in RelativeLayout
s) you would have to import the LayoutParams
of that type of view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With