android BitmapShader类

来源:互联网 发布:win10 安装ubuntu 分区 编辑:程序博客网 时间:2024/05/17 23:04

<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>具体实现

 

[java] view plain copy
  1. package xiaosi.BitmapShader;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. public class BitmapShaderActivity extends Activity {  
  7.     /** Called when the activity is first created. */  
  8.     private BitmapShaders bitmapShaders = null;  
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.           
  13.         bitmapShaders = new BitmapShaders(this);  
  14.         setContentView(bitmapShaders);  
  15.     }  
  16. }  


 

BitmapShaders.java

[java] view plain copy
  1. package xiaosi.BitmapShader;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapShader;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Paint;  
  8. import android.graphics.Shader;  
  9. import android.graphics.drawable.BitmapDrawable;  
  10. import android.graphics.drawable.ShapeDrawable;  
  11. import android.graphics.drawable.shapes.OvalShape;  
  12. import android.view.View;  
  13.   
  14. public class BitmapShaders extends View  
  15. {  
  16.     private  BitmapShader bitmapShader = null;  
  17.     private Bitmap bitmap = null;  
  18.     private Paint paint = null;  
  19.     private ShapeDrawable shapeDrawable = null;  
  20.     private int BitmapWidth  = 0;  
  21.     private int BitmapHeight = 0;  
  22.     public BitmapShaders(Context context)  
  23.     {  
  24.         super(context);  
  25.         //得到图像  
  26.         bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.h)).getBitmap();    
  27.         BitmapWidth = bitmap.getWidth();  
  28.         BitmapHeight = bitmap.getHeight();  
  29.         //构造渲染器BitmapShader  
  30.         bitmapShader = new BitmapShader(bitmap,Shader.TileMode.MIRROR,Shader.TileMode.REPEAT);  
  31.     }  
  32.     @Override  
  33.     protected void onDraw(Canvas canvas)  
  34.     {  
  35.         super.onDraw(canvas);  
  36.         //将图片裁剪为椭圆形    
  37.         //构建ShapeDrawable对象并定义形状为椭圆    
  38.         shapeDrawable = new ShapeDrawable(new OvalShape());  
  39.         //得到画笔并设置渲染器  
  40.         shapeDrawable.getPaint().setShader(bitmapShader);  
  41.         //设置显示区域  
  42.         shapeDrawable.setBounds(2020,BitmapWidth-60,BitmapHeight-60);  
  43.         //绘制shapeDrawable  
  44.         shapeDrawable.draw(canvas);  
  45.     }  
  46. }  
0 0
原创粉丝点击