ImageView的的像素在运行时设定的大小 [英] Set size of ImageView in px at runtime

查看:137
本文介绍了ImageView的的像素在运行时设定的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望把8形象大拇指在一个水平线上,使用整个可用宽度。
这些图像检索到一个web服务,它可以让我指定的尺寸。
我试过如下:

I want to put 8 image thumbs in one horizontal line, using the whole available width.
The images are retrieved from a webservice which lets me specify the dimensions.
I tried the following:

int widthPx = container.getWidth();
LinearLayout thumbs = (LinearLayout)curView.findViewById(R.id.thumbs);
    for(int i=0; i<pics.length; i++) {
        ImageView iv = new ImageView(mContextt);

        int thumbSize = widthPx / 8;

        try {
            String url = "http://someurl/" + pics[i] + "&width=" + thumbSize  + "&height=" + thumbSize;
            URL imgUrl = new URL(url);
            Drawable imgD = Drawable.createFromStream(imgUrl.openStream(), "src");
            iv.setImageDrawable(imgD);
        }
        catch (Exception e) {
            Log.e(TAG, "loading image failed");
            e.printStackTrace();
        }

        thumbs.addView(iv);
    }

本的LinearLayout大拇指有安卓。layout_width =FILL_PARENT集这个code产生的拇指显著较小然后宽度的1/8这是为什么什么是正确的方法?

更新:
这code是里面的 onCreateView 的一个片段。虽然我的宽度值是基于根视图计算是正确的, thumbs.getWidth()的返回0,虽然视图之前膨胀并还应该有480因为layout_width的宽度被设定到FILL_PARENT。我不知道这是一个问题。

The LinearLayout thumbs has android:layout_width="fill_parent set. The thumbs produced by this code are significantly smaller then 1/8 of the width. Why is this? What would be the correct way?

Update:
This code is inside onCreateView of a Fragment. While my width value is calculated based on the root-view and is correct, thumbs.getWidth() returns 0, although the view is inflated before and should also have a width of 480 because of layout_width is set to fill_parent. I'm not sure if that's a problem.

是我的假设是正确的创建ImageViews的布局是默认设置为WRAP_CONTENT?如果不是,该如何设置与Java code?

Is my assumption correct that the layout of the created ImageViews is set to wrap_content by default? If not, how to set this with Java code?

推荐答案

添加的ImageView s的固定大小,即:

Add the ImageViews with a fixed size, i.e.:

thumbs.addView (iv, new LayoutParams(thumbSize, thumbSize));


要回答(部分)在评论这些问题:


To answer (partially) the questions in the comments:

借助 ImageView的 API说:

负责从图像计算它的测量,以便它可以在任何布局管理器可使用

takes care of computing its measurement from the image so that it can be used in any layout manager

所以它可能是假设60PX为160 DPI(我可能是错了)。

so it is probably assuming 60px for a 160 dpi (I may be wrong there).

这篇关于ImageView的的像素在运行时设定的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆