public static void setSepiaColorFilter(Drawable drawable) { if (drawable == null) return; final ColorMatrix matrixA = new ColorMatrix(); // making image B&W matrixA.setSaturation(0); final ColorMatrix matrixB = new ColorMatrix(); // applying scales for RGB color values matrixB.setScale(1f, .95f, .82f, 1.0f); matrixA.setConcat(matrixB, matrixA); final ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrixA); drawable.setColorFilter(filter); }
@Override public void onAnimationUpdate(ValueAnimator valueAnimator) { ImageView imageView = (ImageView) animator.getTarget(); float fraction = animator.getAnimatedFraction(); saturationMatrix.setSaturation((Float) animator.getAnimatedValue()); float scale = 2 - interpolator.getInterpolation(Math.min(fraction * 4 / 3, 1)); brightnessMatrix.setScale(scale, scale, scale, 1); saturationMatrix.preConcat(brightnessMatrix); imageView.setColorFilter(new ColorMatrixColorFilter(saturationMatrix)); imageView.setAlpha(interpolator.getInterpolation(Math.min(fraction * 2, 1))); } });
@Override public void onAnimationUpdate(ValueAnimator valueAnimator) { ImageView imageView = (ImageView) animator.getTarget(); float fraction = animator.getAnimatedFraction(); saturationMatrix.setSaturation((Float) animator.getAnimatedValue()); float scale = 2 - interpolator.getInterpolation(Math.min((1 - fraction) * 4 / 3, 1)); brightnessMatrix.setScale(scale, scale, scale, 1); saturationMatrix.preConcat(brightnessMatrix); imageView.setColorFilter(new ColorMatrixColorFilter(saturationMatrix)); imageView.setAlpha(interpolator.getInterpolation(Math.min((1 - fraction) * 2, 1))); } });
/** * 亮度处理 * * @param bitmap 原图 * @param lumValue 新的亮度值 * @return 改变了亮度值之后的图片 */ public static Bitmap lum(Bitmap bitmap, int lumValue) { // 计算出符合要求的亮度值 float newlumValue = lumValue * 1.0F / 127; // 创建一个颜色矩阵 ColorMatrix lumColorMatrix = new ColorMatrix(); // 设置亮度值 lumColorMatrix.setScale(newlumValue, newlumValue, newlumValue, 1); // 创建一个画笔并设置其颜色过滤器 Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(lumColorMatrix)); // 创建一个新的图片并创建画布 Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); // 将原图使用给定的画笔画到画布上 canvas.drawBitmap(bitmap, 0, 0, paint); return newBitmap; }
/** * 亮度处理 * * @param bitmap 原图 * @param lumValue 新的亮度值 * @return 改变了亮度值之后的图片 */ public static Bitmap lum(Bitmap bitmap, int lumValue) { // 计算出符合要求的亮度值 float newlumValue = lumValue * 1.0F / 127; // 创建一个颜色矩阵 ColorMatrix lumColorMatrix = new ColorMatrix(); // 设置亮度值 lumColorMatrix.setScale(newlumValue, newlumValue, newlumValue, 1); // 创建一个画笔并设置其颜色过滤器 Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(lumColorMatrix)); // 创建一个新的图片并创建画布 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; }
colorMatrix.setScale(newlumValue, newlumValue, newlumValue, 1);
colorMatrix.setScale(newlumValue, newlumValue, newlumValue, 1);
public static void setColorScale(int color, ColorMatrix target) { target.setScale(Color.red(color) / 255f, Color.green(color) / 255f, Color.blue(color) / 255f, Color.alpha(color) / 255f); } }
public static void setColorScale(int color, ColorMatrix target) { target.setScale(Color.red(color) / 255f, Color.green(color) / 255f, Color.blue(color) / 255f, Color.alpha(color) / 255f); } }
public static void setColorScale(int color, ColorMatrix target) { target.setScale(Color.red(color) / 255f, Color.green(color) / 255f, Color.blue(color) / 255f, Color.alpha(color) / 255f); }
public static void setColorScale(int color, ColorMatrix target) { target.setScale(Color.red(color) / 255f, Color.green(color) / 255f, Color.blue(color) / 255f, Color.alpha(color) / 255f); } }
InputViewHolder(View v) { super(v); this.mColorMatrix.setScale(0.5f, 0.5f, 0.5f, 1.0f); if (v instanceof BannerView) { this.mBannerView = (BannerView) v; this.mImageView = (ImageView) v.findViewById(R.id.input_image); this.mLabelView = (TextView) v.findViewById(R.id.input_label); this.mBackground = v.getResources().getDrawable(R.drawable.input_banner_background, null); return; } this.mBannerView = null; this.mImageView = null; this.mLabelView = null; this.mBackground = null; }
/** * Scales a color matrix such that, when applied to color R G B A, it produces R' G' B' A' where * R' = r * R * G' = g * G * B' = b * B * A' = a * A * <p> * The matrix will, for instance, turn white into r g b a, and black will remain black. * * @param color The color r g b a * @param target The ColorMatrix to scale */ public static void setColorScaleOnMatrix(int color, ColorMatrix target) { target.setScale(Color.red(color) / 255f, Color.green(color) / 255f, Color.blue(color) / 255f, Color.alpha(color) / 255f); } }
private static void setBrightnessMatrix(ColorMatrix matrix, int brightness) { // Brightness: C-new = C-old*(1-amount) + amount float scale = 1 - brightness / 255.0f; matrix.setScale(scale, scale, scale, 1); float[] array = matrix.getArray(); // Add the amount to RGB components of the matrix, as per the above formula. // Fifth elements in the array correspond to the constant being added to // red, blue, green, and alpha channel respectively. array[4] = brightness; array[9] = brightness; array[14] = brightness; } }
private static void setBrightnessMatrix(ColorMatrix matrix, int brightness) { // Brightness: C-new = C-old*(1-amount) + amount float scale = 1 - brightness / 255.0f; matrix.setScale(scale, scale, scale, 1); float[] array = matrix.getArray(); // Add the amount to RGB components of the matrix, as per the above formula. // Fifth elements in the array correspond to the constant being added to // red, blue, green, and alpha channel respectively. array[4] = brightness; array[9] = brightness; array[14] = brightness; } }
public void setNavigationIconColor(int color) { ColorMatrix matrix = new ColorMatrix(); matrix.setScale(Color.red(color) / 255f, Color.green(color) / 255f, Color.blue(color) / 255f, 1); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix); mToolbar.getNavigationIcon().setColorFilter(filter); }
public static Bitmap handleImageEffect(Bitmap bm, float lum) { Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmp); Paint paint = new Paint(); ColorMatrix lumMatrix = new ColorMatrix(); lumMatrix.setScale(lum, lum, lum, 1); ColorMatrix imageMatrix = new ColorMatrix(); imageMatrix.postConcat(lumMatrix); paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix)); canvas.drawBitmap(bm, 0, 0, paint); return bmp; }
public static void SepiaEffect(Drawable drawable) { if (drawable == null) return; final ColorMatrix matrixA = new ColorMatrix(); // making image B&W matrixA.setSaturation(0); final ColorMatrix matrixB = new ColorMatrix(); // applying scales for RGB color values matrixB.setScale(1f, .95f, .82f, 1.0f); matrixA.setConcat(matrixB, matrixA); final ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrixA); drawable.setColorFilter(filter); }
private void setBackground() { Bitmap bitmap = BlurUtil.doBlur(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_show), 10, 30); iv_bg.setImageBitmap(bitmap); iv_bg.setDrawingCacheEnabled(true); getBitmap(); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setScale(0.7f, 0.7f, 0.7f, 1); ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix); iv_bg.setColorFilter(colorFilter); }
public static Drawable getImageFiltered(Context context, int res, int color) { // load image: Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), res); BitmapDrawable drawable = new BitmapDrawable(context.getResources(), bitmap); // create matrix to convert to greyscale: ColorMatrix greyscaleMatrix = new ColorMatrix(); greyscaleMatrix.setSaturation(0); // create matrix to colorize and apply transluceny: ColorMatrix colorizeMatrix = new ColorMatrix(); color = context.getResources().getColor(color); colorizeMatrix.setScale(Color.red(color) / 255.0f, Color.green(color) / 255.0f, Color.blue(color) / 255.0f, Color.alpha(color) / 255.0f); // <- try setting this to 1.0f for no translucency // concatenate the two matrices and create a ColorMatrixColorFilter from the result: greyscaleMatrix.postConcat(colorizeMatrix); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(greyscaleMatrix); // apply the filter: drawable.setColorFilter(filter); return drawable; }