How to implement “Loading images” pattern (Opacity, Exposure and Saturation) from Google's new Material design guidelines

前端 未结 4 1899
礼貌的吻别
礼貌的吻别 2021-01-31 00:03

Has anyone looked into implementing the Loading images pattern from Google\'s latest Material Design guide.

It\'s a recommended way that \"illustrations and photographs

4条回答
  •  遥遥无期
    2021-01-31 00:36

    Please note that this answer, as it stands, works for Lollipop only. The reason for this is because the colorMatrix property is not available to animate on the ColorMatrixColorFilter class (it doesn't provide getColorMatrix and setColorMatrix methods). To see this in action, try the code, in logcat output you should see a warning message like this:

    Method setColorMatrix() with type class android.graphics.ColorMatrix not found on target class class android.graphics.ColorMatrixColorFilter

    That being said, I was able to get this to work on older android versions (pre-Lollipop) by creating the following class (not the best name, I know)

    private class AnimateColorMatrixColorFilter {
        private ColorMatrixColorFilter mFilter;
        private ColorMatrix mMatrix;
    
        public AnimateColorMatrixColorFilter(ColorMatrix matrix) {
            setColorMatrix(matrix);
        }
    
        public ColorMatrixColorFilter getColorFilter() {
            return mFilter;
        }
    
        public void setColorMatrix(ColorMatrix matrix) {
            mMatrix = matrix;
            mFilter = new ColorMatrixColorFilter(matrix);
        }
    
        public ColorMatrix getColorMatrix() {
            return mMatrix;
        }
    }
    

    Then, the setup code would look something like the following. Note that I have this "setup" in a derived class from ImageView and so I'm doing this in the overriden method setImageBitmap.

    @Override
    public void setImageBitmap(Bitmap bm) {
        final Drawable drawable = new BitmapDrawable(getContext().getResources(), bm);
        setImageDrawable(drawable);
    
        AlphaSatColorMatrixEvaluator evaluator = new AlphaSatColorMatrixEvaluator();
        final AnimateColorMatrixColorFilter filter = new AnimateColorMatrixColorFilter(evaluator.getColorMatrix());
        drawable.setColorFilter(filter.getColorFilter());
    
        ObjectAnimator animator = ObjectAnimator.ofObject(filter, "colorMatrix", evaluator, evaluator.getColorMatrix());
    
        animator.addUpdateListener( new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                drawable.setColorFilter(filter.getColorFilter());
            }
        });
        animator.setDuration(1500);
        animator.start();
    }
    

提交回复
热议问题