Shader与MaskFilter

public class

Shader

extends  Object
java.lang.Object
   ↳ android.graphics.Shader
Known Direct Subclasses

Shader里有个内部类Shader.TileMode

Shader.TileMode.CLAMP   如果渲染器超出原始边界范围,则会复制边缘颜色对超出范围的区域进行着色

Shader.TileMode.MIRROR 在横向和纵向上以镜像的方式重复渲染位图

Shader.TileMode.REPEAT  在横向和纵向上以平铺的形式重复渲染位图




BitmapShader(图像渲染)

Public Constructors
BitmapShader( Bitmap bitmap,  Shader.TileMode tileX,  Shader.TileMode tileY)
Call this to create a new shader that will draw with a bitmap.
bitmap 指定Bitmap

tileX   指定X方向的Bitmap 渲染方式

tileY 指定Y轴方向Bitmap的渲染方式

例如:

	Shader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
	mPaint.setShader(bitmapShader);

BitmapShader先应用了Y轴的模式而X轴是后应用的



RadialGradient(环形渲染)

Public Constructors
RadialGradient(float centerX, float centerY, float radius, int[] colors, float[] stops,  Shader.TileMode tileMode)
Create a shader that draws a radial gradient given the center and radius.
RadialGradient(float centerX, float centerY, float radius, int centerColor, int edgeColor,  Shader.TileMode tileMode)
Create a shader that draws a radial gradient given the center and radius.
centerX 、centerY、radius,表示渲染的中心点和渲染半径;

coloers 里是颜色值数组

stops表示colors里颜色数组的权重,范围是[ 0 , 1],为null表示colors的颜色平分半径距离


class SampleView extends View {

		private int width;
		private int height;
		
		private float radius;
		Paint mPaint = null;
		
		Shader mRadialGradient = null;

		public SampleView(Context context) {
			super(context);
			
			width = getResources().getDisplayMetrics().widthPixels;
			height = getResources().getDisplayMetrics().heightPixels;
			radius = Math.min(width, height)/2;
			
			mRadialGradient = new RadialGradient(
					width/2, 
					height/2, 
					radius, 
					new int[] {Color.WHITE, Color.RED, Color.BLUE, Color.GREEN }, 
					null,
					Shader.TileMode.CLAMP);

			mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
			
		}

		@Override
		protected void onDraw(Canvas canvas) {
			
			mPaint.setShader(mRadialGradient);
			
			canvas.drawCircle(width/2, height/2, radius, mPaint);
		}
	}





mRadialGradient = new RadialGradient(
					width/2, 
					height/2-200, 
					radius, 
					new int[] {Color.WHITE, Color.RED, Color.BLUE, Color.GREEN }, 
					null,
					Shader.TileMode.CLAMP);



我们修改半径-200,使用Shader.TileMode.MIRROR渲染模式,先按指定x、y点和半径画完,然后以Shader.TileMode.MIRROR 画完剩余部分

	mRadialGradient = new RadialGradient(
					width/2, 
					height/2, 
					radius-200, 
					new int[] {Color.WHITE, Color.RED, Color.BLUE, Color.GREEN }, 
					null,
					Shader.TileMode.MIRROR);



到现在都没有使用stops数组参数


	mRadialGradient = new RadialGradient(
					width/2, 
					height/2, 
					radius, 
					new int[] {Color.WHITE, Color.RED, Color.BLUE, Color.GREEN }, 
					new float[]{0.2f,0.4f,0.6f,1f},
					Shader.TileMode.MIRROR);

0.2 、0.4 、0.6 、1的意思是白色在20%的地方,红色在40%的地方,蓝色在60%的地方,绿色在100%的地方,

0-20%是白色的渐变,

20%-40%是白色到红色的渐变

40% - 60% 是红色到蓝色的渐变

60% - 100% 是蓝色到绿色的渐变

 


第二个构造函数比较简单

mRadialGradient = new RadialGradient(
					width/2, 
					height/2, 
					radius,
					Color.RED,
					Color.BLUE,
					Shader.TileMode.MIRROR);
定义一个开始色(0.5f)和结束色(1f),



LinearGradient(线性渲染)

Public Constructors
LinearGradient(float x0, float y0, float x1, float y1, int[] colors, float[] positions,  Shader.TileMode tile)
Create a shader that draws a linear gradient along a line.
LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1,  Shader.TileMode tile)
Create a shader that draws a linear gradient along a line.

跟环形渲染差不多,主要是前面的4个参数,前2个表示开始点,后2个表示结束点,这2个坐标连接而成就是一条线,那么这条线是什么方向,那么渲染就在那个方向。

	mLinearGradient1 = new LinearGradient(
					0,0, 500, 0, 
					new int[] {Color.RED, Color.GREEN, Color.BLUE, Color.WHITE }, 
					null,
					Shader.TileMode.MIRROR);

			mLinearGradient2 = new LinearGradient(
					0, 0, 0, 500, 
					new int[] {Color.RED, Color.GREEN, Color.BLUE, Color.WHITE }, 
					null,
					Shader.TileMode.MIRROR);

			mLinearGradient3 = new LinearGradient(
					0, 0, 500, 500, 
					new int[] {Color.RED, Color.GREEN, Color.BLUE, Color.WHITE },
					null,
					Shader.TileMode.REPEAT);




SweepGradient(梯度渲染)

Public Constructors
SweepGradient(float cx, float cy, int[] colors, float[] positions)
A subclass of Shader that draws a sweep gradient around a center point.
SweepGradient(float cx, float cy, int color0, int color1)
A subclass of Shader that draws a sweep gradient around a center point.
cx 、cy表示在View里的坐标位置,后面的参数跟环形渲染一样


	@Override
		protected void onDraw(Canvas canvas) {
			 Shader   mSweepGradient1 = new SweepGradient(
		        		350, 105,
		        		new int[] { Color.GREEN,  Color.RED, Color.BLUE }, null);  
	        mPaint.setShader(mSweepGradient1);  
	        canvas.drawRect(200, 10, 500, 200, mPaint);  
	        
	        
	        Shader   mSweepGradient2 = new SweepGradient(
	        		350, 360,
	        		new int[] { Color.GREEN,  Color.RED, Color.BLUE }, null);  
            mPaint.setShader(mSweepGradient2);  
	        canvas.drawOval(new RectF(200, 320, 500, 400), mPaint);
	        
	        Shader   mSweepGradient3 = new SweepGradient(
	        		500, 700,
	        		new int[] { Color.GREEN,  Color.RED, Color.BLUE }, null);  
            mPaint.setShader(mSweepGradient3);  
	        canvas.drawCircle(500, 700, 200, mPaint);
	        
	        mPaint.setStyle(Style.STROKE);
	        mPaint.setStrokeWidth(50);
	       
	        Shader   mSweepGradient4 = new SweepGradient(
	        		225, 1200,
	        		new int[] { Color.GREEN,  Color.RED, Color.BLUE ,Color.GREEN}, null);  
            mPaint.setShader(mSweepGradient4);  
	        canvas.drawCircle(225, 1200, 150, mPaint);
	        
	        
	        
	        Shader   mSweepGradient5 = new SweepGradient(
	        		600, 1200,
	        		new int[] { Color.GREEN,  Color.RED, Color.BLUE}, null);  
            mPaint.setShader(mSweepGradient5);  
	        canvas.drawCircle(600, 1200, 150, mPaint);
	        
	        
	        
	        Shader   mSweepGradient6 = new SweepGradient(
	        		1000, 1200,
	        		new int[] { Color.GREEN,  Color.RED, Color.BLUE}, new float[]{0.1f,0.3f,1});  
            mPaint.setShader(mSweepGradient6);  
	        canvas.drawCircle(1000, 1200, 150, mPaint);
	        
		}






ComposeShader(组合渲染)

Public Constructors
ComposeShader( Shader shaderA,  Shader shaderB,  Xfermode mode)
Create a new compose shader, given shaders A, B, and a combining mode.
ComposeShader( Shader shaderA,  Shader shaderB,  PorterDuff.Mode mode)
Create a new compose shader, given shaders A, B, and a combining PorterDuff mode.
将多个种渲染组合在一起,区别是第三个参数




public class

MaskFilter

extends  Object
java.lang.Object
   ↳ android.graphics.MaskFilter
Known Direct Subclasses
mPaint.setMaskFilter(new EmbossMaskFilter(new float[]{0,1,1}, 0.5f, 2.3f, 20));
从正上方打光


光源在哪个方向好像是这样的(我自己的理解)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值