我使用开源库来划分imageview。我在那个视图上应用了那个库
<com.winsontan520.WScratchView
xmlns:wsv="http://schemas.android.com/apk/res-auto"
android:id="@+id/scratch_view"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true" />
(注意:此视图扩展到surfaceview)。
然后我在该视图上应用了图像源
WScratchView _imageToErase = (WScratchView) findViewById(R.id.scratch_view);
BitmapFactory.Options options2 = new BitmapFactory.Options();
options2.inDither = false;
options2.inPurgeable = true;
options2.inInputShareable = true;
options2.inPreferredConfig = Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(filepath, options2);
_imageToErase.setScratchBitmap(bitmap);
然后在将图像源设置到该视图后,我稍微划了一下。在刮擦之后,我会像那样截取该视图的截图
View u = findViewById(R.id.scratch_view);
u.setDrawingCacheEnabled(true);
u.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(u.getDrawingCache());
u.setDrawingCacheEnabled(false);
File mlr = new File(Environment.getExternalStorageDirectory()
.toString() + File.separator + "Colors");
if (!mlr.exists()) {
mlr.mkdir();
}
String extr = mlr.getPath();
String fileName = new SimpleDateFormat("yyyyMMddhhmm'_Colors.png'")
.format(new Date());
File myPath = new File(extr, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), b,
"Screen", "screen");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
从此视图截取的屏幕截图显示空白视图,有或没有划痕。我搜索了很多,但我不知道我做错了什么。任何帮助将非常感激。谢谢
答案 0 :(得分:0)
这是因为SurfaceView
的渲染与任何其他视图不同。
试试这样:
Bitmap screenshot = Bitmap.createBitmap(_imageToErase.getWidth(), _imageToErase.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(screenshot);
_imageToErase.draw(canvas);
File mlr = new File(Environment.getExternalStorageDirectory()
.toString() + File.separator + "Colors");
if (!mlr.exists()) {
mlr.mkdir();
}
String extr = mlr.getPath();
String fileName = new SimpleDateFormat("yyyyMMddhhmm'_Colors.png'")
.format(new Date());
File myPath = new File(extr, fileName);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
screenshot.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(getContentResolver(), b,
"Screen", "screen");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}