Search in sources :

Example 31 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 32 with ColorFilter

use of android.graphics.ColorFilter in project vector-compat by wnafee.

the class VectorDrawable method draw.

@Override
public void draw(Canvas canvas) {
    final Rect bounds = getBounds();
    if (bounds.width() == 0 || bounds.height() == 0) {
        // too small to draw
        return;
    }
    final int saveCount = canvas.save();
    final boolean needMirroring = needMirroring();
    canvas.translate(bounds.left, bounds.top);
    if (needMirroring) {
        canvas.translate(bounds.width(), 0);
        canvas.scale(-1.0f, 1.0f);
    }
    // Color filters always override tint filters.
    final ColorFilter colorFilter = mColorFilter == null ? mTintFilter : mColorFilter;
    if (!mAllowCaching) {
        // AnimatedVectorDrawable
        if (!mVectorState.hasTranslucentRoot()) {
            mVectorState.mVPathRenderer.draw(canvas, bounds.width(), bounds.height(), colorFilter);
        } else {
            mVectorState.createCachedBitmapIfNeeded(bounds);
            mVectorState.updateCachedBitmap(bounds);
            mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter);
        }
    } else {
        // Static Vector Drawable case.
        mVectorState.createCachedBitmapIfNeeded(bounds);
        if (!mVectorState.canReuseCache()) {
            mVectorState.updateCachedBitmap(bounds);
            mVectorState.updateCacheStates();
        }
        mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter);
    }
    canvas.restoreToCount(saveCount);
}
Also used : Rect(android.graphics.Rect) ColorFilter(android.graphics.ColorFilter) PorterDuffColorFilter(android.graphics.PorterDuffColorFilter) Paint(android.graphics.Paint)

Example 33 with ColorFilter

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

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 34 with ColorFilter

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

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 35 with ColorFilter

use of android.graphics.ColorFilter in project glide by bumptech.

the class GifDrawableTest method testSetColorFilterSetsColorFilterOnPaint.

@Test
public void testSetColorFilterSetsColorFilterOnPaint() {
    ColorFilter colorFilter = new ColorFilter();
    drawable.setColorFilter(colorFilter);
    verify(paint).setColorFilter(eq(colorFilter));
}
Also used : ColorFilter(android.graphics.ColorFilter) Test(org.junit.Test)

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