开发者

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

Set image width and height in ImageView

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.


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


What happens if you change this:

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

To:

new LinearLayout.LayoutParams(width, height);

.. and is the object placed in a LinearLayout?


I'm not sure in what scope you're manually setting your image view, but if it is before the onMeasure gets called for that given ImageView, then that's probobly why it is respecting the XML layout params.

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


try this

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
imageView.setLayoutParams(params);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜