我正在开发一个应用程序,它会截取布局的屏幕截图,将其保存为PDF,然后将其发送到Google Cloud Print进行打印。一切正常,但我对截图的方向有疑问。
对于宿主片段,我制作了横向和纵向布局。屏幕截图必须始终处于纵向状态,因此我们的想法是以编程方式将屏幕旋转为纵向,截取屏幕截图,然后将屏幕方向设置回传感器。我用这种方法更改了配置:
public void lockScreenOrientation(String orientation) {
if (orientation.contains("portrait")) {
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (orientation.contains("auto")) {
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
}
屏幕截图和PDF创建发生在AsyncTask中,由Host Fragment中的确认对话框触发
if (print) {
new AsyncPDF(mActivity).execute(field_client_name_txt, "birthday");
prefs.edit().putBoolean("Print", false).commit();
}
当方向是纵向时,一切都很棒!如果必须将方向旋转为纵向以进行屏幕截图并进行打印,则会出现问题。 屏幕截图捕捉横向布局的图像而不是纵向图像,最终的PDF格式很杂乱并被裁剪!
这里的参考是我的Asynctask:
public class AsyncPDF extends AsyncTask<String, Void, Void> {
Activity mActivity;
Context mContext;
private static final int REQUEST_CODE = 4;
private static String SDCARD = Environment.getExternalStorageDirectory().getPath();
private static String DIRECTORY = SDCARD + "/Amorino";
private static String DIR_CACHE = DIRECTORY + "/Cache/";
private static String DIR_BIRTHDAY_ORDERS = DIRECTORY + "/Orders/Birthday/";
private static String DIR_WEDDING_ORDERS = DIRECTORY + "/Orders/Wedding/";
private static String DIR_OTHER_ORDERS = DIRECTORY + "/Orders/Other/";
private static String DIR_PRODUCTION = DIRECTORY + "/Production/";
String pdfName, pdfType = "";
Utils mUtility;
public AsyncPDF(Activity activity) {
this.mActivity = activity;
this.mContext = activity;
}
@Override
protected void onPreExecute() {
mUtility = new Utils(mContext);
Toast.makeText(mActivity, "Αποθήκευση αρχείου...", Toast.LENGTH_SHORT)
.show();
screenCapture();
};
@Override
protected Void doInBackground(String... params) {
pdfName = params[0];
pdfType = params[1];
createPdf();
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
clearCacheFolder();
Toast.makeText(mActivity, "Το αρχείο αποθηκέυτηκε επιτυχώς!",
Toast.LENGTH_SHORT).show();
}
private void screenCapture() {
try {
ScrollView mLayoutRoot = (ScrollView) mActivity
.findViewById(R.id.print_screen_layout);
mLayoutRoot.setDrawingCacheEnabled(true);
mLayoutRoot.buildDrawingCache();
Bitmap mBitmap = mLayoutRoot.getDrawingCache();
File file, f = null;
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
file = new File(DIR_CACHE);
if (!file.exists()) {
file.mkdirs();
}
f = new File(file.getAbsolutePath() + File.separator
+ "temp_layout" + ".png");
}
FileOutputStream ostream = new FileOutputStream(f);
mBitmap.compress(CompressFormat.PNG, 10, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void createPdf() {
Document document = new Document();
Image header_img, main_img;
InputStream inputStream_header = null;
String current_directory = "";
try {
AssetManager mngr = mContext.getAssets();
if (pdfType.contains("birthday")) {
inputStream_header = mngr.open("header_birthday.png");
current_directory = DIR_BIRTHDAY_ORDERS;
} else if (pdfType.contains("wedding")) {
inputStream_header = mngr.open("header_wedding.png");
current_directory = DIR_WEDDING_ORDERS;
} else if (pdfType.contains("other")) {
inputStream_header = mngr.open("header_order.png");
current_directory = DIR_OTHER_ORDERS;
} else if (pdfType.contains("production")) {
inputStream_header = mngr.open("header_order.png");
current_directory = DIR_PRODUCTION;
}
File file = new File(current_directory);
if (!file.exists()) {
file.mkdirs();
}
PdfWriter.getInstance(document, new FileOutputStream(
current_directory + "/" + pdfName + ".pdf"));
document.open();
Bitmap bmp = BitmapFactory.decodeStream(inputStream_header);
ByteArrayOutputStream stream_header = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream_header);
header_img = Image.getInstance(stream_header.toByteArray());
header_img.scalePercent(24f);
header_img.setAbsolutePosition(-1f, 750f);
document.add(header_img);
Paragraph paragraph = new Paragraph(
header_img.getScaledHeight() + 50f);
document.add(paragraph);
main_img = Image.getInstance(DIR_CACHE
+ "/temp_layout.png");
main_img.scaleToFit(520f, 2000f);
main_img.setAlignment(Image.ALIGN_CENTER);
Log.d("Original Width: ", String.valueOf(main_img.getWidth()));
Log.d("Original Height ", String.valueOf(main_img.getHeight()));
Log.d("Scaled Width: ", String.valueOf(main_img.getScaledWidth()));
Log.d("Scaled Height ", String.valueOf(main_img.getScaledHeight()));
document.add(main_img);
document.close();
File f = new File(current_directory + "/" + pdfName + ".pdf");
printPdf(f, pdfName);
} catch (Exception e) {
e.printStackTrace();
}
}
private void printPdf(File f, String name) {
Uri docUri = Uri.fromFile(f);
Intent printIntent = new Intent(mContext, PrintDialogActivity.class);
printIntent.setDataAndType(docUri, "application/pdf");
printIntent.putExtra("title", name + ".pdf");
mActivity.startActivityForResult(printIntent, REQUEST_CODE);
}
private void clearCacheFolder() {
File dir = new File(DIR_CACHE);
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
new File(dir, children[i]).delete();
}
}
}
}
我认为毛刺发生在mActivity参数上。我认为它通过片段与原始的景观布局而不是肖像。我试图使用存储在SharedPreference中的“print”标志,然后从onResume方法执行AsyncTask,没有运气。屏幕截图仍然抓住了景观布局。有什么想法吗?
在片段生命周期中的哪一点之后我应该执行AsyncTask以获得正确的(纵向)布局?
P.S。第一个问题!
答案 0 :(得分:0)
我终于在一周前弄明白了,这就是解决方案!
问题通过this answer的提示得到解决。我添加了一个GlobalLayoutListener,以便在新布局解决后启动AsyncPDF类,问题几乎解决了。后来我不得不处理一个新问题......!
现在PDF上印有正确的版面,但是一些EditText字段显示为空!经过很多挫折,头脑刮擦和试错测试,我弄清楚问题是什么。 GlobalLayoutListener按预期工作,但似乎它过早地启动了打印功能,因此字段没有时间被正确绘制/填充。
为了解决这个问题,我使用Handler类添加了一个最小延迟(100ms),之后一切都按原样运行!这是打印方法的代码:
public void print(String orientation) {
print = prefs.getBoolean("print", false);
if (print)
if (orientation.contains("portrait")) {
new AsyncPDF(mActivity, v).execute(field_client_name_txt,
"birthday");
} else {
ViewTreeObserver observer = v.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
/* Adds a minimal wait time for the view to settle */
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
new AsyncPDF(mActivity, v).execute(
field_client_name_txt, "birthday");
}
}, 100);
v.getViewTreeObserver().removeOnGlobalLayoutListener(
this);
}
});
}
/* Reset the print flag to false */
prefs.edit().putBoolean("print", false).commit();
}
该方法从ActionBar中的打印按钮启动,如果我们已经在Portrait中,它将启动AsyncPDF类imm。在横向方向的情况下,它等待纵向布局稳定,然后增加100ms延迟,然后启动AsyncTask。
public void onClick(DialogInterface dialog, int id) {
prefs.edit().putBoolean("print", true).commit();
if (mUtility.getScreenOrientation() == 1)
print("portrait");
else
mUtility.lockScreenOrientation("portrait");
}
});
print(字符串方向)方法放在onActivityCreated()方法内部,因此当配置发生更改时,它会检查是否触发了打印标记并采取相应的行动。