Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set image width and height in ImageView

No matter what I try, I cannot set the width and height of my image that is passed from a soap service to my android emulator. I'm using an 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 attempting to set the width and height manually, of a jpeg image that has a 259px width and 194px height.

The /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>

After trying some of the approaches in below's answers, I just see the following on my emulator

enter image description here

I'm not even sure if the approach I am taking is correct. Most other solutions I have found from searching the forums don't work for me. Any suggestions would be great.

like image 593
Joeblackdev Avatar asked Jul 25 '11 14:07

Joeblackdev


1 Answers

Try like this. Make a use of yourBitmap.getWidth() and .getHeight()

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

Edit:

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

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

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

so you do

ll.addView(image);

Hope it works

like image 74
Nikola Despotoski Avatar answered Sep 17 '22 12:09

Nikola Despotoski