ComposeShader混合渲染

package shader.yb;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.view.View;

/**
 * @author yubin
 * @version 2012-8-14 上午10:45:28
 **/
public class ComposeGameView extends View implements Runnable {
    Bitmap mBitmap = null;
    int bitwidth = 0;
    int bitheight = 0;
    Paint mPaint = null;
    // bitmap渲染
    Shader mBitmapShader = null;
    // 线性渐变渲染
    Shader mLinearGradient = null;
    // 混合渲染
    Shader mComposeShader = null;

    ShapeDrawable mShapeDrawable = null;

    public ComposeGameView(Context context) {
        super(context);
        // 装载资源
        mBitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.abc))
                .getBitmap();
        // 得到宽高
        bitwidth = mBitmap.getWidth();
        bitheight = mBitmap.getHeight();
        // 创建BitmapShader对象
        mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT,
                Shader.TileMode.MIRROR);
        // 创建LinearGradient并设置渐变颜色数组
        mLinearGradient = new LinearGradient(0, 0, 0, 100, new int[] {
                Color.RED, Color.GREEN, Color.BLUE, Color.WHITE }, null,
                Shader.TileMode.MIRROR);
        // 混合渲染 将两个效果叠加
        mComposeShader = new ComposeShader(mBitmapShader, mLinearGradient,
                PorterDuff.Mode.LIGHTEN);

        mPaint = new Paint();
        new Thread(this).start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // 绘制混合渲染效果
        mPaint.setShader(mComposeShader);
        canvas.drawRect(0, 0, bitwidth + 300, bitheight + 300, mPaint);
    }

    @Override
    public void run() {
        while(!Thread.currentThread().isInterrupted()) {
            try {
                Thread.sleep(100);
            }
            catch(Exception e) {
                Thread.currentThread().interrupt();
            }
            postInvalidate();
        }
    }
}
PorterDuff.Mode

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