首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >保存大于物理屏幕的画布

保存大于物理屏幕的画布
EN

Stack Overflow用户
提问于 2022-03-22 11:02:06
回答 1查看 59关注 0票数 0

我动态地绘制一个图像,文本通过自定义视图,用户可以向上/向下移动文本,左/右也可以放大/缩小,但我想将整个视图保存为图像。我知道我可以保存图像,但我需要它的分辨率要高于实际的屏幕,我正在捕捉它。

我正在保存图像

代码语言:javascript
复制
                    File file = new File(imagePath);
                    try {
                        FileOutputStream out = new FileOutputStream(file, false);
                        if (parentView != null) {
                            parentView.setDrawingCacheEnabled(true);
                            Bitmap drawingCache = saveSettings.isTransparencyEnabled()
                                    ? BitmapUtil.removeTransparency(parentView.getDrawingCache())
                                    : parentView.getDrawingCache();
                            drawingCache.compress(saveSettings.getCompressFormat(), saveSettings.getCompressQuality(), out);
                        }
                        out.flush();
                        out.close();
                        Log.d(TAG, "Filed Saved Successfully");
                        return null;
                    } catch (Exception e) {
                        FirebaseCrashlytics.getInstance().recordException(e);
                        e.printStackTrace();
                        Log.d(TAG, "Failed to save File");
                        return e;
                    }

使用上述节省的功能,图像质量很差。我需要非常高分辨率的图像。

EN

回答 1

Stack Overflow用户

发布于 2022-03-22 12:39:35

请试试这条路可能对你有帮助。

代码语言:javascript
复制
class MyActivity extends Activity {
    private View rootLayoutContainerView;
    private int imageWidth;
    private int imageHeight;
    private int screenLayoutWidth;
    private int screenLayoutHeight;

    public final void nextToOverlay() {
        Bitmap result = Bitmap.createBitmap(this.screenLayoutWidth, this.screenLayoutHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        rootLayoutContainerView.draw(canvas);
        result = resizeBitmapFitXY(imageWidth, imageHeight, result);
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        result.compress(Bitmap.CompressFormat.PNG, 100, bytes);
        File file = getimagePath((Context)this, "framelayer");

        try {
            FileOutputStream fo = new FileOutputStream(file);
            fo.write(bytes.toByteArray());
            fo.flush();
            fo.close();
        } catch (IOException var6) {
            var6.printStackTrace();
        }

    }

    public final Bitmap resizeBitmapFitXY(int width, int height,Bitmap bitmap) {
        Bitmap background = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        float originalWidth = (float)bitmap.getWidth();
        float originalHeight = (float)bitmap.getHeight();
        Canvas canvas = new Canvas(background);
        float scale = 0.0F;
        float xTranslation = 0.0F;
        float yTranslation = 0.0F;
        if (originalWidth > originalHeight) {
            scale = (float)height / originalHeight;
            xTranslation = ((float)width - originalWidth * scale) / 2.0F;
        } else {
            scale = (float)width / originalWidth;
            yTranslation = ((float)height - originalHeight * scale) / 2.0F;
        }

        Matrix transformation = new Matrix();
        transformation.postTranslate(xTranslation, yTranslation);
        transformation.preScale(scale, scale);
        Paint paint = new Paint();
        paint.setFilterBitmap(true);
        canvas.drawBitmap(bitmap, transformation, paint);
        return background;
    }

    public File getimagePath(Context context,String name) {
        File mTranscodeOutputFile = null;
        try {
            File outputDir = new File(context.getExternalFilesDir(null), "image");
            if (!outputDir.exists()) {
                outputDir.mkdir();
            }

            mTranscodeOutputFile = new File(outputDir, name + ".jpg");
        } catch (Exception var5) {
        }
        return mTranscodeOutputFile;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71570954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档