ComposeShader组合渲染

public ComposeShader(Shader shaderA,Shader shaderB, Xfermode mode)

Parameters
shaderA 渲染器A,Shader及其子类对象
shaderB 渲染器B,Shader及其子类对象
mode  两种渲染器组合的模式,Xfermode对象

public ComposeShader(Shader shaderA,Shader shaderB, PorterDuff.Mode mode)

Parameters

shaderA 渲染器A,Shader及其子类对象
shaderB 渲染器B,Shader及其子类对象
mode .两种渲染器组合的模式,ProterDuff.Mode对象

主要代码:

public class ComposeShaderActivity extends Activity {
    ImageView image,image2,image3,image4;
    int width,height;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        image = (ImageView) findViewById(R.id.image);
        image2 = (ImageView) findViewById(R.id.image2);
        image3 = (ImageView) findViewById(R.id.image3);
        image4 = (ImageView) findViewById(R.id.image4);
        width = ValueUtil.dp2px(this, 120);
        height = ValueUtil.dp2px(this, 120);


        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tupian);

        Bitmap bitmapTemp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmapTemp);
        // 创建BitmapShader对象
        BitmapShader mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR,
                Shader.TileMode.MIRROR);
        // 创建LinearGradient并设置渐变颜色数组,平铺效果为镜像
        LinearGradient mLinearGradient = new LinearGradient(0, 0, 0, height, new int[] {
                Color.WHITE, Color.LTGRAY, Color.TRANSPARENT, Color.GREEN }, null,
                Shader.TileMode.MIRROR);
        // 混合渲染 将两个效果叠加,使用PorterDuff叠加模式
        ComposeShader mComposeShader = new ComposeShader(mBitmapShader, mLinearGradient,  PorterDuff.Mode.MULTIPLY);
        Paint paint = new Paint();
        paint.setShader(mComposeShader);
        canvas.drawRect(new RectF(0, 0, width, height), paint);
        image.setImageBitmap(bitmapTemp);
    }
}

效果图:

ComposeShader组合渲染_第1张图片


参考资料:http://blog.csdn.net/t12x3456/article/details/10366131


源码:http://yunpan.cn/c3f2qgcVtNSwP (提取码:2ccf)


你可能感兴趣的:(渲染)