如何将xml布局转换为图像?



在我的Android项目中,我有超过100个xml布局。 安卓工作室中是否有可以截图布局的功能? 我想将公司报告的所有布局转换为 jpeg 或 png。

在这个函数中,你只需要传递视图,它就会给你位图,你可以存储它或在imageview上显示它

public void takeScreenshot(View screenshotView) {
try {
View view = null;
Bitmap b = null;
if (screenshotView instanceof TextureView) {
b = ((TextureView) screenshotView).getBitmap();
} else {
view = screenshotView;
}
if (view != null) {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
b = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
}
if (imageViewParentLayout != null) {
if (imageView != null) {
imageView.setVisibility(View.VISIBLE);
imageView.setImageBitmap(b);
}
}
String path = saveToAlbum(b);
Notify.INSTANCE.Toast("Image Saved :" + path);
scanfiles(path);
} catch (Exception e) {
e.printStackTrace();
Notify.INSTANCE.Toast("Something Went Wrong");
}finally {
hideImageView();
}
}

private void scanfiles(String path) {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
File outputFile = new File(path);
final Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
final Uri contentUri = Uri.fromFile(outputFile);
scanIntent.setData(contentUri);
AppController.getAppContext().sendBroadcast(scanIntent);
} else {
final Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()));
AppController.getAppContext().sendBroadcast(intent);
}
} catch (Exception e) {
e.printStackTrace();
File file=new File(path);
MediaScannerConnection.scanFile(AppController.getAppContext(), new String[]{
file.getAbsolutePath()},
null, (path1, uri) -> {

});
}
}

最新更新