mobile/android/base/toolbar/CanvasDelegate.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/toolbar/CanvasDelegate.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,76 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.toolbar;
     1.9 +
    1.10 +import android.graphics.Bitmap;
    1.11 +import android.graphics.Canvas;
    1.12 +import android.graphics.Paint;
    1.13 +import android.graphics.Path;
    1.14 +import android.graphics.PorterDuff.Mode;
    1.15 +import android.graphics.PorterDuffXfermode;
    1.16 +import android.graphics.Shader;
    1.17 +import android.os.Build;
    1.18 +
    1.19 +class CanvasDelegate {
    1.20 +    Paint mPaint;
    1.21 +    PorterDuffXfermode mMode;
    1.22 +    DrawManager mDrawManager;
    1.23 +
    1.24 +    // DrawManager would do a default draw of the background.
    1.25 +    static interface DrawManager {
    1.26 +        public void defaultDraw(Canvas cavas);
    1.27 +    }
    1.28 +
    1.29 +    CanvasDelegate(DrawManager drawManager, Mode mode) {
    1.30 +        mDrawManager = drawManager;
    1.31 +
    1.32 +        // DST_IN masks, DST_OUT clips.
    1.33 +        mMode = new PorterDuffXfermode(mode);
    1.34 +
    1.35 +        mPaint = new Paint();
    1.36 +        mPaint.setAntiAlias(true);
    1.37 +        mPaint.setColor(0xFFFF0000);
    1.38 +        mPaint.setStrokeWidth(0.0f);
    1.39 +    }
    1.40 +
    1.41 +    void draw(Canvas canvas, Path path, int width, int height) {
    1.42 +        // Save the canvas. All PorterDuff operations should be done in a offscreen bitmap.
    1.43 +        int count = canvas.saveLayer(0, 0, width, height, null,
    1.44 +                                     Canvas.MATRIX_SAVE_FLAG |
    1.45 +                                     Canvas.CLIP_SAVE_FLAG |
    1.46 +                                     Canvas.HAS_ALPHA_LAYER_SAVE_FLAG |
    1.47 +                                     Canvas.FULL_COLOR_LAYER_SAVE_FLAG |
    1.48 +                                     Canvas.CLIP_TO_LAYER_SAVE_FLAG);
    1.49 +
    1.50 +        // Do a default draw.
    1.51 +        mDrawManager.defaultDraw(canvas);
    1.52 +
    1.53 +        if (path != null && !path.isEmpty()) {
    1.54 +            // ICS added double-buffering, which made it easier for drawing the Path directly over the DST.
    1.55 +            // In pre-ICS, drawPath() doesn't seem to use ARGB_8888 mode for performance, hence transparency is not preserved.
    1.56 +            if (Build.VERSION.SDK_INT >= 14) {
    1.57 +                mPaint.setXfermode(mMode);
    1.58 +                canvas.drawPath(path, mPaint);
    1.59 +            } else {
    1.60 +                // Allocate a bitmap and draw the masking/clipping path.
    1.61 +                Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    1.62 +                (new Canvas(bitmap)).drawPath(path, mPaint);
    1.63 +
    1.64 +                mPaint.setXfermode(mMode);
    1.65 +                canvas.drawBitmap(bitmap, 0, 0, mPaint);
    1.66 +                bitmap.recycle();
    1.67 +
    1.68 +                mPaint.setXfermode(null);
    1.69 +            }
    1.70 +        }
    1.71 +
    1.72 +        // Restore the canvas.
    1.73 +        canvas.restoreToCount(count);
    1.74 +    }
    1.75 +
    1.76 +    void setShader(Shader shader) {
    1.77 +        mPaint.setShader(shader);
    1.78 +    }
    1.79 +}

mercurial