编辑:
别客气,这样就行了
TopRatedPage.setDrawingCacheEnabled(true);
TopRatedPage.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
TopRatedPage.layout(0, 0, TopRatedPage.getMeasuredWidth(), TopRatedPage.getMeasuredHeight());
TopRatedPage.buildDrawingCache(true);
Bitmap screenshot = Bitmap.createBitmap(TopRatedPage.getDrawingCache());
TopRatedPage.setDrawingCacheEnabled(false);
旧的:
我正试着从布局中截取一张截图,就像这样。
LinearLayout TopRatedPage = (LinearLayout)inflater.inflate(R.layout.toprated, null);
...
Bitmap screenshot;
TopRatedPage.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(TopRatedPage.getDrawingCache()); // Caused by: java.lang.NullPointerException
TopRatedPage.setDrawingCacheEnabled(false);
你知道我做错了什么吗?
谢谢!
编辑:
也尝试过这样做,它不会抛出错误,而是给出一个空位图。
Bitmap screenshot = TopRatedPage.getDrawingCache();
最佳答案
试试这个让我知道发生了什么…
LinearLayout TopRatedPage = (LinearLayout)inflater.inflate(R.layout.toprated, null);
Bitmap screenshot = Bitmap.createBitmap(LinearLayout .getWidth(), LinearLayout .getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(screenshot);
LinearLayout.draw(c);
imageView.setImageBitmap(screenshot);
如果可能的话,使用,
View captureView = null;
captureView = inflater.inflate(R.layout.toprated, null);
Bitmap screenshot = Bitmap.createBitmap(captureView.getWidth(), captureView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(screenshot);
captureView .draw(c);
imageView.setImageBitmap(screenshot);
关于android - MyLinearLayout.getDrawingCache()提供了NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10278717/