/** * 变化Item的灰度值 * @param child 需要设置灰度值的Item * @param frame 位置信息 */ private void greyItem(View child, Rect frame) { float value = computeGreyScale(frame.left - mOffsetAll); ColorMatrix cm = new ColorMatrix(new float[]{ value, 0, 0, 0, 120*(1-value), 0, value, 0, 0, 120*(1-value), 0, 0, value, 0, 120*(1-value), 0, 0, 0, 1, 250*(1-value), }); // cm.setSaturation(0.9f); // Create a paint object with color matrix Paint greyPaint = new Paint(); greyPaint.setColorFilter(new ColorMatrixColorFilter(cm)); // Create a hardware layer with the grey paint child.setLayerType(View.LAYER_TYPE_HARDWARE, greyPaint); if (value >= 1) { // Remove the hardware layer child.setLayerType(View.LAYER_TYPE_NONE, null); } }
private int rotateColor(int color, float rad) { float deg = rad * 180 / 3.1415927f; int r = Color.red(color); int g = Color.green(color); int b = Color.blue(color); ColorMatrix cm = new ColorMatrix(); ColorMatrix tmp = new ColorMatrix(); cm.setRGB2YUV(); tmp.setRotate(0, deg); cm.postConcat(tmp); tmp.setYUV2RGB(); cm.postConcat(tmp); final float[] a = cm.getArray(); int ir = floatToByte(a[0] * r + a[1] * g + a[2] * b); int ig = floatToByte(a[5] * r + a[6] * g + a[7] * b); int ib = floatToByte(a[10] * r + a[11] * g + a[12] * b); return Color.argb(Color.alpha(color), pinToByte(ir), pinToByte(ig), pinToByte(ib)); }
public PageWidget(Context context, String bookId, List<BookMixAToc.mixToc.Chapters> chaptersList, OnReadStateChangeListener listener) { super(context, bookId, chaptersList, listener); mPath0 = new Path(); mPath1 = new Path(); mMaxLength = (float) Math.hypot(mScreenWidth, mScreenHeight); mPaint = new Paint(); mPaint.setStyle(Paint.Style.FILL); createDrawable(); ColorMatrix cm = new ColorMatrix();//设置颜色数组 float array[] = {0.55f, 0, 0, 0, 80.0f, 0, 0.55f, 0, 0, 80.0f, 0, 0, 0.55f, 0, 80.0f, 0, 0, 0, 0.2f, 0}; cm.set(array); mColorMatrixFilter = new ColorMatrixColorFilter(cm); mMatrix = new Matrix(); mTouch.x = 0.01f; // 不让x,y为0,否则在点计算时会有问题 mTouch.y = 0.01f; }
/** * Keep all colorspace representations in sync. */ private void updateYUVfromRGB() { float r = mRGB[0] / 255.0f; float g = mRGB[1] / 255.0f; float b = mRGB[2] / 255.0f; ColorMatrix cm = new ColorMatrix(); cm.setRGB2YUV(); final float[] a = cm.getArray(); mYUV[0] = a[0] * r + a[1] * g + a[2] * b; mYUV[0] = pinToUnit(mYUV[0]); mYUV[1] = a[5] * r + a[6] * g + a[7] * b; mYUV[1] = pin(mYUV[1], -.5f, .5f); mYUV[2] = a[10] * r + a[11] * g + a[12] * b; mYUV[2] = pin(mYUV[2], -.5f, .5f); }
public static Bitmap FriskyEnhanceImage(Bitmap mBitmap, float contrast, float brightness) { ColorMatrix cm = new ColorMatrix(new float[] { contrast, 0, 0, 0, brightness, 0, contrast, 0, 0, brightness, 0, 0, contrast, 0, brightness, 0, 0, 0, 1, 0 }); Bitmap BrightedImage = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), mBitmap .getConfig()); Canvas canvas = new Canvas(BrightedImage); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(mBitmap, 0, 0, paint); return BrightedImage; }
public static Bitmap FriskyContrast(Bitmap bitmap,int Value) { ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.set(new float[] { Value, 0, 0, 0, 1, 0, Value, 0, 0, 1, 0, 0, Value, 0, 1, 0, 0, 0, Value, 0 }); Bitmap BrightedImage = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap .getConfig()); Canvas canvas = new Canvas(BrightedImage); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); canvas.drawBitmap(bitmap, 0, 0, paint); return BrightedImage; }
public static Bitmap FriskyBright(Bitmap mBitmap,int fb) { ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.set(new float[] { 1, 0, 0, 0, fb, 0, 1, 0, 0, fb, 0, 0, 1, 0, fb, 0, 0, 0, 1, 0 }); Bitmap BrightedImage = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), mBitmap .getConfig()); Canvas canvas = new Canvas(BrightedImage); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); canvas.drawBitmap(mBitmap, 0, 0, paint); return BrightedImage; }
private void animateFilterTo(float[] targetFilter) { float[] oldFilter = mCurrentFilter == null ? new ColorMatrix().getArray() : mCurrentFilter; mCurrentFilter = Arrays.copyOf(oldFilter, oldFilter.length); if (mFilterAnimator != null) { mFilterAnimator.cancel(); } mFilterAnimator = ValueAnimator.ofObject(new FloatArrayEvaluator(mCurrentFilter), oldFilter, targetFilter); mFilterAnimator.setDuration(COLOR_CHANGE_DURATION); mFilterAnimator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mPaint.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter)); invalidate(); } }); mFilterAnimator.start(); }
public SimulationPageAnim(int w, int h, View view, OnPageChangeListener listener) { super(w, h, view, listener); mPath0 = new Path(); mPath1 = new Path(); mMaxLength = (float) Math.hypot(mScreenWidth, mScreenHeight); mPaint = new Paint(); mPaint.setStyle(Paint.Style.FILL); createDrawable(); ColorMatrix cm = new ColorMatrix();//设置颜色数组 float array[] = { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,1, 0, 0, 0, 0, 0, 1, 0 }; cm.set(array); mColorMatrixFilter = new ColorMatrixColorFilter(cm); mMatrix = new Matrix(); mTouchX = 0.01f; // 不让x,y为0,否则在点计算时会有问题 mTouchY = 0.01f; }
private void checkSelected() { boolean selected = BridgeSettings.isActivityForward(getData().activityInfo.name); itemView.setSelected(selected); if (!selected) { if (filter.get() == null) { ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); ColorMatrixColorFilter grayColorFilter = new ColorMatrixColorFilter(cm); filter = new WeakReference<>(grayColorFilter); } icon.setColorFilter(filter.get()); } else { icon.setColorFilter(null); } }
/** * 转为灰度图片 * * @param src 源图片 * @param recycle 是否回收 * @return 灰度图 */ public static Bitmap toGray(final Bitmap src, final boolean recycle) { if (isEmptyBitmap(src)) { return null; } Bitmap ret = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig()); Canvas canvas = new Canvas(ret); Paint paint = new Paint(); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix); paint.setColorFilter(colorMatrixColorFilter); canvas.drawBitmap(src, 0, 0, paint); if (recycle && !src.isRecycled()) { src.recycle(); } return ret; }
/** * 饱和度处理 * * @param bitmap 原图 * @param saturationValue 新的饱和度值 * @return 改变了饱和度值之后的图片 */ public static Bitmap saturation(Bitmap bitmap, int saturationValue) { // 计算出符合要求的饱和度值 float newSaturationValue = saturationValue * 1.0F / 127; // 创建一个颜色矩阵 ColorMatrix saturationColorMatrix = new ColorMatrix(); // 设置饱和度值 saturationColorMatrix.setSaturation(newSaturationValue); // 创建一个画笔并设置其颜色过滤器 Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(saturationColorMatrix)); // 创建一个新的图片并创建画布 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(), Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); // 将原图使用给定的画笔画到画布上 canvas.drawBitmap(bitmap, 0, 0, paint); return newBitmap; }
/** * 色相处理 * * @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; }
private void otherInit() { // Other initialization mBlackPaint = new Paint(); mBlackPaint.setColor(0xff000000); mMaskedPaint = new Paint(); mMaskedPaint .setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); // Always want a cache allocated. mCacheBitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); if (mDesaturateOnPress) { // Create a desaturate color filter for pressed state. ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); mDesaturateColorFilter = new ColorMatrixColorFilter(cm); } }
@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); }
/** * 转为灰度图片 * * @param src 源图片 * @param recycle 是否回收 * @return 灰度图 */ public static Bitmap toGray(Bitmap src, boolean recycle) { if (isEmptyBitmap(src)) { return null; } Bitmap grayBitmap = Bitmap.createBitmap(src.getWidth(), src .getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(grayBitmap); Paint paint = new Paint(); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0); ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix); paint.setColorFilter(colorMatrixColorFilter); canvas.drawBitmap(src, 0, 0, paint); if (recycle && !src.isRecycled()) { src.recycle(); } return grayBitmap; }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fold); mImageView = (ImageView)findViewById(R.id.image_view); mImageView.setPadding(ANTIALIAS_PADDING, ANTIALIAS_PADDING, ANTIALIAS_PADDING, ANTIALIAS_PADDING); mImageView.setScaleType(ImageView.ScaleType.FIT_XY); mImageView.setImageDrawable(getResources().getDrawable(R.drawable.image)); mTextureView = new TextureView(this); mTextureView.setSurfaceTextureListener(mSurfaceTextureListener); mAnchorSeekBar = (SeekBar)findViewById(R.id.anchor_seek_bar); mFoldLayout = (FoldingLayout)findViewById(R.id.fold_view); mFoldLayout.setBackgroundColor(Color.BLACK); mFoldLayout.setFoldListener(mOnFoldListener); mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop(); mAnchorSeekBar.setOnSeekBarChangeListener(mSeekBarChangeListener); mScrollGestureDetector = new GestureDetector(this, new ScrollGestureDetector()); mItemSelectedListener = new ItemSelectedListener(); mDefaultPaint = new Paint(); mSepiaPaint = new Paint(); ColorMatrix m1 = new ColorMatrix(); ColorMatrix m2 = new ColorMatrix(); m1.setSaturation(0); m2.setScale(1f, .95f, .82f, 1.0f); m1.setConcat(m2, m1); mSepiaPaint.setColorFilter(new ColorMatrixColorFilter(m1)); }
static Bitmap contrastBitmap(Bitmap bitmap, float contrast) { float[] colorTransform = new float[]{ contrast, 0, 0, 0, 0, 0, contrast, 0, 0, 0, 0, 0, contrast, 0, 0, 0, 0, 0, 1, 0}; ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0f); colorMatrix.set(colorTransform); ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix); Paint paint = new Paint(); paint.setColorFilter(colorFilter); Bitmap resultBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(resultBitmap); canvas.drawBitmap(resultBitmap, 0, 0, paint); return resultBitmap; }
public static Bitmap brightBitmap(Bitmap bitmap, int brightness) { float[] colorTransform = { 1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0}; ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0f); colorMatrix.set(colorTransform); ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix); Paint paint = new Paint(); paint.setColorFilter(colorFilter); Bitmap resultBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(resultBitmap); canvas.drawBitmap(resultBitmap, 0, 0, paint); return resultBitmap; }
public static Bitmap contrastBitmap(Bitmap bitmap, int contrast) { float[] colorTransform = { contrast, 0, 0, 0, 1, 0, contrast, 0, 0, 1, 0, 0, contrast, 0, 1, 0, 0, 0, 1, 0}; ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0f); colorMatrix.set(colorTransform); ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix); Paint paint = new Paint(); paint.setColorFilter(colorFilter); Bitmap resultBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(resultBitmap); canvas.drawBitmap(resultBitmap, 0, 0, paint); return resultBitmap; }
static Bitmap brightBitmap(Bitmap bitmap, float brightness) { float[] colorTransform = { 1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0}; ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(0f); colorMatrix.set(colorTransform); ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix); Paint paint = new Paint(); paint.setColorFilter(colorFilter); Bitmap resultBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); Canvas canvas = new Canvas(resultBitmap); canvas.drawBitmap(resultBitmap, 0, 0, paint); return resultBitmap; }
/** * 传入改变亮度前的bitmap,返回改变亮度后的bitmap * @author leibing * @createTime 2016/12/30 * @lastModify 2016/12/30 * @param srcBitmap * @return */ @SuppressWarnings("deprecation") public Drawable changeBrightnessBitmap(Bitmap srcBitmap){ Bitmap bmp = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Config.ARGB_8888); int brightness = 60 - 127; ColorMatrix cMatrix = new ColorMatrix(); // 改变亮度 cMatrix.set(new float[] { 1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0 }); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(cMatrix)); Canvas canvas = new Canvas(bmp); // 在Canvas上绘制一个Bitmap canvas.drawBitmap(srcBitmap, 0, 0, paint); return new BitmapDrawable(bmp); }
public static Bitmap handleImageGrayEffect(Bitmap bitmap) { Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); float[] colorMatrixValue = new float[] { 0.33f, 0.59f, 0.11f, 0, 0, 0.33f, 0.59f, 0.11f, 0, 0, 0.33f, 0.59f, 0.11f, 0, 0, 0, 0, 0, 1, 0 }; ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.set(colorMatrixValue); Canvas canvas = new Canvas(bmp); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); canvas.drawBitmap(bitmap, 0, 0, paint); return bmp; }
public static Bitmap handleImageHighSaturationEffect(Bitmap bitmap) { Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); float[] colorMatrixValue = new float[] { 1.438f, -0.122f, -0.016f, 0, -0.03f, -0.062f, 1.378f, -0.016f, 0, 0.05f, -0.062f, -0.122f, 1.483f, 0, -0.02f, 0, 0, 0, 1, 0 }; ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.set(colorMatrixValue); Canvas canvas = new Canvas(bmp); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix)); canvas.drawBitmap(bitmap, 0, 0, paint); return bmp; }
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; }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void animateFilterTo(float[] targetFilter) { float[] oldFilter = mCurrentFilter == null ? new ColorMatrix().getArray() : mCurrentFilter; mCurrentFilter = Arrays.copyOf(oldFilter, oldFilter.length); if (mFilterAnimator != null) { mFilterAnimator.cancel(); } mFilterAnimator = ValueAnimator.ofObject(new FloatArrayEvaluator(mCurrentFilter), oldFilter, targetFilter); mFilterAnimator.setDuration(COLOR_CHANGE_DURATION); mFilterAnimator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mPaint.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter)); invalidate(); } }); mFilterAnimator.start(); }
public static void setBackground(View v, Bitmap bm) { if (bm == null) { v.setBackgroundResource(0); return; } int vwidth = v.getWidth()/4; int vheight = v.getHeight()/4; int bwidth = bm.getWidth(); int bheight = bm.getHeight(); float scalex = (float) vwidth / bwidth; float scaley = (float) vheight / bheight; float scale = Math.max(scalex, scaley) * 1.3f; Bitmap.Config config = Bitmap.Config.ARGB_8888; Bitmap bg = Bitmap.createBitmap(vwidth, vheight, config); Canvas c = new Canvas(bg); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setFilterBitmap(true); ColorMatrix greymatrix = new ColorMatrix(); greymatrix.setSaturation(0); ColorMatrix darkmatrix = new ColorMatrix(); darkmatrix.setScale(1f, 1f, 1f, .1f); greymatrix.postConcat(darkmatrix); ColorFilter filter = new ColorMatrixColorFilter(greymatrix); paint.setColorFilter(filter); Matrix matrix = new Matrix(); matrix.setTranslate(-bwidth/2, -bheight/2); // move bitmap center to origin matrix.postRotate(10); matrix.postScale(scale, scale); matrix.postTranslate(vwidth/2, vheight/2); // Move bitmap center to view center c.drawBitmap(bm, matrix, paint); v.setBackgroundDrawable(new BitmapDrawable(bg)); }
@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); ColorMatrix saturation = new ColorMatrix(); saturation.setSaturation(0f); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(saturation)); canvas.drawBitmap(source, 0, 0, paint); source.recycle(); return bitmap; }
/** * 设置View的灰度 */ public static void setGreyscale(View v, boolean greyscale) { if (greyscale) { // Create a paint object with 0 saturation (black and white) ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); Paint greyscalePaint = new Paint(); greyscalePaint.setColorFilter(new ColorMatrixColorFilter(cm)); // Create a hardware layer with the greyscale paint v.setLayerType(LAYER_TYPE_HARDWARE, greyscalePaint); } else { // Remove the hardware layer v.setLayerType(LAYER_TYPE_NONE, null); } }
/** * 设置Selector。 本次只增加点击变暗的效果,注释的代码为更多的效果 */ public static StateListDrawable createSLD(Context context, Drawable drawable) { StateListDrawable bg = new StateListDrawable(); int brightness = 50 - 127; ColorMatrix cMatrix = new ColorMatrix(); cMatrix.set(new float[]{1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness,// 改变亮度 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0}); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(cMatrix)); Drawable normal = drawable; Drawable pressed = createDrawable(drawable, paint); bg.addState(new int[]{android.R.attr.state_pressed,}, pressed); bg.addState(new int[]{android.R.attr.state_focused,}, pressed); bg.addState(new int[]{android.R.attr.state_selected}, pressed); bg.addState(new int[]{}, normal); return bg; }
private Bitmap updateSaturation(Bitmap src, float settingSat) { int w = src.getWidth(); int h = src.getHeight(); Bitmap bitmapResult = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas canvasResult = new Canvas(bitmapResult); Paint paint = new Paint(); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.setSaturation(settingSat); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix); paint.setColorFilter(filter); canvasResult.drawBitmap(src, 0, 0, paint); return bitmapResult; }
private Bitmap convertBitmapForColorMode(Bitmap original, int colorMode) { if (colorMode != COLOR_MODE_MONOCHROME) { return original; } // Create a grayscale bitmap Bitmap grayscale = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Config.ARGB_8888); Canvas c = new Canvas(grayscale); Paint p = new Paint(); ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); p.setColorFilter(f); c.drawBitmap(original, 0, 0, p); c.setBitmap(null); return grayscale; }
public static Bitmap changeBitmapContrastBrightness(Bitmap bmp, float contrast, float brightness) { ColorMatrix cm = new ColorMatrix(new float[] { contrast, 0, 0, 0, brightness, 0, contrast, 0, 0, brightness, 0, 0, contrast, 0, brightness, 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; }
public static Bitmap createColorFilteredBitmap(Bitmap origBitmap, ColorMatrix cm) { if (origBitmap == null || cm == null) { return origBitmap; } final int width = origBitmap.getWidth(); final int height = origBitmap.getHeight(); if (width <= 0 || height <= 0) { return origBitmap; } Bitmap filteredBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas c = new Canvas(filteredBitmap); ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); Paint paint = new Paint(); paint.setColorFilter(f); c.drawBitmap(origBitmap, 0, 0, paint); return filteredBitmap; }
/** * Sets a color to a Drawable * * @param drawable * @param color */ public static void setDrawableColor(Drawable drawable, int color) { // Assuming "color" is your target color float r = Color.red(color) / 255f; float g = Color.green(color) / 255f; float b = Color.blue(color) / 255f; float a = Color.alpha(color) / 255f; ColorMatrix cm = new ColorMatrix(new float[]{ r, r, r, r, r, //red g, g, g, g, g, //green b, b, b, b, b, //blue 1, 1, 1, 1, 1 //alpha }); ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm); drawable.setColorFilter(cf); }