LinearGradient线性渲染


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.view.View;

/**
 * @version 2012-8-14 上午10:45:28
 **/
public class myGameView extends View implements Runnable {
    int bitwidth = 0;
    int bitheight = 0;
    Paint mPaint = null;
    // 线性渐变渲染
    Shader mLinearGradient1 = null;
    // 线性渐变渲染
    Shader mLinearGradient2 = null;
    // 线性渐变渲染
    Shader mLinearGradient3 = null;

    public myGameView(Context context) {
        super(context);
        // 创建LinearGradient并设置渐变颜色数组
        // 第一个,第二个參数表示渐变起点 能够设置起点终点在对角等任何位置
        // 第三个,第四个參数表示渐变终点
        // 第五个參数表示渐变颜色
        // 第六个參数能够为空,表示坐标,值为0-1 new float[] {0.25f, 0.5f, 0.75f, 1 }
        // 假设这是空的,颜色均匀分布,沿梯度线。
        // 第七个表示平铺方式
        // CLAMP反复最后一个颜色至最后
        // MIRROR反复着色的图像水平或垂直方向已镜像方式填充会有翻转效果
        // REPEAT反复着色的图像水平或垂直方向
        mLinearGradient1 = new LinearGradient(0, 0, 0, 100, new int[] {
                Color.RED, Color.GREEN, Color.BLUE, Color.WHITE }, null,
                Shader.TileMode.CLAMP);
        mLinearGradient2 = new LinearGradient(0, 0, 0, 100, new int[] {
                Color.RED, Color.GREEN, Color.BLUE, Color.WHITE }, null,
                Shader.TileMode.MIRROR);
        mLinearGradient3 = new LinearGradient(0, 0, 0, 100, new int[] {
                Color.RED, Color.GREEN, Color.BLUE, Color.WHITE }, null,
                Shader.TileMode.REPEAT);
        mPaint = new Paint();
        new Thread(this).start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // LinearGradient的高度仅仅有100而绘制的矩形有200所以才会有反复
        // 假设高度两者同样clamp反复是看不出效果的
        Paint paint = new Paint();
        paint.setTextSize(20);
        paint.setColor(Color.WHITE);
        // 绘制渐变的矩形
        mPaint.setShader(mLinearGradient1);
        canvas.drawRect(0, 0, 200, 200, mPaint);
        canvas.drawText("CLAMP", 0, 200 + 20, paint);
        // 绘制渐变的矩形
        mPaint.setShader(mLinearGradient2);
        canvas.drawRect(0, 250, 200, 450, mPaint);
        canvas.drawText("MIRROR", 0, 450 + 20, paint);
        // 绘制渐变的矩形
        mPaint.setShader(mLinearGradient3);
        canvas.drawRect(0, 500, 200, 700, mPaint);
        canvas.drawText("REPEAT", 0, 700 + 20, paint);
    }

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

posted @ 2015-02-02 16:09  phlsheji  阅读(222)  评论(0编辑  收藏  举报