在ImageView中设置图像宽度和高度

Joe*_*dev 6 android image imageview

无论我尝试什么,我都无法设置从soap服务传递到我的android模拟器的图像的宽度和高度.我正在使用ImageView,如下所示:

            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);
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我试图手动设置宽度为259像素,高度为194像素的jpeg图像的宽度和高度.

/res/layout/main.xml看起来像

<?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>
Run Code Online (Sandbox Code Playgroud)

在尝试下面的答案中的一些方法后,我只是在我的模拟器上看到以下内容

在此输入图像描述

我甚至不确定我采取的方法是否正确.我在搜索论坛时找到的大多数其他解决方案对我不起作用.任何建议都会很棒.

Nik*_*ski 10

试试这样吧.利用yourBitmap.getWidth().getHeight()

  image.setLayoutParams(
              new LinearLayout.LayoutParams(
                     bmp.getWidth(), 
                     bmp.getHeight())); 
Run Code Online (Sandbox Code Playgroud)

编辑:

现在您已经为ImgView设置了LP,您要做的第一件事就是将它添加到布局中

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

现在,您可以直接添加视图,也可以像分离参数并将其添加到.addView(view, params).addView(View);调用一样.

所以你也是

ll.addView(image);

希望它有效

  • 因为`setContentView()`是为你曾经做过的活动设置显式视图(settig the main.xml),然后用你的ImageView设置父fr,用blabla.height = 100你刚刚分配了你从未使用过的值.我希望我足够清楚和简单:D (3认同)