mobile/android/base/LightweightThemeDrawable.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/LightweightThemeDrawable.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,128 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko;
    1.10 +
    1.11 +import android.content.res.Resources;
    1.12 +import android.graphics.Bitmap;
    1.13 +import android.graphics.BitmapShader;
    1.14 +import android.graphics.Canvas;
    1.15 +import android.graphics.ColorFilter;
    1.16 +import android.graphics.ComposeShader;
    1.17 +import android.graphics.LinearGradient;
    1.18 +import android.graphics.Paint;
    1.19 +import android.graphics.PixelFormat;
    1.20 +import android.graphics.PorterDuff;
    1.21 +import android.graphics.PorterDuffColorFilter;
    1.22 +import android.graphics.Rect;
    1.23 +import android.graphics.Shader;
    1.24 +import android.graphics.drawable.Drawable;
    1.25 +
    1.26 +/**
    1.27 + * A special drawable used with lightweight themes. This draws a color 
    1.28 + * (with an optional color-filter) and a bitmap (with a linear gradient 
    1.29 + * to specify the alpha) in order.
    1.30 + */
    1.31 +public class LightweightThemeDrawable extends Drawable {
    1.32 +    private Paint mPaint;
    1.33 +    private Paint mColorPaint;
    1.34 +
    1.35 +    private Bitmap mBitmap;
    1.36 +    private Resources mResources;
    1.37 +
    1.38 +    private int mStartColor;
    1.39 +    private int mEndColor;
    1.40 +
    1.41 +    public LightweightThemeDrawable(Resources resources, Bitmap bitmap) {
    1.42 +        mBitmap = bitmap;
    1.43 +        mResources = resources;
    1.44 +
    1.45 +        mPaint = new Paint();
    1.46 +        mPaint.setAntiAlias(true);
    1.47 +        mPaint.setStrokeWidth(0.0f);
    1.48 +    }
    1.49 +
    1.50 +    @Override
    1.51 +    protected void onBoundsChange(Rect bounds) {
    1.52 +        super.onBoundsChange(bounds);
    1.53 +        initializeBitmapShader();
    1.54 +    }
    1.55 +
    1.56 +    @Override
    1.57 +    public void draw(Canvas canvas) {
    1.58 +        // Draw the colors, if available.
    1.59 +        if (mColorPaint != null)
    1.60 +            canvas.drawPaint(mColorPaint);
    1.61 +
    1.62 +        // Draw the bitmap.
    1.63 +        canvas.drawPaint(mPaint);
    1.64 +    }
    1.65 +
    1.66 +    @Override
    1.67 +    public int getOpacity() {
    1.68 +        return PixelFormat.TRANSLUCENT;
    1.69 +    }
    1.70 +
    1.71 +    @Override
    1.72 +    public void setAlpha(int alpha) {
    1.73 +        // A StateListDrawable will reset the alpha value with 255.
    1.74 +        // We cannot use to be the bitmap alpha.
    1.75 +        mPaint.setAlpha(alpha);
    1.76 +    }
    1.77 +
    1.78 +    @Override
    1.79 +    public void setColorFilter(ColorFilter filter) {
    1.80 +        mPaint.setColorFilter(filter);
    1.81 +    }		
    1.82 +
    1.83 +    /**
    1.84 +     * Creates a paint that paint a particular color.
    1.85 +     *
    1.86 +     * @param color The color to be painted.
    1.87 +     */
    1.88 +    public void setColor(int color) {
    1.89 +        mColorPaint = new Paint(mPaint);
    1.90 +        mColorPaint.setColor(color);
    1.91 +    }
    1.92 +
    1.93 +    /**
    1.94 +     * Creates a paint that paint a particular color, and a filter for the color.
    1.95 +     *
    1.96 +     * @param color The color to be painted.
    1.97 +     * @param filter The filter color to be applied using SRC_OVER mode.
    1.98 +     */
    1.99 +    public void setColorWithFilter(int color, int filter) {
   1.100 +        mColorPaint = new Paint(mPaint);
   1.101 +        mColorPaint.setColor(color);
   1.102 +        mColorPaint.setColorFilter(new PorterDuffColorFilter(filter, PorterDuff.Mode.SRC_OVER));
   1.103 +    }
   1.104 +
   1.105 +    /**
   1.106 +     * Set the alpha for the linear gradient used with the bitmap's shader.
   1.107 +     *
   1.108 +     * @param startAlpha The starting alpha (0..255) value to be applied to the LinearGradient.
   1.109 +     * @param startAlpha The ending alpha (0..255) value to be applied to the LinearGradient.
   1.110 +     */
   1.111 +    public void setAlpha(int startAlpha, int endAlpha) {
   1.112 +        mStartColor = startAlpha << 24;
   1.113 +        mEndColor = endAlpha << 24;
   1.114 +        initializeBitmapShader();
   1.115 +    }
   1.116 +
   1.117 +    private void initializeBitmapShader() {
   1.118 +	// A bitmap-shader to draw the bitmap.
   1.119 +        // Clamp mode will repeat the last row of pixels.
   1.120 +        // Hence its better to have an endAlpha of 0 for the linear-gradient.
   1.121 +	BitmapShader bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
   1.122 +
   1.123 +	// A linear-gradient to specify the opacity of the bitmap.
   1.124 +	LinearGradient gradient = new LinearGradient(0, 0, 0, mBitmap.getHeight(), mStartColor, mEndColor, Shader.TileMode.CLAMP);
   1.125 +
   1.126 +	// Make a combined shader -- a performance win.
   1.127 +        // The linear-gradient is the 'SRC' and the bitmap-shader is the 'DST'.
   1.128 +	// Drawing the DST in the SRC will provide the opacity.
   1.129 +	mPaint.setShader(new ComposeShader(bitmapShader, gradient, PorterDuff.Mode.DST_IN));
   1.130 +    }
   1.131 +}

mercurial