Set image width and height in ImageView

No matter what I try, I cannot set the width and height of my image, which is transferred from the soap service to my Android emulator. I am using ImageView as follows:

byte[] bloc = Base64.decode(result, Base64.DEFAULT); Bitmap bmp = BitmapFactory.decodeByteArray(bloc,0,bloc.length); ImageView image = new ImageView(this); image.setImageBitmap(bmp); image.setLayoutParams( new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); image.getLayoutParams().height = 100; image.getLayoutParams().width = 100; setContentView(image); 

In the above code, I am trying to set the width and height manually, jpeg images with a width of 259 pixels and a height of 194 pixels.

The file / res / layout / main.xml looks like

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1" android:id="@+id/llid"> </LinearLayout> 

Having tried some of the approaches below, I just see the following on the emulator

enter image description here

I'm not even sure if the approach I am taking is the right one. Most of the other solutions that I found when searching the forums do not work for me. Any suggestions would be great.

+6
source share
4 answers

Try it. Use yourBitmap.getWidth() and .getHeight()

  image.setLayoutParams( new LinearLayout.LayoutParams( bmp.getWidth(), bmp.getHeight())); 

Edit:

Now you have installed LP for ImgView, the first thing you need to do is add it to the layout

LinearLayout ll = (LinearLayout)findViewById(R.layout.llid);

Now you can either add your view directly, or do it like separating parameters and adding it using .addView(view, params) or .addView(View); your call.

what do you do

ll.addView(image);

Hope this works.

+10
source

What happens if you change this:

 new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

To:

 new LinearLayout.LayoutParams(width, height); 

.. and an object placed in LinearLayout?

+3
source

I’m not sure in which area you manually configure your image, but if this is before onMeasure is called for this ImageView, then this is probably due to the fact that it takes into account the XML layout options.

You can try to overload the onMeasure method for your ImageView and call setMeasuredDimension (100,100) to manually set it there.

+1
source

try it

 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height); imageView.setLayoutParams(params); 
-1
source

Source: https://habr.com/ru/post/893500/


All Articles