@Override public void setColorFilter(ColorFilter cf) { this.mColorFilter = cf; mFstHalfPaint.setColorFilter(cf); mScndHalfPaint.setColorFilter(cf); mAbovePaint.setColorFilter(cf); }
@Override public void setColorFilter(ColorFilter colorFilter) { mArcPaint.setColorFilter(colorFilter); mBackgroundArcPaint.setColorFilter(colorFilter); mForegroundCirclePaint.setColorFilter(colorFilter); mTextPaint.setColorFilter(colorFilter); }
@Override public void setColorFilter(ColorFilter cf) { mColorFilter = cf; mPaint1.setColorFilter(cf); mPaint2.setColorFilter(cf); mPaint3.setColorFilter(cf); mPaint4.setColorFilter(cf); }
@Override public void onDraw(Canvas canvas) { // Don't draw anything without an image if (image == null) return; // Nothing to draw (Empty bounds) if (image.getHeight() == 0 || image.getWidth() == 0) return; // Compare canvas sizes int oldCanvasSize = canvasSize; canvasSize = canvas.getWidth(); if (canvas.getHeight() < canvasSize) canvasSize = canvas.getHeight(); // Reinitialize shader, if necessary if (oldCanvasSize != canvasSize) refreshBitmapShader(); // Apply shader to paint paint.setShader(shader); // Keep track of selectorStroke/border width int outerWidth = 0; // Get the exact X/Y axis of the view int center = canvasSize / 2; if (hasSelector && isSelected) { // Draw the selector stroke & apply the selector filter, if applicable outerWidth = selectorStrokeWidth; center = (canvasSize - (outerWidth * 2)) / 2; paint.setColorFilter(selectorFilter); canvas.drawCircle( center + outerWidth, center + outerWidth, ((canvasSize - (outerWidth * 2)) / 2) + outerWidth - 4.0f, paintSelectorBorder); } else if (hasBorder) { // If no selector was drawn, draw a border and clear the filter // instead... if enabled outerWidth = borderWidth; center = (canvasSize - (outerWidth * 2)) / 2; paint.setColorFilter(null); canvas.drawCircle( center + outerWidth, center + outerWidth, ((canvasSize - (outerWidth * 2)) / 2) + outerWidth - 4.0f, paintBorder); } else // Clear the color filter if no selector nor border were drawn paint.setColorFilter(null); // Draw the circular image itself canvas.drawCircle( center + outerWidth, center + outerWidth, ((canvasSize - (outerWidth * 2)) / 2) - 4.0f, paint); }
public void setColor(int color) { if (mPaint == null) { mPaint = new Paint(Paint.FILTER_BITMAP_FLAG); } if (color != 0) { mPaint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP)); } else { mPaint.setColorFilter(null); } invalidate(); }
private void setColorFilter(float percent) { if (percent != 1) { PorterDuffColorFilter filter = new PorterDuffColorFilter( addAlphaPercentToColor((int) (80 - 80 * percent), Color.BLACK), PorterDuff.Mode.DARKEN); mPaintBg.setColorFilter(filter); } else { mPaintBg.setColorFilter(null); } }
private void initRendering(int mode) { Paint paint = new Paint(); switch (mode) { case 0: { // Default paint.setColorFilter(null); break; } case 1: { // Grayscale ColorMatrix matrix = new ColorMatrix(); matrix.setSaturation(0); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix); paint.setColorFilter(filter); break; } case 2: { // Inverted ColorMatrixColorFilter filter = new ColorMatrixColorFilter(NEGATIVE_COLOR); paint.setColorFilter(filter); break; } case 3: { // Inverted grayscale ColorMatrix matrix = new ColorMatrix(); matrix.set(NEGATIVE_COLOR); ColorMatrix gcm = new ColorMatrix(); gcm.setSaturation(0); ColorMatrix concat = new ColorMatrix(); concat.setConcat(matrix, gcm); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(concat); paint.setColorFilter(filter); break; } default: { paint.setColorFilter(null); break; } } // maybe sometime LAYER_TYPE_NONE would better? setLayerType(View.LAYER_TYPE_HARDWARE, paint); }
@Override public void draw(Canvas canvas) { Rect bounds = getBounds(); if (bounds.width() == 0 || bounds.height() == 0) { return; } if (mPaint == null) { mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setColor(Color.BLACK); onPreparePaint(mPaint); } mPaint.setAlpha(mAlpha); ColorFilter colorFilter = mColorFilter != null ? mColorFilter : mTintFilter; mPaint.setColorFilter(colorFilter); int saveCount = canvas.save(); canvas.translate(bounds.left, bounds.top); if (needMirroring()) { canvas.translate(bounds.width(), 0); canvas.scale(-1, 1); } onDraw(canvas, bounds.width(), bounds.height(), mPaint); canvas.restoreToCount(saveCount); }
private Bitmap tintImage(Bitmap image, Bitmap image2, KrollDict args) { String col = args.optString("color", ""); String mod1 = args.optString("modeColor", "multiply"); String mod2 = args.optString("modeImage", "multiply"); Boolean grad = args.optBoolean("vignette", false); if (image != null) { Mode filterMode1 = getFilter(mod1); Mode filterMode2 = getFilter(mod2); int width = image.getWidth(); int height = image.getHeight(); Bitmap workingBitmap = Bitmap.createScaledBitmap(image, width, height, true); Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(mutableBitmap); Bitmap resultBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas2 = new Canvas(resultBitmap); // add second image if (image2 != null) { Paint Compose = new Paint(); Compose.setXfermode( new PorterDuffXfermode(filterMode2)); // KAI: fixed error in the original code canvas.drawBitmap(image2, 0, 0, Compose); } Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // add color filter if (col != "") { PorterDuffColorFilter cf = new PorterDuffColorFilter( Color.parseColor(col), filterMode1); // KAI: fixed error in the original code paint.setColorFilter(cf); } // gradient if (grad) { int[] Colors = {0x00000000, 0xFF000000}; float[] ColorPosition = {0.10f, 0.99f}; RadialGradient gradient = new RadialGradient( width / 2, height / 2, width - width / 2, Colors, ColorPosition, android.graphics.Shader.TileMode.CLAMP); paint.setDither(true); paint.setShader(gradient); } canvas2.drawBitmap(mutableBitmap, 0, 0, paint); return resultBitmap; } return null; }
/** * 图像调整 * * @param bm 默认不可修改 * @param hue 色相 * @param saturation 饱和度 * @param lum 亮度 * @return */ public static Bitmap handleImageEffect(Bitmap bm, float hue, float saturation, float lum) { Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmp); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); ColorMatrix hueMatrix = new ColorMatrix(); hueMatrix.setRotate(0, hue); hueMatrix.setRotate(1, hue); hueMatrix.setRotate(2, hue); ColorMatrix saturationMatrix = new ColorMatrix(); saturationMatrix.setSaturation(saturation); ColorMatrix lumMatrix = new ColorMatrix(); lumMatrix.setScale(lum, lum, lum, 1); ColorMatrix imageMatrix = new ColorMatrix(); imageMatrix.postConcat(hueMatrix); imageMatrix.postConcat(saturationMatrix); imageMatrix.postConcat(lumMatrix); paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix)); canvas.drawBitmap(bm, 0, 0, paint); return bmp; }
public static Bitmap setTint(Bitmap sourceBitmap, int color) { try { Bitmap resultBitmap = Bitmap.createBitmap( sourceBitmap.getWidth(), sourceBitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(resultBitmap); float r = ((color >> 16) & 0xFF) / 255.0f; float g = ((color >> 8) & 0xFF) / 255.0f; float b = ((color >> 0) & 0xFF) / 255.0f; float[] colorTransform = {0, r, 0, 0, 0, 0, 0, g, 0, 0, 0, 0, 0, b, 0, 0, 0, 0, 1f, 0}; ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0f); colorMatrix.set(colorTransform); ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix); Paint paint = new Paint(); paint.setColorFilter(colorFilter); canvas.drawBitmap(sourceBitmap, 0, 0, paint); return resultBitmap; } catch (IllegalArgumentException ex) { return null; } }
public synchronized void saveEffect() { if (saveScreenCopy != null) { saveScreenCopy.recycle(); saveScreenCopy = null; } saveScreenCopy = Bitmap.createBitmap( (int) (windowWidth * windowScale / 2), (int) (windowHeight * windowScale / 2), Config.RGB_565); Canvas c = new Canvas(saveScreenCopy); saveEffectColorMatrix.setSaturation(0.1f); ColorMatrixColorFilter f = new ColorMatrixColorFilter(saveEffectColorMatrix); Paint p = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); p.setColorFilter(f); c.scale(windowScale * 0.5f, windowScale * 0.5f); c.drawBitmap(screen, 0, 0, p); saveEffectRotation = (float) (10f + Math.random() * 20f); // 10 to 20 degrees if (Math.random() < 0.5f) saveEffectRotation = -saveEffectRotation; // saveScreenCopy = screen.copy(Config.RGB_565, false); saveEffectTimer = saveEffectDuration; }
/** For screensavers to dim the lights if necessary. */ public static void dimClockView(boolean dim, View clockView) { Paint paint = new Paint(); paint.setColor(Color.WHITE); paint.setColorFilter( new PorterDuffColorFilter((dim ? 0x60FFFFFF : 0xC0FFFFFF), PorterDuff.Mode.MULTIPLY)); clockView.setLayerType(View.LAYER_TYPE_HARDWARE, paint); }
BitmapsView(Context c) { super(c); mBitmap1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1); mBitmap2 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset2); mColorMatrixPaint = new Paint(); final ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); mColorMatrixPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); mLightingPaint = new Paint(); mLightingPaint.setColorFilter(new LightingColorFilter(0x0060ffff, 0x00101030)); mBlendPaint = new Paint(); mBlendPaint.setColorFilter(new PorterDuffColorFilter(0x7f990040, PorterDuff.Mode.SRC_OVER)); }
private static Bitmap convertToAlphaMask(Bitmap b) { Bitmap a = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.ALPHA_8); Canvas c = new Canvas(a); Paint pt = new Paint(); pt.setColorFilter(new ColorMatrixColorFilter(MASK)); c.drawBitmap(b, 0.0f, 0.0f, pt); return a; }
@Override public void draw(Canvas canvas) { final Paint paint = mPaint; final boolean clearColorFilter; if (mTintFilter != null && paint.getColorFilter() == null) { paint.setColorFilter(mTintFilter); clearColorFilter = true; } else { clearColorFilter = false; } canvas.drawRoundRect(mBoundsF, mRadius, mRadius, paint); if (clearColorFilter) { paint.setColorFilter(null); } }
public DeleteZone(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); final int srcColor = context.getResources().getColor(R.color.delete_color_filter); mTrashPaint.setColorFilter(new PorterDuffColorFilter(srcColor, PorterDuff.Mode.SRC_ATOP)); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DeleteZone, defStyle, 0); mOrientation = a.getInt(R.styleable.DeleteZone_direction, ORIENTATION_HORIZONTAL); a.recycle(); }
@Override public void setColorFilter(ColorFilter cf) { if (cf == mColorFilter) { return; } mColorFilter = cf; mBitmapPaint.setColorFilter(mColorFilter); invalidate(); }
/** * Changes color of given bitmap to specified value. The original bitmap is not released. * * @param sourceBitmap The bitmap to be colored. * @param color The RGB color of color filter. * @return The colored bitmap. */ public static Bitmap changeBitmapColor(Bitmap sourceBitmap, int color) { Bitmap resultBitmap = sourceBitmap.copy(sourceBitmap.getConfig(), true); Paint p = new Paint(); ColorFilter filter = new LightingColorFilter(color, 0); p.setColorFilter(filter); Canvas canvas = new Canvas(resultBitmap); canvas.drawBitmap(resultBitmap, 0, 0, p); return resultBitmap; }
// no way to reuse LightingColorFilter here @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { float r = CENTER_X - mPaint.getStrokeWidth() * 0.5f; drawRect.left = -r; drawRect.top = -r; drawRect.right = r; drawRect.bottom = r; canvas.translate(CENTER_X, CENTER_X); canvas.drawOval(drawRect, mPaint); // canvas.drawCircle(0, 0, CENTER_RADIUS, mCenterPaint); if (mTrackingCenter) { // int c = mCenterPaint.getColor(); // mCenterPaint.setStyle(Paint.Style.STROKE); // if (mHighlightCenter) { mCenterPaint.setAlpha(0xFF); } else { mCenterPaint.setAlpha(0x80); } // canvas.drawCircle(0, 0, // CENTER_RADIUS + mCenterPaint.getStrokeWidth(), // mCenterPaint); // // mCenterPaint.setStyle(Paint.Style.FILL); // mCenterPaint.setColor(c); canvas.drawBitmap( mHomesteadSelectedIcon, -mHomesteadHalfIconWidth, -mHomesteadHalfIconHeight, mCenterPaint); } mCenterPaint.setColorFilter(new LightingColorFilter(mCenterPaint.getColor(), 1)); canvas.drawBitmap( mHomesteadIcon, -mHomesteadHalfIconWidth, -mHomesteadHalfIconHeight, mCenterPaint); mCenterPaint.setColorFilter(null); }
private Bitmap generateShortcutPreview( ResolveInfo info, int maxWidth, int maxHeight, Bitmap preview) { Bitmap tempBitmap = mCachedShortcutPreviewBitmap.get(); final Canvas c = mCachedShortcutPreviewCanvas.get(); if (tempBitmap == null || tempBitmap.getWidth() != maxWidth || tempBitmap.getHeight() != maxHeight) { tempBitmap = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888); mCachedShortcutPreviewBitmap.set(tempBitmap); } else { c.setBitmap(tempBitmap); c.drawColor(0, PorterDuff.Mode.CLEAR); c.setBitmap(null); } // Render the icon Drawable icon = mIconCache.getFullResIcon(info); int paddingTop = mContext.getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_top); int paddingLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_left); int paddingRight = mContext.getResources().getDimensionPixelOffset(R.dimen.shortcut_preview_padding_right); int scaledIconWidth = (maxWidth - paddingLeft - paddingRight); renderDrawableToBitmap( icon, tempBitmap, paddingLeft, paddingTop, scaledIconWidth, scaledIconWidth); if (preview != null && (preview.getWidth() != maxWidth || preview.getHeight() != maxHeight)) { throw new RuntimeException("Improperly sized bitmap passed as argument"); } else if (preview == null) { preview = Bitmap.createBitmap(maxWidth, maxHeight, Config.ARGB_8888); } c.setBitmap(preview); // Draw a desaturated/scaled version of the icon in the background as a watermark Paint p = mCachedShortcutPreviewPaint.get(); if (p == null) { p = new Paint(); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); p.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); p.setAlpha((int) (255 * 0.06f)); mCachedShortcutPreviewPaint.set(p); } c.drawBitmap(tempBitmap, 0, 0, p); c.setBitmap(null); renderDrawableToBitmap(icon, preview, 0, 0, mAppIconSize, mAppIconSize); return preview; }
private void initGrayBackgroundBitmap() { mGrayBackgroundBitmap = Bitmap.createBitmap( mBackgroundBitmap.getWidth(), mBackgroundBitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(mGrayBackgroundBitmap); Paint grayPaint = new Paint(); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix); grayPaint.setColorFilter(filter); canvas.drawBitmap(mBackgroundBitmap, 0, 0, grayPaint); }
private Bitmap toGrayscale(Bitmap bmpOriginal) { Bitmap bmpGrayscale = Bitmap.createBitmap( bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bmpGrayscale); Paint paint = new Paint(); ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); paint.setColorFilter(f); c.drawBitmap(bmpOriginal, 0, 0, paint); return bmpGrayscale; }
/** * 将图片转化为灰度图片 * * @param bitmap * @return */ public static Bitmap grayBitmap(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Bitmap greyBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(greyBitmap); Paint paint = new Paint(); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix); paint.setColorFilter(colorMatrixFilter); canvas.drawBitmap(bitmap, 0, 0, paint); return greyBitmap; }
@Test public void shouldReceiveDescriptionWhenDrawABitmapToCanvasWithAPaintEffect() throws Exception { Bitmap bitmap1 = create("Bitmap One"); Bitmap bitmap2 = create("Bitmap Two"); Canvas canvas = new Canvas(bitmap1); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix())); canvas.drawBitmap(bitmap2, new Matrix(), paint); assertThat(shadowOf(bitmap1).getDescription()) .isEqualTo( "Bitmap One\nBitmap Two with ColorMatrixColorFilter<1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0> transformed by matrix"); }
private void getFromAttributes(Context context, AttributeSet attrs) { final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PatternView); try { maxSize = typedArray.getDimensionPixelSize(R.styleable.PatternView_maxSize, 0); circlePaint.setColorFilter( new PorterDuffColorFilter( typedArray.getColor(R.styleable.PatternView_circleColor, Color.RED), PorterDuff.Mode.SRC_ATOP)); pathPaint.setColor(typedArray.getColor(R.styleable.PatternView_pathColor, Color.WHITE)); gridSize = typedArray.getInt(R.styleable.PatternView_gridSize, 3); } finally { typedArray.recycle(); } }
public Bitmap toBinary(Bitmap bmpOriginal) { int width, height; height = bmpOriginal.getHeight(); width = bmpOriginal.getWidth(); Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); Canvas c = new Canvas(bmpGrayscale); Paint paint = new Paint(); ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); paint.setColorFilter(f); c.drawBitmap(bmpOriginal, 0, 0, paint); return bmpGrayscale; }
public static Bitmap makeGaryBitmap(Bitmap bitmap, Context context) { final int w = bitmap.getWidth(); final int h = bitmap.getHeight(); final Bitmap garyBitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888); final Canvas canvas = new Canvas(garyBitmap); Paint paint = new Paint(); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix); paint.setColorFilter(colorMatrixFilter); paint.setAlpha(100); canvas.drawBitmap(bitmap, 0, 0, paint); return garyBitmap; }
@Override public void draw(Canvas canvas) { final Rect bounds = getBounds(); final boolean clearColorFilter; if (mTintFilter != null && getPaint().getColorFilter() == null) { mPaint.setColorFilter(mTintFilter); clearColorFilter = true; } else { clearColorFilter = false; } final boolean needsMirroring = needsMirroring(); if (needsMirroring) { // Mirror the 9patch canvas.translate(bounds.right - bounds.left, 0); canvas.scale(-1.0f, 1.0f); } final int restoreAlpha; if (mNinePatchState.mBaseAlpha != 1.0f) { restoreAlpha = mPaint.getAlpha(); mPaint.setAlpha((int) (restoreAlpha * mNinePatchState.mBaseAlpha + 0.5f)); } else { restoreAlpha = -1; } mNinePatch.draw(canvas, bounds, mPaint); if (clearColorFilter) { mPaint.setColorFilter(null); } if (restoreAlpha >= 0) { mPaint.setAlpha(restoreAlpha); } }
public static Bitmap colorMatrixBmp(Bitmap bmp, float[] matrixSrc) { int width = bmp.getWidth(); int height = bmp.getHeight(); ColorMatrix matrix = new ColorMatrix(); matrix.set(matrixSrc); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix); Paint p = new Paint(); p.setColorFilter(filter); Bitmap colorBmp = Bitmap.createBitmap(width, height, bmp.getConfig()); Canvas c = new Canvas(colorBmp); c.drawBitmap(bmp, 0, 0, p); return colorBmp; }