简体   繁体   English

获取动态创建的Imageview的高度和宽度

[英]Get Height and Width of Dynamically created Imageview

I have multiple images which is created dynamically in for loop. 我有多个在for循环中动态创建的图像。

for (int i = 0; i < 15; i++) {
            ivFront = new TouchImageView(this);
            ivFront.setId(i + 1);
            ivFront.setBackgroundResource(frontImages[i]);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    frontWidth[i], frontHeight[i]);
            ((MarginLayoutParams) params).setMargins(frontX_axis[i],
                    frontY_axis[i], 0, 0);
            ivFront.setAdjustViewBounds(true);
            ivFront.setLayoutParams(params);
            ivFront.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent arg1) {
                    Bitmap bmp = loadBitmapFromView(v);
                    return true;
                }
            });

            //Bitmap bmp_new = loadBitmapFromView(ivFront);
            rlMain.addView(ivFront, params);


        }

public static Bitmap loadBitmapFromView(View v) {
    Canvas bitmapCanvas = new Canvas();
    Bitmap bitmap = Bitmap.createBitmap(v.getWidth() * 2,
            v.getHeight() * 2, Bitmap.Config.ARGB_8888);
    bitmapCanvas.setBitmap(bitmap);
    bitmapCanvas.scale(2.0f, 2.0f);
    v.draw(bitmapCanvas);
    return bitmap;
}

When i write ivFront.getWidth() & ivFront.getHeight() then it shows me 0(Height)-0(Width). 当我编写ivFront.getWidth()和ivFront.getHeight()时,它向我显示0(高度)-0(宽度)。 I already tried to get ImageView height & width in onWindowFocusChanged() method, but the output is same 0(Height)-0(Width). 我已经尝试过在onWindowFocusChanged()方法中获取ImageView的高度和宽度,但是输出是相同的0(Height)-0(Width)。

So when i open the comment then it shows me below error: 因此,当我打开评论时,它向我显示以下错误:

11-05 19:16:28.522: E/AndroidRuntime(9153): Caused by: java.lang.IllegalArgumentException: width and height must be > 0

Use a PreDrawListener to get the dimensions once your view has been measured. 测量视图后,使用PreDrawListener获取尺寸。 OnWindowFocusChanged does not guarantee all views have been measured. OnWindowFocusChanged不保证已测量所有视图。

view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    @Override
    public boolean onPreDraw() {
        view.getViewTreeObserver().removeOnPreDrawListener(this);
        int width = view.getWidth();

    }
});

Add this code for each one of the dynamic imageview : 为每个动态imageview添加以下代码:

ivFront.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    public boolean onPreDraw() {
        iv.getViewTreeObserver().removeOnPreDrawListener(this);
        height = iv.getMeasuredHeight();
        width = iv.getMeasuredWidth();
        return true;
    }
});

ivFront.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent arg1) {
                    Bitmap bmp = loadBitmapFromView(v);
                    return true;
                }
            });

If you want to save all of your dynamic imageview's width and height , you can use List of integer to save the width and height of each your imageview . 如果要保存所有动态imageview's widthheight ,则可以使用整数List保存每个imageview的宽度和高度。

If you have created multiple images dynamically than try this one: 如果您动态创建了多个图像,则可以尝试以下操作:

// initialize your images array //初始化图片数组

private ImageView myImages[] = new ImageView[your_array_length];

// create programatically and add to parent view //以编程方式创建并添加到父视图

for (int i = 0; i < your_array_length; i++) {
                myImages[i] = new ImageView(this);
                myImages[i].setId(i + 1);
                myImages[i].setBackgroundResource(your_array[i]);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                        frontWidth[i], frontHeight[i]);
                ((MarginLayoutParams) params).setMargins(frontX_axis[i],
                        frontY_axis[i], 0, 0);
                myImages[i].setAdjustViewBounds(true);
                myImages[i].setLayoutParams(params);

                if (getIntent() != null && i != your_array,length) {
                    final int j = i;
                    myImages[j].getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                        public boolean onPreDraw() {
                            myImages[j].getViewTreeObserver().removeOnPreDrawListener(this);
                    finalHeight = myImages[j].getMeasuredHeight();
                        finalWidth = myImages[j].getMeasuredWidth();
                    your_textview.setText("Height: " + finalHeight + " Width: " + finalWidth);
                            return true;
                        }
                    });
                }
                your_parent_layout.addView(myImages[i], params);
            }

// Run code //运行代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM