渲染 Shader:
BimapShader位图的图像渲染器LinearGradient线性渲染
RadialGradient环形渲染SweepGradient梯度渲染(扫描渲染)
ComposeShader组合渲染
利用bitmapshader我们可以实现圆形等一些列的不规则的突破
/** * TileMode.CLAMP 拉伸最后一个像素去铺满剩下的地方 * TileMode.MIRROR 通过镜像翻转铺满剩下的地方。 * TileMode.REPEAT 重复图片平铺整个画面(电脑设置壁纸) */
bitmapShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
可以利用矩阵对bitmrpshader实现平移 旋转 缩放等一些列操作
width = bitmap.getWidth(); height = bitmap.getHeight(); paint = new Paint(Paint.ANTI_ALIAS_FLAG); Matrix matrix = new Matrix(); matrix.setScale(bitmapRadius * 2.0f / width, bitmapRadius * 2.0f / height); bitmapShader.setLocalMatrix(matrix);
最后设置shader
paint.setShader(bitmapShader);
最后在ondraw中进行绘制
canvas.drawCircle(bitmapRadius, bitmapRadius, bitmapRadius, paint);
关于描边
strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG); strokePaint.setColor(Color.RED); strokePaint.setStrokeWidth(5); strokePaint.setStyle(Paint.Style.STROKE); canvas.drawCircle(bitmapRadius, bitmapRadius, bitmapRadius, strokePaint);
我们还可以利用shapeDrawable去实现圆形图片
ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
shapeDrawable.getPaint().setShader(bitmapShader);
shapeDrawable.setBounds(0, 0, bitmapRadius * 2, bitmapRadius * 2);
shapeDrawable.draw(canvas);
完整代码如下
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.View;
/**
* Created by qianxin on 16/11/24.
*/
public class MyPathShader extends View {
private Bitmap bitmap;
private Paint paint;
private BitmapShader bitmapShader;
private int width;
private int height;
private int bitmapRadius = 100;
private Paint strokePaint;
private int[] colors = {Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW};
private RadialGradient radialGradient;
public MyPathShader(Context context) {
super(context);
bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.zsh)).getBitmap();
strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
strokePaint.setColor(Color.RED);
strokePaint.setStrokeWidth(5);
strokePaint.setStyle(Paint.Style.STROKE);
bitmapShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
width = bitmap.getWidth();
height = bitmap.getHeight();
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Matrix matrix = new Matrix();
matrix.setScale(bitmapRadius * 2.0f / width, bitmapRadius * 2.0f / height);
bitmapShader.setLocalMatrix(matrix);
//将图片根据控件的尺寸进行缩放
paint.setShader(bitmapShader);
}
/**
* TileMode.CLAMP 拉伸最后一个像素去铺满剩下的地方
* TileMode.MIRROR 通过镜像翻转铺满剩下的地方。
* TileMode.REPEAT 重复图片平铺整个画面(电脑设置壁纸)
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
canvas.drawCircle(bitmapRadius, bitmapRadius, bitmapRadius, paint);
canvas.drawCircle(bitmapRadius, bitmapRadius, bitmapRadius, strokePaint);
//用shapeDrawable也可以实现
ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
shapeDrawable.getPaint().setShader(bitmapShader);
shapeDrawable.setBounds(0, 0, bitmapRadius * 2, bitmapRadius * 2);
shapeDrawable.draw(canvas);
canvas.drawCircle(bitmapRadius, bitmapRadius, bitmapRadius, strokePaint);
radialGradient = new RadialGradient(300, 300, 50, colors, null, Shader.TileMode.MIRROR);
paint.setShader(radialGradient);
canvas.drawCircle(300, 300, 150, paint);
}
}