Search in sources :

Example 1 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 2 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 3 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)

Example 4 with ColorFilter

use of android.graphics.ColorFilter in project ETSMobile-Android2 by ApplETS.

the class TodayWidgetProvider method setUpSyncBtn.

private void setUpSyncBtn(Context context, RemoteViews views, int textColor) {
    Intent intentRefresh = new Intent(context, TodayWidgetProvider.class);
    intentRefresh.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    intentRefresh.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetsIds(context));
    PendingIntent pendingIntentRefresh = PendingIntent.getBroadcast(context, 0, intentRefresh, PendingIntent.FLAG_UPDATE_CURRENT);
    Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_sync);
    // Copie mutable de l'icĂ´ne
    icon = icon.copy(Bitmap.Config.ARGB_8888, true);
    Paint paint = new Paint();
    ColorFilter filter = new PorterDuffColorFilter(textColor, PorterDuff.Mode.SRC_IN);
    paint.setColorFilter(filter);
    Canvas canvas = new Canvas(icon);
    canvas.drawBitmap(icon, 0, 0, paint);
    views.setImageViewBitmap(syncBtnId, icon);
    views.setInt(syncBtnId, "setBackgroundColor", Color.TRANSPARENT);
    views.setOnClickPendingIntent(syncBtnId, pendingIntentRefresh);
}
Also used : Bitmap(android.graphics.Bitmap) ColorFilter(android.graphics.ColorFilter) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter) Canvas(android.graphics.Canvas) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter) PendingIntent(android.app.PendingIntent) Paint(android.graphics.Paint)

Example 5 with ColorFilter

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

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)

Aggregations

ColorFilter (android.graphics.ColorFilter)75 PorterDuffColorFilter (android.graphics.PorterDuffColorFilter)26 LightingColorFilter (android.graphics.LightingColorFilter)25 Paint (android.graphics.Paint)25 Drawable (android.graphics.drawable.Drawable)23 Canvas (android.graphics.Canvas)14 FilterableStateListDrawable (com.simplecity.amp_library.ui.views.FilterableStateListDrawable)14 LayerDrawable (android.graphics.drawable.LayerDrawable)12 Bitmap (android.graphics.Bitmap)11 View (android.view.View)7 Test (org.junit.Test)7 Path (android.graphics.Path)5 Rect (android.graphics.Rect)5 RectF (android.graphics.RectF)5 ColorMatrixColorFilter (android.graphics.ColorMatrixColorFilter)4 ImageView (android.widget.ImageView)4 TextView (android.widget.TextView)4 Animator (android.animation.Animator)3 SuppressLint (android.annotation.SuppressLint)3 Intent (android.content.Intent)3