Android 颜色渲染(四) BitmapShader位图渲染

转载自:http://blog.csdn.net/t12x3456/article/details/10366063


Android 颜色处理(四) 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方向渲染器平铺模式

TileMode:

CLAMP  :如果渲染器超出原始边界范围,会复制范围内边缘染色。

REPEAT :横向和纵向的重复渲染器图片,平铺。

MIRROR :横向和纵向的重复渲染器图片,这个和REPEAT重复方式不一样,他是以镜像方式平铺。


首先看一下效果图:



                                                                                                           

还是直接上代码:

MainActivity:

[java]  view plain copy
  1. package com.tony.shader;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5.   
  6. public class MainActivity extends Activity {  
  7.   
  8.     private BitmapShaderView shaderView;  
  9.       
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.   
  14.         shaderView = new BitmapShaderView(this);  
  15.         setContentView(shaderView);  
  16.   
  17.     }  
  18.   
  19. }  

BitmapShaderView:

[java]  view plain copy
  1. package com.tony.shader;  
  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.util.AttributeSet;  
  13. import android.view.View;  
  14.   
  15. public class BitmapShaderView extends View {  
  16.   
  17.     private BitmapShader bitmapShader = null;  
  18.     private Bitmap bitmap = null;  
  19.     private Paint paint = null;  
  20.     private ShapeDrawable shapeDrawable = null;  
  21.     private int BitmapWidth = 0;  
  22.     private int BitmapHeight = 0;  
  23.   
  24.     public BitmapShaderView(Context context) {  
  25.         super(context);  
  26.   
  27.         // 得到图像  
  28.         bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.cat))  
  29.                 .getBitmap();  
  30.         BitmapWidth = bitmap.getWidth();  
  31.         BitmapHeight = bitmap.getHeight();  
  32.         // 构造渲染器BitmapShader  
  33.         bitmapShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR,Shader.TileMode.REPEAT);  
  34.     }  
  35.       
  36.     public BitmapShaderView(Context context,AttributeSet set) {  
  37.         super(context, set);  
  38.     }  
  39.       
  40.       
  41.     @Override  
  42.     protected void onDraw(Canvas canvas) {  
  43.         // TODO Auto-generated method stub  
  44.     super.onDraw(canvas);  
  45.     //将图片裁剪为椭圆形      
  46.         //构建ShapeDrawable对象并定义形状为椭圆      
  47.         shapeDrawable = new ShapeDrawable(new OvalShape());    
  48.         //得到画笔并设置渲染器    
  49.         shapeDrawable.getPaint().setShader(bitmapShader);    
  50.         //设置显示区域    
  51.         shapeDrawable.setBounds(2020,BitmapWidth-140,BitmapHeight);    
  52.         //绘制shapeDrawable    
  53.         shapeDrawable.draw(canvas);    
  54.     }  
  55.   
  56. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值