Java Code Examples for android.graphics.Paint#setColorFilter()

The following examples show how to use android.graphics.Paint#setColorFilter() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
static Bitmap setBitmapBrightness(Bitmap bitmap, float brightness)
{
    if (bitmap == null)
        return null;

    float[] colorTransform = {
            1f, 0, 0, 0, brightness,
            0, 1f, 0, 0, brightness,
            0, 0, 1f, 0, brightness,
            0, 0, 0, 1f, 0};

    Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(),
            bitmap.getConfig());
    Canvas canvas = new Canvas(newBitmap);
    Paint paint = new Paint();
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.set(colorTransform);
    paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
    Matrix matrix = new Matrix();
    canvas.drawBitmap(bitmap, matrix, paint);

    return newBitmap;
}
 
Example 2
private void createBitmap(Bitmap quarter) {
  bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);

  Paint paint = new Paint();

  // Top left
  paint.setColorFilter(new LightingColorFilter(Color.RED, 0));
  canvas.drawBitmap(quarter, 0, 0, paint);

  // Top right
  paint.setColorFilter(new LightingColorFilter(Color.YELLOW, 0));
  canvas.drawBitmap(quarter, getWidth() / 2, 0, paint);

  // Bottom left
  paint.setColorFilter(new LightingColorFilter(Color.BLUE, 0));
  canvas.drawBitmap(quarter, 0, getHeight()/2, paint);

  // Bottom right
  paint.setColorFilter(new LightingColorFilter(Color.GREEN, 0));
  canvas.drawBitmap(quarter, getWidth() / 2, getHeight() / 2, paint);
}
 
Example 3
@Override public Bitmap transform(Bitmap source) {

    int width = source.getWidth();
    int height = source.getHeight();

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColorFilter(new PorterDuffColorFilter(mColor, PorterDuff.Mode.SRC_ATOP));
    canvas.drawBitmap(source, 0, 0, paint);
    source.recycle();

    return bitmap;
  }
 
Example 4
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();

  int width = source.getWidth();
  int height = source.getHeight();

  Bitmap.Config config =
      source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
  Bitmap bitmap = mBitmapPool.get(width, height, config);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(width, height, config);
  }

  Canvas canvas = new Canvas(bitmap);
  ColorMatrix saturation = new ColorMatrix();
  saturation.setSaturation(0f);
  Paint paint = new Paint();
  paint.setColorFilter(new ColorMatrixColorFilter(saturation));
  canvas.drawBitmap(source, 0, 0, paint);

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 5
/**
 * 色相处理
 *
 * @param bitmap   原图
 * @param hueValue 新的色相值
 * @return 改变了色相值之后的图片
 */
public static Bitmap hue(Bitmap bitmap, int hueValue) {
    // 计算出符合要求的色相值
    float newHueValue = (hueValue - 127) * 1.0F / 127 * 180;
    // 创建一个颜色矩阵
    ColorMatrix hueColorMatrix = new ColorMatrix();
    // 控制让红色区在色轮上旋转的角度
    hueColorMatrix.setRotate(0, newHueValue);
    // 控制让绿红色区在色轮上旋转的角度
    hueColorMatrix.setRotate(1, newHueValue);
    // 控制让蓝色区在色轮上旋转的角度
    hueColorMatrix.setRotate(2, newHueValue);
    // 创建一个画笔并设置其颜色过滤器
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(hueColorMatrix));
    // 创建一个新的图片并创建画布
    Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    // 将原图使用给定的画笔画到画布上
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return newBitmap;
}
 
Example 6
private Bitmap applyColorFilter(Bitmap marker) {
    Canvas canvas = new Canvas(marker);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    ColorFilter filter = new PorterDuffColorFilter(mMarkerColor, PorterDuff.Mode.SRC_ATOP);
    paint.setColorFilter(filter);

    canvas.drawBitmap(marker, 0, 0, paint);

    return marker;
}
 
Example 7
private void invertColor() {
    if (!isInverted) {
        Bitmap bmpMonochrome = Bitmap.createBitmap(cropImage.getWidth(), cropImage.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmpMonochrome);
        ColorMatrix ma = new ColorMatrix();
        ma.setSaturation(0);
        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(ma));
        canvas.drawBitmap(cropImage, 0, 0, paint);
        cropImage = bmpMonochrome.copy(bmpMonochrome.getConfig(), true);
    } else {
        cropImage = cropImage.copy(cropImage.getConfig(), true);
    }
    isInverted = !isInverted;
}
 
Example 8
public LightingColorFilterView(Context context, AttributeSet attrs) {
    super(context, attrs);

    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x00FF00FF));
    bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);

}
 
Example 9
protected Bitmap process(Bitmap original, ColorMatrix colorMatrix) {
  Bitmap bitmap = Bitmap.createBitmap(
      original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);

  Paint paint = new Paint();
  paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
  canvas.drawBitmap(original, 0, 0, paint);

  return bitmap;
}
 
Example 10
Source Project: KUAS-AP-Material   File: Utils.java    License: MIT License 5 votes vote down vote up
/**
 * White colors can be transformed in to different colors.
 *
 * @param sourceBitmap The source Bitmap
 * @param color        The different color
 * @return The different color Bitmap
 */
public static Bitmap changeImageColor(Bitmap sourceBitmap, int color) {
	Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth() - 1,
			sourceBitmap.getHeight() - 1);
	Paint p = new Paint();
	ColorFilter filter = new LightingColorFilter(color, 1);
	p.setColorFilter(filter);

	Canvas canvas = new Canvas(resultBitmap);
	canvas.drawBitmap(resultBitmap, 0, 0, p);
	return resultBitmap;
}
 
Example 11
Source Project: fresco   File: RoundedBitmapDrawableTest.java    License: MIT License 5 votes vote down vote up
@Test
public void testPreservePaintOnDrawableCopy() {
  ColorFilter colorFilter = mock(ColorFilter.class);
  Paint originalPaint = mock(Paint.class);
  BitmapDrawable originalVersion = mock(BitmapDrawable.class);

  originalPaint.setColorFilter(colorFilter);
  when(originalVersion.getPaint()).thenReturn(originalPaint);

  RoundedBitmapDrawable roundedVersion =
      RoundedBitmapDrawable.fromBitmapDrawable(mResources, originalVersion);

  assertEquals(
      originalVersion.getPaint().getColorFilter(), roundedVersion.getPaint().getColorFilter());
}
 
Example 12
@Override
public void process(Bitmap dst, Bitmap src) {
  super.process(dst, src);

  final Canvas canvas = new Canvas(dst);
  final Paint paint = new Paint();

  paint.setColorFilter(new LightingColorFilter(mMul, mAdd));

  canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
  canvas.drawBitmap(src, 0, 0, paint);
}
 
Example 13
Source Project: ticdesign   File: BlurBehind.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Bitmap doInBackground(Bitmap... params) {
    if (params.length == 0) {
        return null;
    }

    Bitmap source = params[0];

    int width = mBlurFactor.width / mBlurFactor.sampling;
    int height = mBlurFactor.height / mBlurFactor.sampling;

    if (width == 0 || height == 0) {
        return null;
    }

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    canvas.scale(1 / (float) mBlurFactor.sampling, 1 / (float) mBlurFactor.sampling);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
    PorterDuffColorFilter filter =
            new PorterDuffColorFilter(mBlurFactor.color, PorterDuff.Mode.SRC_ATOP);
    paint.setColorFilter(filter);
    canvas.drawBitmap(source, 0, 0, paint);

    return FastBlur.doBlur(bitmap, mBlurFactor.radius, true);
}
 
Example 14
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);
}
 
Example 15
private void init(Context context, AttributeSet attrs) {
    mStartTouch = null;
    mSelectedChild = null;
    mContainer = new SparseArray<Parcelable>(5);
    mListener = null;
    mChildStartRect = null;
    mChildEndRect = null;

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RearrangeableLayout);
    float strokeWidth = a.getDimension(R.styleable.RearrangeableLayout_outlineWidth, 2.0f);
    int color = a.getColor(R.styleable.RearrangeableLayout_outlineColor, Color.GRAY);
    float alpha = a.getFloat(R.styleable.RearrangeableLayout_selectionAlpha, 0.5f);
    mSelectionZoom = a.getFloat(R.styleable.RearrangeableLayout_selectionZoom, 1.2f);
    a.recycle();

    float filter[] = new float[] {
            1f, 0f, 0f, 0f, 0f,
            0f, 1f, 0f, 0f, 0f,
            0f, 0f, 1f, 0f, 0f,
            0f, 0f, 0f, alpha, 0f
    };
    ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(new ColorMatrix(filter));

    mOutlinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mOutlinePaint.setStyle(Paint.Style.STROKE);
    mOutlinePaint.setStrokeWidth(strokeWidth);
    mOutlinePaint.setColor(color);
    mOutlinePaint.setColorFilter(colorFilter);

    mSelectionPaint = new Paint();
    mSelectionPaint.setColorFilter(colorFilter);

    Animation trans = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0,
            Animation.RELATIVE_TO_PARENT, 1, Animation.RELATIVE_TO_PARENT, 0);
    trans.setDuration(500);
    trans.setInterpolator(new AccelerateInterpolator(1.0f));

    LayoutAnimationController c = new LayoutAnimationController(trans, 0.25f);
    setLayoutAnimation(c);
}
 
Example 16
public void setUpPaint()
{
    if (stroke != null)
    {
        strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        strokePaint.setStrokeWidth(stroke.lineWidth);
        //int col = OBUtils.applyColourOpacity(stroke.colour,opacity);
        strokePaint.setColor(stroke.colour);
        strokePaint.setStrokeCap(stroke.paintLineCap());
        strokePaint.setStrokeJoin(stroke.paintLineJoin());
        DashPathEffect dpe = stroke.dashPathEffect();
        if (dpe == null)
            strokePaint.setPathEffect(null);
        else
            strokePaint.setPathEffect(dpe);
        strokePaint.setStyle(Paint.Style.STROKE);
        if (colourFilter == null)
            strokePaint.setColorFilter(null);
        else
            strokePaint.setColorFilter(colourFilter);

    }
    if (fillColour != 0)
    {
        fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        //int col = OBUtils.applyColourOpacity(fillColour,opacity);
        fillPaint.setColor(fillColour);
        //fillPaint.setMaskFilter(new BlurMaskFilter(7, BlurMaskFilter.Blur.NORMAL));
        fillPaint.setStyle(Paint.Style.FILL);
        if (colourFilter == null)
            fillPaint.setColorFilter(null);
        else
            fillPaint.setColorFilter(colourFilter);
    }
}
 
Example 17
Source Project: DevUtils   File: ImageFilterUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 色相处理
 * @param bitmap   待操作源图片
 * @param hueValue 新的色相值
 * @return 改变了色相值之后的图片
 */
public static Bitmap hue(final Bitmap bitmap, final int hueValue) {
    if (bitmap == null) return null;
    try {
        // 计算出符合要求的色相值
        float newHueValue = (hueValue - 127) * 1.0F / 127 * 180;
        // 创建一个颜色矩阵
        ColorMatrix hueColorMatrix = new ColorMatrix();
        // 控制让红色区在色轮上旋转的角度
        hueColorMatrix.setRotate(0, newHueValue);
        // 控制让绿红色区在色轮上旋转的角度
        hueColorMatrix.setRotate(1, newHueValue);
        // 控制让蓝色区在色轮上旋转的角度
        hueColorMatrix.setRotate(2, newHueValue);
        // 创建一个画笔并设置其颜色过滤器
        Paint paint = new Paint();
        paint.setColorFilter(new ColorMatrixColorFilter(hueColorMatrix));
        // 创建一个新的图片并创建画布
        Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(newBitmap);
        // 将源图片使用给定的画笔画到画布上
        canvas.drawBitmap(bitmap, 0, 0, paint);
        return newBitmap;
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "hue");
    }
    return null;
}
 
Example 18
@Override public void process(Bitmap dest, Bitmap source) {
  super.process(dest, source);

  Canvas canvas = new Canvas(dest);
  ColorMatrix saturation = new ColorMatrix();
  saturation.setSaturation(0f);
  Paint paint = new Paint();
  paint.setColorFilter(new ColorMatrixColorFilter(saturation));
  canvas.drawBitmap(source, 0, 0, paint);
}
 
Example 19
/**
 * Update contrast and brightness of a bitmap.
 *
 * @param bmp              input bitmap
 * @param contrast         0..infinity - 1 is default
 * @param brightness       -1..1 - 0 is default
 * @param saturation       1/3..infinity - 1 is default
 * @param colorTemperature -1..1 - 0 is default
 * @return new bitmap
 */
public static Bitmap changeBitmapColors(@NonNull final Bitmap bmp, final float contrast, final float brightness,
										final float saturation, final float colorTemperature) {
	if (contrast == 1 && brightness == 0 && saturation == 1 && colorTemperature == 0) {
		return bmp;
	}

	// some baseCalculations for the mapping matrix
	int temperatureColor = ImageUtil.convertTemperatureToColor(colorTemperature);
	float factorRed = (float) BYTE / Color.red(temperatureColor);
	float factorGreen = (float) BYTE / Color.green(temperatureColor);
	float factorBlue = (float) BYTE / Color.blue(temperatureColor);
	float correctionFactor = (float) Math.pow(factorRed * factorGreen * factorBlue, -1f / 3); // MAGIC_NUMBER
	factorRed *= correctionFactor * contrast;
	factorGreen *= correctionFactor * contrast;
	factorBlue *= correctionFactor * contrast;
	float offset = BYTE / 2f * (1 - contrast + brightness * contrast + brightness);
	float oppositeSaturation = (1 - saturation) / 2;

	ColorMatrix cm = new ColorMatrix(new float[]{ //
			factorRed * saturation, factorGreen * oppositeSaturation, factorBlue * oppositeSaturation, 0, offset, //
			factorRed * oppositeSaturation, factorGreen * saturation, factorBlue * oppositeSaturation, 0, offset, //
			factorRed * oppositeSaturation, factorGreen * oppositeSaturation, factorBlue * saturation, 0, offset, //
			0, 0, 0, 1, 0});

	Bitmap ret = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());

	Canvas canvas = new Canvas(ret);

	Paint paint = new Paint();
	paint.setColorFilter(new ColorMatrixColorFilter(cm));
	canvas.drawBitmap(bmp, 0, 0, paint);

	return ret;
}
 
Example 20
@Nullable
public static Marker getMarker(Androzic application, String name, int color, @Nullable Bitmap icon)
{
	if (icon == null)
		return null;
	
	Marker marker = new Marker(name);

	Resources resources = application.getResources();
	int border = (int) (2 * resources.getDisplayMetrics().density);
	Bitmap pin = BitmapFactory.decodeResource(resources, R.drawable.marker_pin_1);
	marker.image = Bitmap.createBitmap(pin.getWidth(), pin.getHeight(), Bitmap.Config.ARGB_8888);

	Paint paint = new Paint();
	paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
	
	Canvas bc = new Canvas(marker.image);
	bc.drawBitmap(pin, 0f, 0f, paint);

	marker.anchorX = marker.image.getWidth() / 2;
	marker.anchorY = marker.image.getHeight();

	int width = marker.image.getWidth() - border * 2;

	int iconWidth = icon.getWidth();
	int iconHeight = icon.getHeight();
	int iconSize = iconWidth > iconHeight ? iconWidth : iconHeight;

	Matrix matrix = new Matrix();

	if (iconSize > width)
	{
		float scale = (float) (1. * width / iconSize);
		matrix.postScale(scale, scale);
		iconWidth = (int) (iconWidth * scale);
		iconHeight = (int) (iconHeight * scale);
	}

	matrix.postTranslate(border + (width - iconWidth) / 2, border + (width - iconHeight) / 2);
	bc.drawBitmap(icon, matrix, null);
	return marker;
}