如何绘制外部画布并制作它的位图?

时间:2013-07-02 20:42:23

标签: android canvas view bitmap

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

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;
            }
        }

    }

所以基本上我想从所有行创建一个位图,即使行是在画布外绘制的。

1 个答案:

答案 0 :(得分:1)

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

Canvas myCanvas = new Canvas(myBitmap);