Android 颜色渲染

Shader

该类作为基类主要是返回绘制时颜色的横向跨度。其子类可以作用与Piant。通过 paint.setShader(Shader shader);来实现一些渲染效果。

方法:
boolean getLoaclMatrix(Matrix localM);
如果shader有一个非本地的矩阵将返回true. localM:如果不为null将被设置为shader的本地矩阵.

void setLocalMatrix(Matrix localM);
设置shader的本地矩阵,如果localM为空将重置shader的本地矩阵。

子类:

  • BitmapShader:位图图像渲染
  • LinearGradient:线性渲染
  • RadialGradient:环形渲染
  • SweepGradient:扫描渐变渲染/梯度渲染
  • ComposeShader:组合渲染

使用步骤:

  1. 构建shader对象,一般是子类
  2. 通过Paint的setShader方法设置渲染对象
  3. 绘制时使用这个Paint对象

BitmapShader位图渲染

public   BitmapShader(Bitmap bitmap,Shader.TileMode tileX,Shader.TileMode tileY)
调用这个方法来产生一个画有一个位图的渲染器(Shader)。
bitmap   在渲染器内使用的位图
tileX      The tiling mode for x to draw the bitmap in.   在位图上X方向渲染器平铺模式
tileY     The tiling mode for y to draw the bitmap in.    在位图上Y方向渲染器平铺模式

实例:

public class BitmapShaderView extends View {

    private BitmapShader bitmapShader = null;
    private Bitmap bitmap = null;
    private Paint paint = null;
    private ShapeDrawable shapeDrawable = null;
    private int BitmapWidth = 0;
    private int BitmapHeight = 0;

    public BitmapShaderView(Context context) {
        super(context);

        // 得到图像
        bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.a))
                .getBitmap();
        BitmapWidth = bitmap.getWidth();
        BitmapHeight = bitmap.getHeight();
        // 构造渲染器BitmapShader
        bitmapShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR,Shader.TileMode.REPEAT);
    }

    public BitmapShaderView(Context context, AttributeSet set) {
        super(context, set);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //将图片裁剪为椭圆形
        //构建ShapeDrawable对象并定义形状为椭圆
        shapeDrawable = new ShapeDrawable(new OvalShape());
        //得到画笔并设置渲染器
        shapeDrawable.getPaint().setShader(bitmapShader);
        //设置显示区域
        shapeDrawable.setBounds(20, 20,1000,BitmapHeight);
        //绘制shapeDrawable
        shapeDrawable.draw(canvas);
    }

}

LinearGradient线性渲染

public LinearGradient(float x0, float y0, float x1, float y1, int[] colors, float[] positions,Shader.TileMode tile) 
参数:
float x0: 渐变起始点x坐标
float y0:渐变起始点y坐标
float x1:渐变结束点x坐标
float y1:渐变结束点y坐标
int[] colors:颜色 的int 数组
float[] positions: 相对位置的颜色数组,可为null,  若为null,可为null,颜色沿渐变线均匀分布
Shader.TileMode tile: 渲染器平铺模式

===========
public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1,Shader.TileMode tile)
float x0: 渐变起始点x坐标
float y0:渐变起始点y坐标
float x1:渐变结束点x坐标
float y1:渐变结束点y坐标
int color0: 起始渐变色
int color1: 结束渐变色
Shader.TileMode tile: 渲染器平铺模式

CLAMP:的作用是如果渲染器超出原始边界范围,则会复制边缘颜色对超出范围的区域进行着色
REPEAT:的作用是在横向和纵向上以平铺的形式重复渲染位图
MIRROR:的作用是在横向和纵向上以镜像的方式重复渲染位图

一个例子:

public class LinearGradientView extends View {

    private LinearGradient linearGradient = null;
    private Paint paint = null;

    public LinearGradientView(Context context)
    {
        super(context);
        linearGradient = new LinearGradient(0, 0, 100, 100, new int[] {
                Color.YELLOW, Color.GREEN, Color.TRANSPARENT, Color.WHITE }, new float[]{0.2f, 0.5f, 0.8f, 1.0f},
                Shader.TileMode.REPEAT);
        paint = new Paint();
    }

    public LinearGradientView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        //设置渲染器
        paint.setShader(linearGradient);
        //绘制圆环
        canvas.drawCircle(240, 360, 200, paint);
    }

}

RadialGradient 环形渲染

public RadialGradient(float x, float y, float radius, int[] colors, float[] positions,Shader.TileMode tile)
float x:  圆心X坐标
float y:  圆心Y坐标
float radius: 半径
int[] colors:  渲染颜色数组
floate[] positions: 相对位置数组,可为null,  若为null,可为null,颜色沿渐变线均匀分布
Shader.TileMode tile:渲染器平铺模式

public RadialGradient(float x, float y, float radius, int color0, int color1,Shader.TileMode tile)
float x:  圆心X坐标
float y:  圆心Y坐标
float radius: 半径
int color0: 圆心颜色
int color1: 圆边缘颜色
Shader.TileMode tile:渲染器平铺模式

一个例子:

public class RadialGradientView extends View {

    Paint mPaint = null;
    // 环形渐变渲染
    Shader mRadialGradient = null;
    public RadialGradientView(Context context) {
        super(context);
        //1.圆心X坐标2.Y坐标3.半径 4.颜色数组 5.相对位置数组,可为null 6.渲染器平铺模式
        mRadialGradient = new RadialGradient(240, 240, 240, new int[] {
                Color.YELLOW, Color.GREEN, Color.TRANSPARENT, Color.RED }, null,
                Shader.TileMode.REPEAT);

        mPaint = new Paint();
    }


    public RadialGradientView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // 绘制环形渐变
        mPaint.setShader(mRadialGradient);
        // 第一个,第二个参数表示圆心坐标
        // 第三个参数表示半径
        canvas.drawCircle(240, 360, 200, mPaint);
    }
}

RadialGradient 环形渲染实现水波纹效果

public class WaterRipplesView extends View {
    Paint mPaint = null;
    Shader mRadialGradient = null;

    public WaterRipplesView(Context context) {
        super(context);
        mPaint = new Paint();
    }

    public WaterRipplesView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        mRadialGradient = new RadialGradient(300, 300, 48,
                new int[] { Color.WHITE, Color.TRANSPARENT },null, Shader.TileMode.REPEAT);

        if (mRadialGradient != null) {
            mPaint.setShader(mRadialGradient);
            canvas.drawCircle(300, 300, 200, mPaint);
        }

    }
}

SweepGradient扫描/梯度渲染

public SweepGradient(float cx, float cy, int[] colors, float[] positions)
Parameters:

cx    渲染中心点x 坐标
cy    渲染中心y 点坐标
colors    围绕中心渲染的颜色数组,至少要有两种颜色值
positions    相对位置的颜色数组,可为null,  若为null,可为null,颜色沿渐变线均匀分布

public SweepGradient(float cx, float cy, int color0, int color1)
Parameters:

cx    渲染中心点x 坐标
cy    渲染中心点y 坐标
color0    起始渲染颜色
color1    结束渲染颜色

ComposeShader组合渲染

results matching ""

    No results matching ""