Search in sources :

Example 26 with ColorFilter

use of android.graphics.ColorFilter in project android_frameworks_base by AOSPA.

the class GradientDrawable method draw.

@Override
public void draw(Canvas canvas) {
    if (!ensureValidRect()) {
        // nothing to draw
        return;
    }
    // remember the alpha values, in case we temporarily overwrite them
    // when we modulate them with mAlpha
    final int prevFillAlpha = mFillPaint.getAlpha();
    final int prevStrokeAlpha = mStrokePaint != null ? mStrokePaint.getAlpha() : 0;
    // compute the modulate alpha values
    final int currFillAlpha = modulateAlpha(prevFillAlpha);
    final int currStrokeAlpha = modulateAlpha(prevStrokeAlpha);
    final boolean haveStroke = currStrokeAlpha > 0 && mStrokePaint != null && mStrokePaint.getStrokeWidth() > 0;
    final boolean haveFill = currFillAlpha > 0;
    final GradientState st = mGradientState;
    final ColorFilter colorFilter = mColorFilter != null ? mColorFilter : mTintFilter;
    /*  we need a layer iff we're drawing both a fill and stroke, and the
            stroke is non-opaque, and our shapetype actually supports
            fill+stroke. Otherwise we can just draw the stroke (if any) on top
            of the fill (if any) without worrying about blending artifacts.
         */
    final boolean useLayer = haveStroke && haveFill && st.mShape != LINE && currStrokeAlpha < 255 && (mAlpha < 255 || colorFilter != null);
    /*  Drawing with a layer is slower than direct drawing, but it
            allows us to apply paint effects like alpha and colorfilter to
            the result of multiple separate draws. In our case, if the user
            asks for a non-opaque alpha value (via setAlpha), and we're
            stroking, then we need to apply the alpha AFTER we've drawn
            both the fill and the stroke.
        */
    if (useLayer) {
        if (mLayerPaint == null) {
            mLayerPaint = new Paint();
        }
        mLayerPaint.setDither(st.mDither);
        mLayerPaint.setAlpha(mAlpha);
        mLayerPaint.setColorFilter(colorFilter);
        float rad = mStrokePaint.getStrokeWidth();
        canvas.saveLayer(mRect.left - rad, mRect.top - rad, mRect.right + rad, mRect.bottom + rad, mLayerPaint);
        // don't perform the filter in our individual paints
        // since the layer will do it for us
        mFillPaint.setColorFilter(null);
        mStrokePaint.setColorFilter(null);
    } else {
        /*  if we're not using a layer, apply the dither/filter to our
                individual paints
            */
        mFillPaint.setAlpha(currFillAlpha);
        mFillPaint.setDither(st.mDither);
        mFillPaint.setColorFilter(colorFilter);
        if (colorFilter != null && st.mSolidColors == null) {
            mFillPaint.setColor(mAlpha << 24);
        }
        if (haveStroke) {
            mStrokePaint.setAlpha(currStrokeAlpha);
            mStrokePaint.setDither(st.mDither);
            mStrokePaint.setColorFilter(colorFilter);
        }
    }
    switch(st.mShape) {
        case RECTANGLE:
            if (st.mRadiusArray != null) {
                buildPathIfDirty();
                canvas.drawPath(mPath, mFillPaint);
                if (haveStroke) {
                    canvas.drawPath(mPath, mStrokePaint);
                }
            } else if (st.mRadius > 0.0f) {
                // since the caller is only giving us 1 value, we will force
                // it to be square if the rect is too small in one dimension
                // to show it. If we did nothing, Skia would clamp the rad
                // independently along each axis, giving us a thin ellipse
                // if the rect were very wide but not very tall
                float rad = Math.min(st.mRadius, Math.min(mRect.width(), mRect.height()) * 0.5f);
                canvas.drawRoundRect(mRect, rad, rad, mFillPaint);
                if (haveStroke) {
                    canvas.drawRoundRect(mRect, rad, rad, mStrokePaint);
                }
            } else {
                if (mFillPaint.getColor() != 0 || colorFilter != null || mFillPaint.getShader() != null) {
                    canvas.drawRect(mRect, mFillPaint);
                }
                if (haveStroke) {
                    canvas.drawRect(mRect, mStrokePaint);
                }
            }
            break;
        case OVAL:
            canvas.drawOval(mRect, mFillPaint);
            if (haveStroke) {
                canvas.drawOval(mRect, mStrokePaint);
            }
            break;
        case LINE:
            {
                RectF r = mRect;
                float y = r.centerY();
                if (haveStroke) {
                    canvas.drawLine(r.left, y, r.right, y, mStrokePaint);
                }
                break;
            }
        case RING:
            Path path = buildRing(st);
            canvas.drawPath(path, mFillPaint);
            if (haveStroke) {
                canvas.drawPath(path, mStrokePaint);
            }
            break;
    }
    if (useLayer) {
        canvas.restore();
    } else {
        mFillPaint.setAlpha(prevFillAlpha);
        if (haveStroke) {
            mStrokePaint.setAlpha(prevStrokeAlpha);
        }
    }
}
Also used : RectF(android.graphics.RectF) Path(android.graphics.Path) ColorFilter(android.graphics.ColorFilter) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 27 with ColorFilter

use of android.graphics.ColorFilter in project android_frameworks_base by ResurrectionRemix.

the class GradientDrawable method draw.

@Override
public void draw(Canvas canvas) {
    if (!ensureValidRect()) {
        // nothing to draw
        return;
    }
    // remember the alpha values, in case we temporarily overwrite them
    // when we modulate them with mAlpha
    final int prevFillAlpha = mFillPaint.getAlpha();
    final int prevStrokeAlpha = mStrokePaint != null ? mStrokePaint.getAlpha() : 0;
    // compute the modulate alpha values
    final int currFillAlpha = modulateAlpha(prevFillAlpha);
    final int currStrokeAlpha = modulateAlpha(prevStrokeAlpha);
    final boolean haveStroke = currStrokeAlpha > 0 && mStrokePaint != null && mStrokePaint.getStrokeWidth() > 0;
    final boolean haveFill = currFillAlpha > 0;
    final GradientState st = mGradientState;
    final ColorFilter colorFilter = mColorFilter != null ? mColorFilter : mTintFilter;
    /*  we need a layer iff we're drawing both a fill and stroke, and the
            stroke is non-opaque, and our shapetype actually supports
            fill+stroke. Otherwise we can just draw the stroke (if any) on top
            of the fill (if any) without worrying about blending artifacts.
         */
    final boolean useLayer = haveStroke && haveFill && st.mShape != LINE && currStrokeAlpha < 255 && (mAlpha < 255 || colorFilter != null);
    /*  Drawing with a layer is slower than direct drawing, but it
            allows us to apply paint effects like alpha and colorfilter to
            the result of multiple separate draws. In our case, if the user
            asks for a non-opaque alpha value (via setAlpha), and we're
            stroking, then we need to apply the alpha AFTER we've drawn
            both the fill and the stroke.
        */
    if (useLayer) {
        if (mLayerPaint == null) {
            mLayerPaint = new Paint();
        }
        mLayerPaint.setDither(st.mDither);
        mLayerPaint.setAlpha(mAlpha);
        mLayerPaint.setColorFilter(colorFilter);
        float rad = mStrokePaint.getStrokeWidth();
        canvas.saveLayer(mRect.left - rad, mRect.top - rad, mRect.right + rad, mRect.bottom + rad, mLayerPaint);
        // don't perform the filter in our individual paints
        // since the layer will do it for us
        mFillPaint.setColorFilter(null);
        mStrokePaint.setColorFilter(null);
    } else {
        /*  if we're not using a layer, apply the dither/filter to our
                individual paints
            */
        mFillPaint.setAlpha(currFillAlpha);
        mFillPaint.setDither(st.mDither);
        mFillPaint.setColorFilter(colorFilter);
        if (colorFilter != null && st.mSolidColors == null) {
            mFillPaint.setColor(mAlpha << 24);
        }
        if (haveStroke) {
            mStrokePaint.setAlpha(currStrokeAlpha);
            mStrokePaint.setDither(st.mDither);
            mStrokePaint.setColorFilter(colorFilter);
        }
    }
    switch(st.mShape) {
        case RECTANGLE:
            if (st.mRadiusArray != null) {
                buildPathIfDirty();
                canvas.drawPath(mPath, mFillPaint);
                if (haveStroke) {
                    canvas.drawPath(mPath, mStrokePaint);
                }
            } else if (st.mRadius > 0.0f) {
                // since the caller is only giving us 1 value, we will force
                // it to be square if the rect is too small in one dimension
                // to show it. If we did nothing, Skia would clamp the rad
                // independently along each axis, giving us a thin ellipse
                // if the rect were very wide but not very tall
                float rad = Math.min(st.mRadius, Math.min(mRect.width(), mRect.height()) * 0.5f);
                canvas.drawRoundRect(mRect, rad, rad, mFillPaint);
                if (haveStroke) {
                    canvas.drawRoundRect(mRect, rad, rad, mStrokePaint);
                }
            } else {
                if (mFillPaint.getColor() != 0 || colorFilter != null || mFillPaint.getShader() != null) {
                    canvas.drawRect(mRect, mFillPaint);
                }
                if (haveStroke) {
                    canvas.drawRect(mRect, mStrokePaint);
                }
            }
            break;
        case OVAL:
            canvas.drawOval(mRect, mFillPaint);
            if (haveStroke) {
                canvas.drawOval(mRect, mStrokePaint);
            }
            break;
        case LINE:
            {
                RectF r = mRect;
                float y = r.centerY();
                if (haveStroke) {
                    canvas.drawLine(r.left, y, r.right, y, mStrokePaint);
                }
                break;
            }
        case RING:
            Path path = buildRing(st);
            canvas.drawPath(path, mFillPaint);
            if (haveStroke) {
                canvas.drawPath(path, mStrokePaint);
            }
            break;
    }
    if (useLayer) {
        canvas.restore();
    } else {
        mFillPaint.setAlpha(prevFillAlpha);
        if (haveStroke) {
            mStrokePaint.setAlpha(prevStrokeAlpha);
        }
    }
}
Also used : RectF(android.graphics.RectF) Path(android.graphics.Path) ColorFilter(android.graphics.ColorFilter) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 28 with ColorFilter

use of android.graphics.ColorFilter in project android_frameworks_base by ResurrectionRemix.

the class VectorDrawable method draw.

@Override
public void draw(Canvas canvas) {
    // We will offset the bounds for drawBitmap, so copyBounds() here instead
    // of getBounds().
    copyBounds(mTmpBounds);
    if (mTmpBounds.width() <= 0 || mTmpBounds.height() <= 0) {
        // Nothing to draw
        return;
    }
    // Color filters always override tint filters.
    final ColorFilter colorFilter = (mColorFilter == null ? mTintFilter : mColorFilter);
    final long colorFilterNativeInstance = colorFilter == null ? 0 : colorFilter.native_instance;
    boolean canReuseCache = mVectorState.canReuseCache();
    int pixelCount = nDraw(mVectorState.getNativeRenderer(), canvas.getNativeCanvasWrapper(), colorFilterNativeInstance, mTmpBounds, needMirroring(), canReuseCache);
    if (pixelCount == 0) {
        // cache, if any.
        return;
    }
    int deltaInBytes;
    // type, only one bitmap cache is allocated.
    if (canvas.isHardwareAccelerated()) {
        // Each pixel takes 4 bytes.
        deltaInBytes = (pixelCount - mVectorState.mLastHWCachePixelCount) * 4;
        mVectorState.mLastHWCachePixelCount = pixelCount;
    } else {
        // Each pixel takes 4 bytes.
        deltaInBytes = (pixelCount - mVectorState.mLastSWCachePixelCount) * 4;
        mVectorState.mLastSWCachePixelCount = pixelCount;
    }
    if (deltaInBytes > 0) {
        VMRuntime.getRuntime().registerNativeAllocation(deltaInBytes);
    } else if (deltaInBytes < 0) {
        VMRuntime.getRuntime().registerNativeFree(-deltaInBytes);
    }
}
Also used : ColorFilter(android.graphics.ColorFilter) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter)

Example 29 with ColorFilter

use of android.graphics.ColorFilter in project MagicaSakura by Bilibili.

the class StateListDrawableInflateImpl method inflateDrawable.

@Override
public Drawable inflateDrawable(Context context, XmlPullParser parser, AttributeSet attrs) throws IOException, XmlPullParserException {
    StateListDrawable sd = null;
    ArrayList<int[]> states = new ArrayList<>();
    ArrayList<Drawable> drawables = new ArrayList<>();
    SparseArray<ColorFilter> mColorFilterMap = null;
    final int innerDepth = parser.getDepth() + 1;
    int type;
    int depth;
    while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
        if (type != XmlPullParser.START_TAG) {
            continue;
        }
        if (depth > innerDepth || !parser.getName().equals("item")) {
            continue;
        }
        Drawable dr = DrawableUtils.getAttrDrawable(context, attrs, android.R.attr.drawable);
        states.add(DrawableUtils.extractStateSet(attrs));
        // attributes and extracting states.
        if (dr == null) {
            while ((type = parser.next()) == XmlPullParser.TEXT) {
            }
            if (type != XmlPullParser.START_TAG) {
                throw new XmlPullParserException(parser.getPositionDescription() + ": <item> tag requires a 'drawable' attribute or " + "child tag defining a drawable");
            }
            dr = DrawableUtils.createFromXmlInner(context, parser, attrs);
        } else {
            ColorFilter colorFilter = DrawableUtils.getAttrColorFilter(context, attrs, R.attr.drawableTint, R.attr.drawableTintMode);
            if (colorFilter != null) {
                if (mColorFilterMap == null) {
                    mColorFilterMap = new SparseArray<>();
                }
                mColorFilterMap.put(drawables.size(), colorFilter);
            }
        }
        drawables.add(dr);
    }
    if (states.size() >= 1) {
        if (mColorFilterMap != null) {
            sd = new FilterableStateListDrawable();
            for (int i = 0; i < states.size(); i++) {
                ((FilterableStateListDrawable) sd).addState(states.get(i), drawables.get(i), mColorFilterMap.get(i));
            }
        } else {
            sd = new StateListDrawable();
            for (int i = 0; i < states.size(); i++) {
                sd.addState(states.get(i), drawables.get(i));
            }
        }
    }
    return sd;
}
Also used : FilterableStateListDrawable(com.bilibili.magicasakura.drawables.FilterableStateListDrawable) ColorFilter(android.graphics.ColorFilter) ArrayList(java.util.ArrayList) StateListDrawable(android.graphics.drawable.StateListDrawable) Drawable(android.graphics.drawable.Drawable) FilterableStateListDrawable(com.bilibili.magicasakura.drawables.FilterableStateListDrawable) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) StateListDrawable(android.graphics.drawable.StateListDrawable) FilterableStateListDrawable(com.bilibili.magicasakura.drawables.FilterableStateListDrawable)

Example 30 with ColorFilter

use of android.graphics.ColorFilter in project Shuttle by timusus.

the class DrawableUtils method getColoredStateListDrawable.

/**
     * Returns a {@link FilterableStateListDrawable}, coloring the passed in
     * drawable according to the theme and the passed in highlight color
     *
     * @param baseDrawableResId the drawable to use
     * @return an {@link FilterableStateListDrawable}, coloring the passed in
     * drawable according to the theme and the passed in highlight color
     */
public static Drawable getColoredStateListDrawable(Context context, int baseDrawableResId) {
    Drawable baseDrawable = context.getResources().getDrawable(baseDrawableResId);
    Drawable highlightDrawable = baseDrawable.getConstantState().newDrawable();
    int baseColor;
    if ((ThemeUtils.getInstance().themeType == ThemeUtils.ThemeType.TYPE_DARK) || (ThemeUtils.getInstance().themeType == ThemeUtils.ThemeType.TYPE_SOLID_DARK) || (ThemeUtils.getInstance().themeType == ThemeUtils.ThemeType.TYPE_SOLID_BLACK)) {
        baseColor = context.getResources().getColor(R.color.drawable_base_color_dark);
    } else {
        baseColor = context.getResources().getColor(R.color.drawable_base_color_light);
    }
    ColorFilter baseColorFilter = new LightingColorFilter(baseColor, 0);
    ColorFilter highlightColorFilter = new LightingColorFilter(ColorUtils.getAccentColor(), 0);
    FilterableStateListDrawable filterableStateListDrawable = new FilterableStateListDrawable();
    filterableStateListDrawable.addState(new int[] { android.R.attr.state_pressed }, baseDrawable, highlightColorFilter);
    filterableStateListDrawable.addState(StateSet.WILD_CARD, highlightDrawable, baseColorFilter);
    return filterableStateListDrawable;
}
Also used : FilterableStateListDrawable(com.simplecity.amp_library.ui.views.FilterableStateListDrawable) ColorFilter(android.graphics.ColorFilter) LightingColorFilter(android.graphics.LightingColorFilter) LayerDrawable(android.graphics.drawable.LayerDrawable) FilterableStateListDrawable(com.simplecity.amp_library.ui.views.FilterableStateListDrawable) Drawable(android.graphics.drawable.Drawable) LightingColorFilter(android.graphics.LightingColorFilter)

Aggregations

ColorFilter (android.graphics.ColorFilter)50 LightingColorFilter (android.graphics.LightingColorFilter)21 PorterDuffColorFilter (android.graphics.PorterDuffColorFilter)17 Drawable (android.graphics.drawable.Drawable)17 FilterableStateListDrawable (com.simplecity.amp_library.ui.views.FilterableStateListDrawable)16 Paint (android.graphics.Paint)14 LayerDrawable (android.graphics.drawable.LayerDrawable)14 Path (android.graphics.Path)5 RectF (android.graphics.RectF)5 Test (org.junit.Test)5 Bitmap (android.graphics.Bitmap)4 Canvas (android.graphics.Canvas)4 Rect (android.graphics.Rect)3 Shader (android.graphics.Shader)3 BitmapDrawable (android.graphics.drawable.BitmapDrawable)2 StateListDrawable (android.graphics.drawable.StateListDrawable)2 View (android.view.View)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 FilterableStateListDrawable (com.bilibili.magicasakura.drawables.FilterableStateListDrawable)2