【问题标题】:How to get height of view inside adapter for creating sized bitmap?如何获取适配器内的视图高度以创建大小位图?
【发布时间】:2013-10-03 11:10:01
【问题描述】:

我将自定义 CursorAdapter 与自定义项一起使用。 我需要视图高度来调整资产文件夹中的位图大小,并将这个调整大小的位图设置为列表项中的 ImegeView;

 @Override
public void bindView(View view, final Context context, final Cursor cursor) {
    final ViewHolder holder = (ViewHolder) view.getTag();

    final int imgCol = cursor.getColumnIndex(TableOdelice.COLUMN_URL);
    int titleCol = cursor.getColumnIndex(TableOdelice.COLUMN_TITRE);
    final int themeCol = cursor.getColumnIndex(TableOdelice.COLUMN_THEME);

    String tempPath = getPath(cursor.getString(themeCol), cursor.getString(imgCol));
    final String path = tempPath.replace(".", "c.");
    String[] arr = cursor.getString(titleCol).split("\\*");
    holder.title.setText(arr[0]);
    holder.subTitle.setText(arr[1]);

    if (itemHeight > 0) {
        showThumb(itemHeight, holder.img, path);
    } else {
        final ImageView v = holder.mainImage;
        v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                itemHeight = v.getHeight();
                showThumb(itemHeight, holder.img, path);
            }
        });
    }
}

  private void showThumb(int height, final ImageView iv, final String path) {
    if (thumbs.containsKey(path)) {
        iv.setImageBitmap(thumbs.get(path));
    } else {
        InputStream is = null;
        try {
            is = context.getAssets().open(path);
        } catch (IOException e) {
            Log.d("no file at path", path);
            e.printStackTrace();
        }
        if (is != null) {
                Bitmap btm = btmHelper.scaleToHeight(BitmapFactory.decodeStream(is);, height);
            thumbs.put(path, btm);
            iv.setImageBitmap(btm);

        }
    }

}

为了获取视图高度,我使用视图的 OnGlobalLayoutListener()。

但是速度很慢... 有什么想法吗?

【问题讨论】:

  • v.getLayoutParams().height 应该可以工作,我想
  • @Thijs Mergaert ,它不起作用并且总是返回-1。感谢您的回复

标签: android view bitmap adapter


【解决方案1】:

我找到了我的问题的答案。 使用这种结构,我可以在适配器内为每个视图获得正确的视图宽度或高度。

final ImageView v = holder.mainImage;
            v.post(new Runnable() {
            @Override
            public void run() {
                itemHeight = v.getHeight();
                Log.d("Height", "" + itemHeight);
            }

        });
    }

也许它会帮助某人:)

【讨论】:

    【解决方案2】:

    试一试可能会更快

     v.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                itemHeight = v.getHeight();
                showThumb(itemHeight, holder.img, path);
                v.getViewTreeObserver().removeGlobalOnLayoutListener( ActivityInsatance);
            }
        });
    

    【讨论】:

    • Ali4Android,我需要在适配器内获取视图高度,并且 iv.getHeight() 总是返回 0。感谢您的回复,看看这个stackoverflow.com/a/11169602/1180477
    • 你使用 v.getViewTreeObserver m 获得图像视图的准确高度
    • 是的 v.getViewTreeObserver 得到正确的高度,但经过一段时间后视图会导致 onGlobalLayout() 方法,它看起来很丑
    【解决方案3】:

    如果您有顶部或底部边距,则会出现错误。当您滚动此列表视图时,每行的高度都会增加。在 v.getViewTreeObserver().addOnGlobalLayoutListener 的情况下,它将无限增长。所以,我的解决方案如下。

    holder.layout.post(new Runnable() {
        @Override
        public void run() {
            int height = holder.layout.getMeasuredHeight(); // Total row height.
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.textView.getLayoutParams();
            holder.textView.setHeight(height - lp.topMargin - lp.bottomMargin);
    });
    

    其中 holder.layout 是到适配器行的根 RelativeLayout 的链接。

    【讨论】:

      猜你喜欢
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 2017-07-22
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      相关资源
      最近更新 更多