如何获得的视图中适配器高度创建位图的大小? [英] How to get height of view inside adapter for creating sized bitmap?

查看:141
本文介绍了如何获得的视图中适配器高度创建位图的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用自定义项目自定义的CursorAdapter。
我需要的视图高度,从资产文件夹中调整位图和列表项调整此位设置为ImegeView;

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

        }
    }

}

有关获取视图的高度我使用的角度OnGlobalLayoutListener()。

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

但它的速度很慢...
任何想法?

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

推荐答案

我发现我的问题的答案。
使用这种结构,我得到一个正确的宽度或视图中的适配器高度为每个视图。

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 :)

这篇关于如何获得的视图中适配器高度创建位图的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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