Android Studio - 生成的PDF无法正确显示drawable

时间:2016-10-06 15:34:55

标签: android-studio pdf android-drawable

我正在尝试在Android Studio应用中生成PDF文件。大多数情况下它工作正常,但我有一个问题,没有出现的drawables。我不确定它们是否完全没有出现,或者它们是否以白色显示。我尝试将色调改为黑色,但这没有用。我不知道这是我做错了什么,还是一个已知的问题。有没有其他人遇到过它?

PDF的制作如下:

// Create PDF document
PdfDocument document = new PdfDocument();
// Create page description
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(content.getWidth(), content.getHeight(), 1).create();

// Start first page - pattern details and chart
PdfDocument.Page page = document.startPage(pageInfo);
content.draw(page.getCanvas());
// Finish page
document.finishPage(page);

Cell类的onDraw函数是:

   protected void onDraw(Canvas canvas) {

        float height = canvas.getHeight();
        float width = canvas.getWidth();

        paint.setColor(0xff000000);
        paint.setStrokeWidth(1);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(1, 1, width-1, height-1, paint);

        // Drawable has been set up already, as listed below
        if(drawable != null) {
            int b_width = (int) (width - 1);
            int b_height = (int) (height - 1);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                if (mode == MODE_KEY) drawable.setTint(Color.BLACK);
            }
            drawable.setBounds(1, 1, b_width, b_height);
            drawable.draw(canvas);
        }
    }

并且Drawable的设置如下:

        String drawableName = "kps_" + thisSymbol.getSymbolBaseName();
        final int resourceId = resources.getIdentifier(drawableName, "drawable", context.getPackageName());
        Drawable d;
        if(resourceId != 0) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                d = resources.getDrawable(resourceId, context.getTheme());
            } else {
                d = resources.getDrawable(resourceId);
            }
            thisCell.setDrawable(d);
        }

'thisCell.setDrawable(d)'行用于将drawable传递给Cell类;这也使Cell无效,导致它重绘。

任何指针?或者有更好的方法吗?

0 个答案:

没有答案