Set image width and height in ImageView

最后都变了- 提交于 2019-12-06 01:46:03

问题


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

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.


回答1:


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




回答2:


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?




回答3:


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.




回答4:


try this

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


来源:https://stackoverflow.com/questions/6817824/set-image-width-and-height-in-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!