<1>简介
具体的看一下博文:Android学习笔记进阶15之Shader渲染
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方向花砖模式
TileMode:(一共有三种)
CLAMP :如果渲染器超出原始边界范围,会复制范围内边缘染色。
REPEAT :横向和纵向的重复渲染器图片,平铺。
MIRROR :横向和纵向的重复渲染器图片,这个和REPEAT 重复方式不一样,他是以镜像方式平铺。
还是不太明白?那看一下效果图吧!
REPEAT MIRROR
<2>具体实现
- packagexiaosi.BitmapShader;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- publicclassBitmapShaderActivityextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- privateBitmapShadersbitmapShaders=null;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- bitmapShaders=newBitmapShaders(this);
- setContentView(bitmapShaders);
- }
- }
BitmapShaders.Java
- packagexiaosi.BitmapShader;
- importandroid.content.Context;
- importandroid.graphics.Bitmap;
- importandroid.graphics.BitmapShader;
- importandroid.graphics.Canvas;
- importandroid.graphics.Paint;
- importandroid.graphics.Shader;
- importandroid.graphics.drawable.BitmapDrawable;
- importandroid.graphics.drawable.ShapeDrawable;
- importandroid.graphics.drawable.shapes.OvalShape;
- importandroid.view.View;
- publicclassBitmapShadersextendsView
- {
- privateBitmapShaderbitmapShader=null;
- privateBitmapbitmap=null;
- privatePaintpaint=null;
- privateShapeDrawableshapeDrawable=null;
- privateintBitmapWidth=0;
- privateintBitmapHeight=0;
- publicBitmapShaders(Contextcontext)
- {
- super(context);
- //得到图像
- bitmap=((BitmapDrawable)getResources().getDrawable(R.drawable.h)).getBitmap();
- BitmapWidth=bitmap.getWidth();
- BitmapHeight=bitmap.getHeight();
- //构造渲染器BitmapShader
- bitmapShader=newBitmapShader(bitmap,Shader.TileMode.MIRROR,Shader.TileMode.REPEAT);
- }
- @Override
- protectedvoidonDraw(Canvascanvas)
- {
- super.onDraw(canvas);
- //将图片裁剪为椭圆形
- //构建ShapeDrawable对象并定义形状为椭圆
- shapeDrawable=newShapeDrawable(newOvalShape());
- //得到画笔并设置渲染器
- shapeDrawable.getPaint().setShader(bitmapShader);
- //设置显示区域
- shapeDrawable.setBounds(20,20,BitmapWidth-60,BitmapHeight-60);
- //绘制shapeDrawable
- shapeDrawable.draw(canvas);
- }
- }