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. }  
  • 相关阅读:
    5+ App开发Native.js入门指南
    uni-app vue-cli命令行
    本地uni-app原生插件提交云端打包
    编写package.json文件
    生成插件包
    无障碍角色 accessibilityRole (iOS, Android)
    ajax 整理
    Javascript分享笔记
    echarts 横坐标分行展示,以及文字显示顶部
    js总结(11)js作用域
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/6722136.html
Copyright © 2011-2022 走看看