美文网首页
Gradient Alpha Tranform

Gradient Alpha Tranform

作者: 弄码哥nomag | 来源:发表于2016-08-16 15:58 被阅读0次

    Gradient Alpha Tranform

    工作中遇到设计给的效果是,图片要从左到右实现一个 alpha 渐变的的效果。当时做的时候花费了蛮长时间的。特此记录一下。

    public class GradientAlphaTransform implements Transformation {
    
        /**
         * A unique key for the transformation, used for caching purposes.
         */
        private static final String KEY = "GradientAlphaTransform";
    
        @Override
        public Bitmap transform(Bitmap source) {
    
            // Init shader
            Shader gradientAlphaShader = new LinearGradient(
                    0,
                    0,
                    source.getWidth(),
                    0,
                    0x99ffffff,
                    0x00ffffff,
                    Shader.TileMode.CLAMP);
    
            // Init paint
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
            paint.setShader(gradientAlphaShader);
    
            // Create and draw circle bitmap
            Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), source.getConfig());
            Canvas canvas = new Canvas(output);
            canvas.drawBitmap(source, 0, 0, null);
            canvas.drawRect(new RectF(0, 0, source.getWidth(), source.getHeight()), paint);
    
            // Recycle the source bitmap, because we already generate a new one
            source.recycle();
            return output;
        }
    
        @Override
        public String key() {
            return KEY;
        }
    }
    

    相关文章

      网友评论

          本文标题:Gradient Alpha Tranform

          本文链接:https://www.haomeiwen.com/subject/coppsttx.html