search

Home  >  Q&A  >  body text

android - viewpager中使用getDrawingCache方法截图在某些机型(联想,魅族...)第二次返回的bitmap和第一次相同

//截图并保存
    private String screenshotAndSave() {
//        Bitmap bitmap = ScreenShotUtils.getScreenshot(this);
//        mViewPager.setDrawingCacheEnabled(true);
//        mViewPager.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
//                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
//        mViewPager.layout(0, 0, mViewPager.getMeasuredWidth(), mViewPager.getMeasuredHeight());
//        mViewPager.buildDrawingCache(true);
        Bitmap bitmap = getScreenViewBitmap(mViewPager);
        String fileName = cacheName + "_" + mViewPager.getCurrentItem() +
                ImageUtils.JPG_SUFFIX;
        ImageUtils.saveBitmap(bitmap, isCreateMode ? 30 : 100, ConstantValues.SCREENSHOT_PATH,
                fileName);
//        mViewPager.setDrawingCacheEnabled(false);
        return fileName;
    }

    private Bitmap getScreenViewBitmap(View v) {
        v.setDrawingCacheEnabled(true);
        v.buildDrawingCache(true);

        //creates immutable clone
        Bitmap b = Bitmap.createBitmap(v.getDrawingCache());

        v.setDrawingCacheEnabled(false); //clear drawing cache
        return b;
    }
天蓬老师天蓬老师2772 days ago609

reply all(1)I'll reply

  • PHPz

    PHPz2017-04-17 17:37:15

    Based on the test, re-do the photoview in the fragment

    mPhotoView.destroyDrawingCache();
    mPhotoView.setDrawingCacheEnabled(false);
    mPhotoView.setDrawingCacheEnabled(true);
    mPhotoView.buildDrawingCache();
    

    is enough, the reason is not clear, please give me an answer?

    reply
    0
  • Cancelreply