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

发布于 2024-11-26 08:23:26 字数 1299 浏览 0 评论 0原文

无论我尝试什么,我都无法设置从肥皂服务传递到 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);

在上面的代码中,我尝试手动设置宽度为 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>

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

“在此处输入图像描述”

我什至不确定我所采取的方法是否正确。我通过搜索论坛找到的大多数其他解决方案都不适合我。任何建议都会很棒。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一刻暧昧 2024-12-03 08:23:26

尝试这样。使用 yourBitmap.getWidth().getHeight()

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

编辑:

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

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

现在您可以直接添加视图,也可以像分隔参数并添加它一样.addView(view, params).addView(View); 您的电话。

所以你这样做

ll.addView(image);

希望它能起作用

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

纸短情长 2024-12-03 08:23:26

如果将其更改

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

为:

new LinearLayout.LayoutParams(width, height);

.. 并且该对象是否放置在 LinearLayout 中,会发生什么?

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?

万水千山粽是情ミ 2024-12-03 08:23:26

我不确定您在什么范围内手动设置图像视图,但如果是在为给定 ImageView 调用 onMeasure 之前,那么这可能就是它尊重 XML 布局参数的原因。

您可以尝试重载 ImageView 的 onMeasure 方法,并调用 setMeasuredDimension(100,100) 手动设置它。

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.

书间行客 2024-12-03 08:23:26

试试这个

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

try this

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
imageView.setLayoutParams(params);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文