zoukankan      html  css  js  c++  java
  • Android学习笔记进阶16之BitmapShader

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

    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

    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(20, 20,BitmapWidth-60,BitmapHeight-60);  
    43.         //绘制shapeDrawable  
    44.         shapeDrawable.draw(canvas);  
    45.     }  
    46. }  
  • 相关阅读:
    第二个月课堂004讲解python之实战之元组(003)_高级讲师肖sir
    第二个月课堂004讲解python之实战之列表(002)_高级讲师肖sir
    多测师课堂_mysql之报错日志(001)高级讲师肖sir
    多测师课堂012_mysql之存储过程(练习和答案)高级讲师肖sir
    linux alias 命令 查看系统设置的命令别名
    前端 CSS 介绍
    前端 CSS语法
    前端 CSS 注释
    前端 CSS 目录
    linux echo 命令 打印字符串
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6722136.html
Copyright © 2011-2022 走看看