android.graphics.ColorMatrixColorFilter Java Examples

The following examples show how to use android.graphics.ColorMatrixColorFilter. 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 check out the related API usage on the sidebar.
Example #1
Source File: DragView.java    From Trebuchet with GNU General Public License v3.0 7 votes vote down vote up
@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();
}
 
Example #2
Source File: PageWidget.java    From fangzhuishushenqi with Apache License 2.0 6 votes vote down vote up
public PageWidget(Context context, String bookId,
                  List<BookToc.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;
}
 
Example #3
Source File: ImageUtils.java    From AndroidBase with Apache License 2.0 6 votes vote down vote up
/**
 * 调整照片的色调,饱和度,亮度
 * MID_VALUE = 177; // 进度条的中间值
 * @param bm 原图
 * @param hue 色调 = (进度条进度 - MID_VALUE) * 1.0F / MIN_VALUE * 180
 * @param saturation 饱和度 = 进度条进度 * 1.0F / MIN_VALUE;
 * @param lum 亮度 = 进度条进度 * 1.0F / MIN_VALUE;
 * @return 调整后的图片
 */
public static Bitmap handleImageEffect(Bitmap bm, int hue, int saturation, int lum) {
    Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    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 bitmap;
}
 
Example #4
Source File: ImageFilterUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 转为灰度图片
 * @param bitmap 待操作源图片
 * @return 灰度图
 */
public static Bitmap toGray(final Bitmap bitmap) {
    if (bitmap == null) return null;
    try {
        Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
        Canvas canvas = new Canvas(newBitmap);
        Paint paint = new Paint();
        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.setSaturation(0);
        ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter(colorMatrix);
        paint.setColorFilter(colorMatrixColorFilter);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        return newBitmap;
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "toGray");
    }
    return null;
}
 
Example #5
Source File: ImageFilterUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 亮度处理
 * @param bitmap   待操作源图片
 * @param lumValue 新的亮度值
 * @return 改变了亮度值之后的图片
 */
public static Bitmap lum(final Bitmap bitmap, final int lumValue) {
    if (bitmap == null) return null;
    try {
        // 计算出符合要求的亮度值
        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;
    } catch (Exception e) {
        LogPrintUtils.eTag(TAG, e, "lum");
    }
    return null;
}
 
Example #6
Source File: PDFView.java    From AndroidPdfViewer with Apache License 2.0 6 votes vote down vote up
public void setNightMode(boolean nightMode) {
    this.nightMode = nightMode;
    if (nightMode) {
        ColorMatrix colorMatrixInverted =
                new ColorMatrix(new float[]{
                        -1, 0, 0, 0, 255,
                        0, -1, 0, 0, 255,
                        0, 0, -1, 0, 255,
                        0, 0, 0, 1, 0});

        ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrixInverted);
        paint.setColorFilter(filter);
    } else {
        paint.setColorFilter(null);
    }
}
 
Example #7
Source File: GreyScaleTransformation.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
@Override
public Bitmap transform(@NonNull 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);

    if (bitmap != source && !source.isRecycled()) {
        source.recycle();
    }

    return bitmap;
}
 
Example #8
Source File: HueFallbackFragment.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
@Override
public void updateBackground(boolean isConnected) {
    try {
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0f);
        final ColorFilter filter = new ColorMatrixColorFilter(cm);
        final View bgView = ImageManager.getWallpaperView();
        if (bgView != null) {
            final Drawable bgDrawable = bgView.getBackground();
            if (bgDrawable != null) {
                bgDrawable.setColorFilter(filter);
            }
        }
    }catch (Exception e){
        logger.error("Can't change background color filter: {}", e);
    }
}
 
Example #9
Source File: ImageUtils.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 6 votes vote down vote up
/**
 * Returns a gray scale version of the input bitmap.
 *
 * @param bmpOriginal the image to convert.
 * @return gray scale version.
 */
static Bitmap toGrayScale(Bitmap bmpOriginal) {
    int         height       = bmpOriginal.getHeight();
    int         width        = bmpOriginal.getWidth();
    Bitmap      bmpGrayScale = Bitmap.createBitmap(width, height, 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;
}
 
Example #10
Source File: ImageUtils.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 6 votes vote down vote up
/**
 * Updates the saturation of the input bitmap.
 *
 * @param src        input bitmap.
 * @param saturation the new saturation value.
 * @return bitmap with updated saturation.
 */
static Bitmap saturatedBitmap(Bitmap src, float saturation) {

    if (saturation < 0.0) {
        return src;
    }

    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(saturation);
    ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix);
    paint.setColorFilter(filter);
    canvasResult.drawBitmap(src, 0, 0, paint);

    return bitmapResult;
}
 
Example #11
Source File: ImageUtils.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 6 votes vote down vote up
/**
 * Adjusts the brightness and contrast in the input bitmap.
 *
 * @param bitmap     input bitmap.
 * @param contrast   the new contrast value.
 * @param brightness the new brightness value.
 * @return bitmap with contrast and brightness changes.
 */
static Bitmap contrastAndBrightnessController(Bitmap bitmap, float contrast, float brightness) {

    ColorMatrix cmatrix = 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(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    Canvas canvas = new Canvas(ret);
    Paint  paint  = new Paint();

    paint.setColorFilter(new ColorMatrixColorFilter(cmatrix));
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return ret;
}
 
Example #12
Source File: BrokenAnimator.java    From BrokenView with MIT License 6 votes vote down vote up
private void buildPaintShader(){
    if(mConfig.paint == null) {
        onDrawPaint = new Paint();
        BitmapShader shader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        Matrix matrix = new Matrix();
        // Refraction effect
        matrix.setTranslate(-offsetX - 10, -offsetY - 7);
        shader.setLocalMatrix(matrix);
        ColorMatrix cMatrix = new ColorMatrix();
        // Increase saturation and brightness
        cMatrix.set(new float[]{
                2.5f, 0, 0, 0, 100,
                0, 2.5f, 0, 0, 100,
                0, 0, 2.5f, 0, 100,
                0, 0, 0, 1, 0});
        onDrawPaint.setColorFilter(new ColorMatrixColorFilter(cMatrix));
        onDrawPaint.setShader(shader);
        onDrawPaint.setStyle(Paint.Style.FILL);
    }
    else
        onDrawPaint = mConfig.paint;
}
 
Example #13
Source File: ColorMatrixView.java    From customview-samples with Apache License 2.0 6 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    int[] argb = parseRgb(mColor);
    Log.d("hyh", "ColorMatrixFragment: onStopTrackingTouch: argb="+ Arrays.toString(argb));

    // 根据SeekBar定义RGBA的矩阵, 通过修改矩阵第五列颜色的偏移量改变图片的颜色
    mColorMatrixArray[4] = argb[0];
    mColorMatrixArray[9] = argb[1];
    mColorMatrixArray[14] = argb[2];
    Log.d("hyh", "ColorMatrixView: onDraw: mColorMatrixArray="+Arrays.toString(mColorMatrixArray));

    // 4.使用ColorMatrix创建一个ColorMatrixColorFilter对象, 作为画笔的滤镜, 设置Paint的颜色
    mPaint.setColorFilter(new ColorMatrixColorFilter(mColorMatrixArray));

    // 5.通过指定了RGBA矩阵的Paint把原图画到空白图片上
    canvas.drawBitmap(mBitmap ,mMatrix ,mPaint);
}
 
Example #14
Source File: IosButton.java    From HumanBody with Apache License 2.0 6 votes vote down vote up
/**
 * 传入改变亮度前的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);  
   }
 
Example #15
Source File: DragView.java    From LaunchEnr with GNU General Public License v3.0 6 votes vote down vote up
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();
}
 
Example #16
Source File: SimulationPageAnim.java    From FriendBook with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #17
Source File: ArcusProductFragment.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
/**
 * convert the current screen color to grey
 * @param apply apply grey scale or not
 */
private void applyGreyScale(final boolean apply){

    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0f);
    final ColorFilter filter = new ColorMatrixColorFilter(cm);

    if (apply) {
        if (deviceImage != null) this.deviceImage.setColorFilter(filter);
        if (this.leftNav != null) this.leftNav.setColorFilter(null);
        if (this.rightNav != null) this.rightNav.setColorFilter(null);
        if (this.bottomView != null && !isBottomViewAlerting) this.bottomView.getBackground().setColorFilter(filter);
    } else {
        if (deviceImage != null) this.deviceImage.setColorFilter(null);
        if (this.leftNav != null) this.leftNav.setColorFilter(null);
        if (this.rightNav != null) this.rightNav.setColorFilter(null);
        if (this.bottomView != null && !isBottomViewAlerting) this.bottomView.getBackground().setColorFilter(null);
    }
}
 
Example #18
Source File: BitmapUtil.java    From AndroidStudyDemo with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 色相处理
 *
 * @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 #19
Source File: XBitmapUtils.java    From XFrame with Apache License 2.0 6 votes vote down vote up
/**
 * 饱和度处理
 *
 * @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;
}
 
Example #20
Source File: XBitmapUtils.java    From XFrame with Apache License 2.0 6 votes vote down vote up
/**
 * 亮度处理
 *
 * @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;
}
 
Example #21
Source File: XBitmapUtils.java    From XFrame with Apache License 2.0 6 votes vote down vote up
/**
 * 色相处理
 *
 * @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 #22
Source File: GrayscaleTransformation.java    From AccountBook with GNU General Public License v3.0 6 votes vote down vote up
@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 #23
Source File: CoverFlowLayoutManger.java    From RecyclerCoverFlow with Apache License 2.0 6 votes vote down vote up
/**
     * 变化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);
        }

    }
 
Example #24
Source File: IosButton.java    From HumanBody with Apache License 2.0 6 votes vote down vote up
/**
 * 传入改变亮度前的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);  
   }
 
Example #25
Source File: ApplicationIconUtils.java    From FreezeYou with Apache License 2.0 6 votes vote down vote up
/**
 * 参考:https://blog.csdn.net/xuwenneng/article/details/52634979
 * 对图片进行灰度化处理
 *
 * @param bm 原始图片
 * @return 灰度化图片
 */
public static Bitmap getGrayBitmap(Bitmap bm) {
    Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    //创建画布
    Canvas canvas = new Canvas(bitmap);
    //创建画笔
    Paint paint = new Paint();
    //创建颜色矩阵
    ColorMatrix matrix = new ColorMatrix();
    //设置颜色矩阵的饱和度:0代表灰色,1表示原图
    matrix.setSaturation(0);
    //颜色过滤器
    ColorMatrixColorFilter cmcf = new ColorMatrixColorFilter(matrix);
    //设置画笔颜色过滤器
    paint.setColorFilter(cmcf);
    //画图
    canvas.drawBitmap(bm, 0, 0, paint);
    return bitmap;
}
 
Example #26
Source File: ImageUtil.java    From XposedNavigationBar with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example #27
Source File: AptoideUtils.java    From aptoide-client with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #28
Source File: Stools.java    From AndroidPhotoshopColorPicker with Artistic License 2.0 6 votes vote down vote up
/**
 * Applies a tint on the drawable with the given color
 * @param drawable Drawable to apply the tint on
 * @param color Color to apply as tint on the drawable
 * @return Tinted drawable
 */
public static Drawable tintDrawable(Drawable drawable,int color){
    int red   = (color & 0xFF0000) / 0xFFFF;
    int green = (color & 0xFF00) / 0xFF;
    int blue  = color & 0xFF;

    float[] matrix = { 0, 0, 0, 0, red,
            0, 0, 0, 0, green,
            0, 0, 0, 0, blue,
            0, 0, 0, 1, 0 };

    ColorFilter colorFilter = new ColorMatrixColorFilter(matrix);
    if (drawable != null) {
        drawable.setColorFilter(colorFilter);
    }

    return drawable;
}
 
Example #29
Source File: ImageHelper.java    From Mysplash with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Execute a saturation animation to make a image from white and black into color.
 * */
public static void startSaturationAnimation(Context context, ImageView target, long duration) {
    target.setHasTransientState(true);
    final AnimUtils.ObservableColorMatrix matrix = new AnimUtils.ObservableColorMatrix();
    final ObjectAnimator saturation = ObjectAnimator.ofFloat(
            matrix, AnimUtils.ObservableColorMatrix.SATURATION, 0f, 1f);
    saturation.addUpdateListener(valueAnimator -> target.setColorFilter(new ColorMatrixColorFilter(matrix)));
    saturation.setDuration(duration);
    saturation.setInterpolator(AnimUtils.getFastOutSlowInInterpolator(context));
    saturation.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            target.clearColorFilter();
            target.setHasTransientState(false);
        }
    });
    saturation.start();
}
 
Example #30
Source File: pageView.java    From Jreader with GNU General Public License v2.0 6 votes vote down vote up
public pageView(Context context, int width, int height) {
    super(context);
    mPath0 = new Path();
    mPath1 = new Path();
    mScreenWidth = width;
    mScreenHeight = height;

    mPaint = new Paint();
    mPaint.setStyle(Paint.Style.FILL);

    createDrawable();

    ColorMatrix cm = new ColorMatrix();//设置颜色数组  4*5颜色矩阵
    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();
    mScroller = new Scroller(getContext());

    mTouch.x = 0.01f; // 不让x,y为0,否则在点计算时会有问题
    mTouch.y = 0.01f;
}