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方向镜像反复




  • 相关阅读:
    快速排序
    冒泡排序
    mysql 拷贝表插入新的表
    http协议
    nginx错误日志error_log日志级别
    MySQL数据库远程访问的权限
    mysql create database 指定utf-8编码
    MYSQL日志
    linux常用命令
    java学习--基础知识进阶第六天--集合&迭代器、增强for & 泛型、常见数据结构、List子体系
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7053369.html
Copyright © 2011-2022 走看看