0

我正在绘制一些文本行。几行之后,它就离开了屏幕。我想要做的是在位图中捕获所有行(也是画布外的行)。

I have the code below which only works for within the canvas(screen).

    private class DeciderView extends View {

        private Paint paint;
        String text = "";
        String[] options = { "een", "twee", "drie", "vier", "vijf", "zes", "zeven", "acht",
                "negen", "tien", "elf", "twaalf", "dertien", "vertien", "vijftien" };

        public DeciderView(Context context) {
            super(context);
            // Keep screen on
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            // Remove title bar
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            // Remove notification bar
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            this.setBackgroundColor(Color.WHITE);
            paint = new Paint();
            paint.setTextSize(75);
            paint.setColor(Color.BLACK);
            paint.setStrokeWidth(10);
            paint.setAntiAlias(true);
            setDrawingCacheEnabled(true);

            setDrawingCacheQuality(DRAWING_CACHE_QUALITY_HIGH);

        }

        private void drawInput(Canvas canvas) {
            Paint p = new Paint();
            p.setTextAlign(Align.CENTER);
            p.setTextSize(canvas.getWidth() / 10f);
            p.setColor(Color.BLACK);

            float xText = canvas.getWidth() / 2f;

            float yText = (canvas.getHeight() / 4f);
            for (int i = 0; i < options.length; i++) {
                text += options[i] + "\n";
            }
            for (String line : text.split("\n")) {
                canvas.drawText(line, xText, yText, p);
                yText -= (p.ascent() + p.descent()) * 2.5f;

            }
        }


        @Override
        public void onDraw(Canvas canvas) {

            drawInput(canvas);

            this.setDrawingCacheEnabled(true);
            Bitmap b = Bitmap.createBitmap(this.getDrawingCache());
            b = Bitmap.createScaledBitmap(b, canvas.getWidth(), canvas.getHeight(), false);

            try {
                b.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(
                        new File(Environment.getExternalStorageDirectory().getPath()
                                + "/Pictures/aaaaa.jpg")));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            if (b != null) {
                b.recycle();
                b = null;
            }
        }

    }

所以基本上我想从所有行中制作一个位图,即使这些行是在画布之外绘制的。

4

1 回答 1

1

为什么不直接绘制位图?您可以创建一个有位图支持的新 Canvas。您使用普通的画布绘制命令,输出将被写入位图而不是屏幕。如果您还想将其绘制到屏幕上,只需在最后将新位图绘制到屏幕上即可。要做到这一点,您只需要

Canvas myCanvas = new Canvas(myBitmap);
于 2013-07-02T20:55:26.217 回答