I am displaying an image using ImageView container. the image needs to be resized when a user enters a specific value and clicks update
resize is just for display, the original resource remains as it is. NO editing, save, etc
xml layout
<ImageView
android:background="#drawable/picture01"
android:id="#+id/picture01holder"
android:layout_below="#+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
</ImageView>
main.java
final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
now what do i do to dynamically assign this pic01 a size of my choice. just the function, i can implement it inside a button myself. i believe i have to use something like pic01.setLayoutParams but i dont know how to use it.
basically i want to overwrite layout_height="wrap_content" and layout_width="wrap_content" in .java
change the Imageview size by taking LayoutParams
final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
pic01.setLayoutParams(layoutParams);
First of all, you have to set the scale type of the image to CENTER_INSIDE or CENTER_CROP depending on your preferences. You should do this in your onCreate() method.
myImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Afterwords you just have to resize the image view in your layout. This will depend on the type of layout you're using. An example for LinearLayout would be:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100,200);
myImageView.setLayoutParams(params);
Or you can set the components minimal size:
myImageView.setMinimumWidth(200);
myImageView.setMinimumHeight(200);
In both cases make sure that the size can be achieved inside the layout. If you have multiple components with smaller weights, the image view may be unable to take up the space you need.
RelativeLayout.LayoutParams myParams = new RelativeLayout.LayoutParams(yourWidthHere, yourHeightHere);
pic01.setLayoutParams(myParams)
Related
I'm working with an ImageView and I want to resize it programmatically passing (for example) from a full-screen ImageView to a 50x50. Is there a way to do that?
Different from the one you suggested me to see because I don't need to fit the image in the ImageView but to resize the dimension of the ImageView.
My suggestion use a relative layout and put image inside that has with weight and able to auto resize which depends on the weight. The best way to auto resize even your screen rotates.
I didn't remember which one layout has weight format
You can use LayoutParams to set height width programmatically -
ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.width = 30;
layoutParams.height = 30;
imageView.setLayoutParams(layoutParams);
ConstraintLayout provides a mechanism for fixing the aspect ratio of a child view. Select the child view that we want to control, and then set the ratio value, we can manually put the ratio that we want to set.
Using constraintLayout makes UI shrink or expand according to screen size without distorting the UI. This is the most important advantage of using ConstraintLayout in UI.
I have an XML Relative layout, with just a few textviews and buttons on it. But I would like to place an image (or sometimes multiple copies of the image) at different x and Y coordinate which is worked out later on.
Unfortunately I can't seem to figure out how to create the ImageView in android (besides XML) and make it appear at the desired coordinate.
Also it would be great if I could help in making it disappear or removing it at a later stage as well.
You can create a ImageView programmatically with ImageView iv = new ImageView(context); Then you have to set the LayoutParameters for the view. If you plan to add the view to a RelativeLayout you must use RelayiveLayout.LayoutParams. So you can have to do the same as you know from xml: add layout rules. See the documentation.
Then overall something like this:
ImageView iv = new ImageView(context);
RelayiveLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// TODO set the params
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
iv.setLayoutParams(params);
relativeLayout.addView(iv);
To hide the imageView you can user imageView.setVisibility(View.GONE)
I have 2 bitmaps that I saved with view.getDrawingCache(); called firstBitmap and secondBitmap. They were saved from initial drawings made by the user. Now I want to display them. My issue is that only the first picture is displayed by itself (I want both to be shown at the same time). I know that the bitmaps are correct because if I remove layout.addView(pic1), I can see the second image by itself clearly. layout is a LinearLayout.
My suspicion is that the size is an issue but I believe that by default views that are added to the layout have fill parent so this should still result in 2 images not one. I have tried dynamically changing the width and size with LayoutParams but this makes the image disappear entirely. I am open to any suggestions.
ImageView pic1 = new ImageView(this);
pic1.setImageBitmap(firstBitmap);
layout.addView(pic1);
ImageView pic2 = new ImageView(this);
pic2.setImageBitmap(secondBitmap);
layout.addView(pic2);
Try to distribute the weight in your linear layout so that both bitmaps will be visible.
you could use the below code to do that.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height, weight);
layout.addView(pic1, params);
layout.addView(pic2, params);
I'm a starter Android learner and I think that this is a very easy question but I can't find it anywhere. I have added an ImageView like this:
//....
iv = new ImageView(context);
iv.setBackground(getResources().getDrawable(drawable));
iv.setAdjustViewBounds(true);
RelativeLayout.LayoutParams imajpara=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
iv.setMaxWidth(100);
iv.setMaxHeight(100);
iv.setLayoutParams(imajpara);
iv.setScaleType(ScaleType.CENTER_INSIDE);
//....
But the ImageView (a ball picture in this case) fits the entire screen, even though the image (.png) is 100x100 pixels. In the code you see my attempts to fix this but none of them worked. The imageview is in a RelativeLayout.
So how can I make the ImageView resize itself according to the image's dimensions?
Use setImageDrawable() instead of setBackground().
Instead of indicating "WRAP_CONTENT" in the constructor of your Layoutparams, you can set directly the right dimensions.
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.
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 RelativeLayouts) you would have to import the LayoutParams of that type of view.
you can do it all like
ImageView imageView = findViewById(R.id.dl_image);
imageView.getLayoutParams().width = 120;