在ImageView中设置图像宽度和高度
无论我尝试什么,我都无法设置从肥皂服务传递到 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试这样。使用
yourBitmap.getWidth()
和.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()
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
如果将其更改
为:
.. 并且该对象是否放置在 LinearLayout 中,会发生什么?
What happens if you change this:
To:
.. and is the object placed in a LinearLayout?
我不确定您在什么范围内手动设置图像视图,但如果是在为给定 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.
试试这个
try this