/** * 变化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); } }
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; }
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); } }
public void adjustNightMode() { if (isNightMode && !isColorFilterOn) { float[] matrix = { -1, 0, 0, 0, nightModeTextBrightness, 0, -1, 0, 0, nightModeTextBrightness, 0, 0, -1, 0, nightModeTextBrightness, 0, 0, 0, 1, 0 }; setColorFilter(new ColorMatrixColorFilter(matrix)); isColorFilterOn = true; } else if (!isNightMode) { clearColorFilter(); isColorFilterOn = false; } invalidate(); }
private void startSaturationAnimation(Context context, final AppCompatImageView target) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { target.setHasTransientState(true); final AnimUtils.ObservableColorMatrix matrix = new AnimUtils.ObservableColorMatrix(); final ObjectAnimator saturation = ObjectAnimator.ofFloat( matrix, AnimUtils.ObservableColorMatrix.SATURATION, 0f, 1f); saturation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { target.setColorFilter(new ColorMatrixColorFilter(matrix)); } }); saturation.setDuration(2000); saturation.setInterpolator(AnimUtils.getFastOutSlowInInterpolator(context)); saturation.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { target.clearColorFilter(); target.setHasTransientState(false); } }); saturation.start(); } }
/** * 转为灰度图片 * * @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)); }
private void initView() { subject = PublishSubject.create(); subject.debounce(0, TimeUnit.MILLISECONDS) // .filter(new Predicate<Float>() { // @Override // public boolean test(Float contrast) throws Exception { // return true; // } // }) .distinctUntilChanged() .switchMap(new Function<Float, ObservableSource<ColorMatrixColorFilter>>() { @Override public ObservableSource<ColorMatrixColorFilter> apply(Float value) throws Exception { return postContrast(value); } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<ColorMatrixColorFilter>() { @Override public void accept(ColorMatrixColorFilter colorMatrixColorFilter) throws Exception { setColorFilter(colorMatrixColorFilter); } }); }
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; }
private void initView() { subject = PublishSubject.create(); subject.debounce(0, TimeUnit.MILLISECONDS) // .filter(new Predicate<Float>() { // @Override // public boolean test(Float brightness) throws Exception { // return true; // } // }) .distinctUntilChanged() .switchMap(new Function<Float, ObservableSource<ColorMatrixColorFilter>>() { @Override public ObservableSource<ColorMatrixColorFilter> apply(Float value) throws Exception { return postBrightness(value); } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer<ColorMatrixColorFilter>() { @Override public void accept(ColorMatrixColorFilter colorMatrixColorFilter) throws Exception { setColorFilter(colorMatrixColorFilter); } }); }
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 PostAdapter(Context context, List<Post> posts, String blogUrl, Picasso picasso, View.OnClickListener itemClickListener) { mContext = context; mBlogUrl = blogUrl; mPicasso = picasso; mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mPosts = posts; mItemClickListener = itemClickListener; mLowAlphaPaint = new Paint(); mLowAlphaPaint.setColorFilter(new ColorMatrixColorFilter(new float[] { 1, 0, 0, 0, 0, // red 0, 1, 0, 0, 0, // green 0, 0, 1, 0, 0, // blue 0, 0, 0, 0.5f, 0, // alpha })); setHasStableIds(true); }
@Override public void convertDiff(ViewHolder holder, int position, List payloads) { Bundle payload = (Bundle) payloads.get(0); int isEnable = payload.getInt(BUNDLE_PAYLOAD); ((TextView) holder.getView(R.id.tv_app_name)).setTextColor(isEnable == 1 ? mResources.getColor(R.color.textPrimary) : mResources.getColor(R.color.translucentBg)); if (isEnable == 1) { mMatrix.setSaturation(1); mColorFilterNormal = new ColorMatrixColorFilter(mMatrix); ((ImageView) holder.getView(R.id.iv_app_icon)).setColorFilter(mColorFilterNormal); } else { mMatrix.setSaturation(0); mColorFilterGrey = new ColorMatrixColorFilter(mMatrix); ((ImageView) holder.getView(R.id.iv_app_icon)).setColorFilter(mColorFilterGrey); } }
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); } }