【问题标题】:view getHeight returning 1 but getWidth is correct查看 getHeight 返回 1 但 getWidth 是正确的
【发布时间】:2015-01-29 16:51:55
【问题描述】:

我正在使用 ZXing 在我的应用程序中绘制条形码。

布局是一个图像视图,将条形码保存在线性布局内。 imageview 的高度为 0dp,权重为 3(下方有一个按钮,权重为 1)。

我将 imageview 的 getHeight 和 getWidth 作为参数传递给绘制条形码方法(如下所示)。

我尝试使用全局布局侦听器和 view.post 来等待视图的全高,但是当我在调试模式下启动时,我可以看到视图在调用绘制条形码方法时尚未完全构建。

我已经在片段的 onCreateView 和 onViewCreated 中尝试了这两种方法:

barcodeImage.post(new Runnable() {
        @Override
        public void run() {
            int height = barcodeImage.getHeight(); // returns 1
            int width = barcodeImage.getWidth(); // returns correct width
            createBitmapArray(width, height);
        }
    });

barcodeImage.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                barcodeImage.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                barcodeImage.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }

            int height = barcodeImage.getHeight(); // returns 1
            int width = barcodeImage.getWidth(); // returns correct width
            createBitmapArray(width, height);
        }
    });

这里是绘制条码的方法:

private Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) {
        int WHITE = 0xFFFFFFFF;
        int BLACK = 0xFF000000;

        if (contents == null) {
            return null;
        }

        if (format == BarcodeFormat.QR_CODE)
            img_height = (int) (img_height * 1.5);

        Map<EncodeHintType, Object> hints = null;
        String encoding = guessAppropriateEncoding(contents);
        if (encoding != null) {
            hints = new EnumMap<>(EncodeHintType.class);
            hints.put(EncodeHintType.CHARACTER_SET, encoding);
        }
        MultiFormatWriter writer = new MultiFormatWriter();
        BitMatrix result;
        try {
            result = writer.encode(contents, format, img_width, img_height, hints);
        } catch (WriterException | IllegalArgumentException | IndexOutOfBoundsException e) {
            System.out.println("" + e);
            return null;
        }
        int width = result.getWidth();
        int height = result.getHeight();
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            int offset = y * width;
            for (int x = 0; x < width; x++) {
                pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
            }
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

任何其他等待重量测量的方法确保在绘制条形码之前构建视图(除了我试图避免的 postDelayed)?

【问题讨论】:

    标签: android android-layout zxing


    【解决方案1】:

    你试过 getMeassuredHeight() 吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-02
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多