必须从UI线程调用getWidth

时间:2016-08-29 10:11:12

标签: android

必须从UI线程调用getWidth ..如何解决?我试图创建pdf文件..但getWidth,getHeight和getCanvas出现错误!!!

 private class PdfGenerationTask extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {

        PdfDocument document = new PdfDocument();

        // repaint the user's text into the page
        View content = findViewById(R.id.pdf_content);

        // crate a page description
        int pageNumber = 1;
        PageInfo pageInfo = new PageInfo.Builder(content.getWidth(),
                content.getHeight() - 20, pageNumber).create();

        // create a new page from the PageInfo
        Page page = document.startPage(pageInfo);

        content.draw(page.getCanvas());

        // do final processing of the page
        document.finishPage(page);

        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyyhhmmss");
        String pdfName = "pdfdemo"
                + sdf.format(Calendar.getInstance().getTime()) + ".pdf";

        File outputFile = new File("/sdcard/PDFDemo_AndroidSRC/", pdfName);

        try {
            outputFile.createNewFile();
            OutputStream out = new FileOutputStream(outputFile);
            document.writeTo(out);
            document.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return outputFile.getPath();
    }

3 个答案:

答案 0 :(得分:0)

您可以像这样获得屏幕尺寸:

Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();

但它似乎已被弃用了。现在就这样:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.heightPixels;
int screenHeight = metrics.widthPixels;

答案 1 :(得分:0)

AsyncTaskThread上执行不同UIMain Thread次操作,因此您无法从{{1}获取UI相关信息}}。您必须将Thread相关数据传递给UI并完成工作。

AsyncTask Activity's方法中获取此类维度

onCreate()

并将其传递给您的DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int screenWidth = metrics.heightPixels; int screenHeight = metrics.widthPixels; Canvas canvas = page.getCanvas(); ,如下所示

asyncTask

并按以下步骤更新您的PdfGenerationTask pdfGenerationTask = new PdfGenerationTask(screenWidth, screenHeight, canvas); pdfGenerationTask.execute(); 课程

PdfGenerationTask

答案 2 :(得分:0)

实现AsyncTask的onPreExecute并调用getHeight,getWidth和getCanvas

private class PdfGenerationTask extends AsyncTask<Void, Void, String> {
     @Override
     protected String onPreExecute(Void params){
        this.height=getHeight();
        this.width=getWidth();
     }
    @Override
    protected String doInBackground(Void... params) {

        PdfDocument document = new PdfDocument();

        // repaint the user's text into the page
        View content = findViewById(R.id.pdf_content);

        // crate a page description
        int pageNumber = 1;
        PageInfo pageInfo = new PageInfo.Builder(content.getWidth(),
                content.getHeight() - 20, pageNumber).create();

        // create a new page from the PageInfo
        Page page = document.startPage(pageInfo);

        content.draw(page.getCanvas());

        // do final processing of the page
        document.finishPage(page);

        SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyyhhmmss");
        String pdfName = "pdfdemo"
                + sdf.format(Calendar.getInstance().getTime()) + ".pdf";

        File outputFile = new File("/sdcard/PDFDemo_AndroidSRC/", pdfName);

        try {
            outputFile.createNewFile();
            OutputStream out = new FileOutputStream(outputFile);
            document.writeTo(out);
            document.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return outputFile.getPath();
    }