获取位图视图的片段而不显示

时间:2015-10-28 07:48:55

标签: android android-fragments bitmap

作为这个问题的标题我想获取视图片段的位图图像而不在屏幕上显示它有我的代码

     public Bitmap getScreenShootViewPage(Context context,ViewGroup container){

        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        mSliderMovieDetailPages = new ArrayList<SliderMovieDetailPages>();
        mRootView = inflater.inflate(R.layout.fragment_screen_slide,container,false);
        mViewPager = (ViewPager)mRootView.findViewById(R.id.pager);
        mViewPager.setOffscreenPageLimit(3);
        mViewPager.setClipToPadding(false);
        mViewPager.setPageMargin((int) UtiliesFunctions.convertPixelsToDp(30, context));
        if(MainActivity.mHomeReponse!=null&&MainActivity.mHomeReponse.getRibbons().get(mCategoryPosition)!=null){
            for(int i=0;i<activity.mHomeReponse.getRibbons().get(mCategoryPosition).getItems().size();i++){
                mSliderMovieDetailPages.add(SliderMovieDetailPages.newInstance(activity.mHomeReponse.getRibbons().get(mCategoryPosition).getItems().get(i)));
            }

            mSliderMovieDetailAdapter = new SliderMovieDetailAdapter(getChildFragmentManager(),mSliderMovieDetailPages);
            mViewPager.setAdapter(mSliderMovieDetailAdapter);
            mViewPager.setCurrentItem(mItemPosition);
        }
       return UtiliesFunctions.loadBitmapFromView(mRootView);
    }

这是我的loadBitmapFromView函数

public static Bitmap loadBitmapFromView(View v) {
            Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
            b.recycle();
            Canvas c = new Canvas(b);
            v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
            v.draw(c);
            return b;

    }

我不知道为什么函数v.getMeasuredWidth()总是返回0请纠正我。非常感谢!

3 个答案:

答案 0 :(得分:0)

原因可能是您过早地调用代码并且仍然没有查看大小。

你可以通过覆盖onWindowFocusChanged(boolean hasFocus) medthod

来尝试

答案 1 :(得分:0)

使用此代码将视图转换为位图。您必须调用view.measure,否则getMeasuredwidth()将返回0.

 public Bitmap getBitmapFromView(View view) {
    view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    view.buildDrawingCache();
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable drawable = view.getBackground();
    if (drawable != null)
        drawable.draw(canvas);
    view.draw(canvas);
    return returnedBitmap;
}

答案 2 :(得分:0)

MeasuredWidth为0,因为尚未测量View,因为它尚未显示(它可能在不同的父项中具有不同的大小)。与MeasuredHeight相同。 但是,您可以自己测量视图。基本上你正在模仿父母给出的大小,如果它有一个/或它将绘制。 以下是一些例子:

父级强制100px宽度和高度

int widthMeasureSpec = MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY);   
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(100, MeasureSpec.EXACTLY);

父级允许任何高度:

int widthMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);   
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

父允许它自己的宽度,高度应该由视图本身计算(例如,如果您有TextView,则非常有用,因为那时高度可能会根据宽度而变化):

int widthMeasureSpec = MeasureSpec.makeMeasureSpec(parentView.getWidth(), MeasureSpec.EXACTLY);   
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

因此,如果您获得了宽度和高度测量规格,那么您可以通过运行来测量视图:

v.measure(widthMeasureSpec, heightMeasureSpec)

运行此功能后,v.getMeasuredWidth()v.getMeasuredHeight()应返回预期值。