zoukankan      html  css  js  c++  java
  • Android Shader渲染器:BitmapShader图片渲染

    public class BitmapShader extends Shader

    BitmapShader,  Shader家族的 专门处理图片渲染的


    构造方法:

    public BitmapShader(Bitmap bitmap, TileMode tileX, TileMode tileY)

       bitmap:原图

       tile直译为 瓷砖,瓦片。这里的TileMode 可看成是 铺图的模式。 

       tileX, tileY:x/y 方向铺图的模式


    public enum TileMode { 
        CLAMP   (0),
        REPEAT  (1),
        MIRROR  (2);
        TileMode(int nativeInt) {
            this.nativeInt = nativeInt;
        }
        final int nativeInt;
    }
      CLAMP:假设超出原始bounds(即原图的边界),则反复边缘上的color

      REPEAT:反复bitmap

      MIRROR:反复bitmap。与REPEAT不同的时,它是镜像反复,即:反向反复


    例:

    public class BitmapShaderView extends View {
    
        private BitmapShader mBitmapShader;
        private ShapeDrawable mShapeDrawable;
    
        public BitmapShaderView(Context context, Bitmap bitmap) {
            super(context);
    
            mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    
            mShapeDrawable = new ShapeDrawable(new OvalShape());
            mShapeDrawable.getPaint().setShader(mBitmapShader);
    //        mShapeDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight()); //原图大小
            mShapeDrawable.setBounds(0, 0, bitmap.getWidth() * 2, bitmap.getHeight() * 2);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawColor(Color.CYAN);
    
            mShapeDrawable.draw(canvas);
        }
    }
      在Activity中,setContentView(new BitmapShaderView(context, bitmap));

    原图                                                                                                       效果图

       x和y 边缘反复


    改:mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.MIRROR, Shader.TileMode.REPEAT);

    效果

     x方向镜像反复;y方向反复


    改:mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR);
    效果:

     x方向反复。y方向镜像反复




  • 相关阅读:
    Cannot execute request on any known server
    swagger快速开发
    SpringBoot(七):SpringBoot整合Swagger2
    集群、分布式
    分布式架构--基本思想汇总
    Mysql联合查询union和union all的使用介绍
    Mysql 语句执行顺序
    Spring AOP四种实现方式Demo详解与相关知识探究
    jvm运行时环境属性一览
    使用ObjectOutputStream进行socket通信的时候出现固定读到四个字节乱码的问题
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7053369.html
Copyright © 2011-2022 走看看