How to get height of view inside adapter for creating sized bitmap?

一个人想着一个人 提交于 2019-12-10 09:30:14

问题


I use custom CursorAdapter with custom items. I need height of view to resize Bitmap from assets folder and set this resized bitmap to ImegeView in list item;

 @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);

        }
    }

}

For getting view height I use OnGlobalLayoutListener() of view.

But it's very slow ... Any ideas?


回答1:


I find answer for my question. Using this construction I get a correct width or height of view inside adapter for each view.

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

        });
    }

Maybe it will help somebody :)




回答2:


Just give it a try may be it will be faster

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



回答3:


If you have top or bottom margins, a bug will appear. When you will scroll this listview, a height of each row will increase. In case of v.getViewTreeObserver().addOnGlobalLayoutListener it will grow infinitely. So, my solution follows.

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);
});

where holder.layout is a link to root RelativeLayout of an adapter's row.



来源:https://stackoverflow.com/questions/19157650/how-to-get-height-of-view-inside-adapter-for-creating-sized-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!