//将bitmap渲染成指定颜色

public static Bitmap tintBitmap(Bitmap inBitmap , int tintColor) {
    if (inBitmap == null) {
        return null;
    }
    Bitmap outBitmap = Bitmap.createBitmap (inBitmap.getWidth(), inBitmap.getHeight() , inBitmap.getConfig());
    Canvas canvas = new Canvas(outBitmap);
    Paint paint = new Paint();
    paint.setColorFilter( new PorterDuffColorFilter(tintColor, PorterDuff.Mode.SRC_IN)) ;
    canvas.drawBitmap(inBitmap , 0, 0, paint) ;
    return outBitmap ;
}

这个方法不是很好,渲染之后的眼色变成一致的颜色了,对于线性图标确实不错,但有时候要的不是这种效果

 if(color!=0){
            float r = ((float)((color>>16)&0xff))/255;
            float g = ((float)((color>>8)&0xff))/255;
            float b = ((float)((color)&0xff))/255;
            float a = ((float)alpha)/255;

            // 生成色彩矩阵
            ColorMatrix colorMatrix = new ColorMatrix(new float[]{
                    r,0,0,0,0,
                    0,g,0,0,0,
                    0,0,b,0,0,
                    0,0,0,a,0
            });
            paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

作者:石头
链接:https://www.jianshu.com/p/62fce0137867

bitmap翻转及镜像

Bitmap convert(Bitmap a, int width, int height)
{
int w = a.getWidth();
int h = a.getHeight();
Bitmap newb = Bitmap.createBitmap(ww, wh, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
Canvas cv = new Canvas(newb);
Matrix m = new Matrix();
m.postScale(1, -1);   //镜像垂直翻转
m.postScale(-1, 1);   //镜像水平翻转
m.postRotate(-90);  //旋转-90度
Bitmap new2 = Bitmap.createBitmap(a, 0, 0, w, h, m, true);
cv.drawBitmap(new2, new Rect(0, 0, new2.getWidth(), new2.getHeight()),new Rect(0, 0, ww, wh), null);
return newb;
}

bitmap镜像绘制
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.mm3);
imageView1.setImageBitmap(bm);

    Bitmap modBm = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
    Canvas canvas = new Canvas(modBm);
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setAntiAlias(true);
    Matrix matrix = new Matrix();
    //matrix.setRotate(90, bm.getWidth()/2, bm.getHeight()/2);
    //matrix.setTranslate(20, 20);
    //镜子效果
    matrix.setScale(-1, 1);
    matrix.postTranslate(bm.getWidth(), 0);

    canvas.drawBitmap(bm, matrix, paint);
    imageView2.setImageBitmap(modBm);
}

最近在做精灵编辑器,需要用到一个可以同时支持旋转,翻转,变色,透明度绘制的方法,所以写了一个包含多种效果的集合方法

    /**
     * 实现图片的旋转 缩放 翻转 设置透明度绘制
     * @param bitmap     图片
     * @param origin_x  旋转 缩放 原点x
     * @param origin_y  旋转 缩放原点 y
     * @param x          绘制x坐标
     * @param y          绘制y坐标
     * @param zoom      缩放比例
     * @param flag      翻转状态 1左右翻转 2上下翻转
     * @param rotate    旋转角度
     * @param alpha     不透明度
     * @param color     颜色
     */
    public static void drawBitmap(Canvas canvas,Bitmap bitmap, float origin_x, float origin_y, float x, float y, float zoom,int flag, float rotate, int alpha, int color){
        Matrix matrix = new Matrix();
        Paint paint = new Paint();
        if (flag == 1) //左右翻转
            matrix.setScale(-1, 1,origin_x,origin_y);
        if (flag == 2)  //上下翻转
            matrix.setScale(1, -1,origin_x,origin_y);
        // 缩放
        matrix.postScale(zoom, zoom,origin_x,origin_y);
        // 旋转
        matrix.postRotate(rotate,origin_x,origin_y);

        matrix.postTranslate(x,y);
        //颜色渲染
        /*
        if(color!=0){
            color = (alpha<<24)|(color&0xffffff);
            paint.setColorFilter( new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN)) ;
            paint.setColor(color);
        }
        */
        if(color!=0){
            float r = ((float)((color>>16)&0xff))/255;
            float g = ((float)((color>>8)&0xff))/255;
            float b = ((float)((color)&0xff))/255;
            float a = ((float)alpha)/255;

            // 生成色彩矩阵
            ColorMatrix colorMatrix = new ColorMatrix(new float[]{
                    r,0,0,0,0,
                    0,g,0,0,0,
                    0,0,b,0,0,
                    0,0,0,a,0
            });
            paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

        }
        else{
            //设置透明度
            paint.setAlpha(alpha);
        }

        canvas.drawBitmap(bitmap,matrix,paint);
    }

超级绘图方法 这个方法主要是为手机CAPP做的

   /**
     * 实现图片的旋转 缩放 翻转 设置透明度绘制
     * @param bitmap     图片
     * @param origin_x  旋转 缩放 原点x
     * @param origin_y  旋转 缩放原点 y
     * @param x          绘制x坐标
     * @param y          绘制y坐标
     * @param zoom_v      纵向缩放比例
     * @param zoom_h      横向缩放比例
     * @param rotate    旋转角度
     * @param color     颜色
     */
    public void drawBitmap(Canvas canvas,Bitmap bitmap, float origin_x, float origin_y, float x, float y,
                           float zoom_h,float zoom_v,float rotate, int color){
        Matrix matrix = new Matrix();
        Paint paint = new Paint();
        // 缩放
        matrix.postScale(zoom_h, zoom_v,origin_x,origin_y);
        // 旋转
        matrix.postRotate(rotate,origin_x,origin_y);

        matrix.postTranslate(x,y);
        //颜色渲染
        /*
        if(color!=0){
            color = (alpha<<24)|(color&0xffffff);
            paint.setColorFilter( new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN)) ;
            paint.setColor(color);
        }
        */
        if(color!=0){
            float r = ((float)((color>>16)&0xff))/255;
            float g = ((float)((color>>8)&0xff))/255;
            float b = ((float)((color)&0xff))/255;
            float a = ((float)alpha)/255;

            // 生成色彩矩阵
            ColorMatrix colorMatrix = new ColorMatrix(new float[]{
                    r,0,0,0,0,
                    0,g,0,0,0,
                    0,0,b,0,0,
                    0,0,0,a,0
            });
            paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

        }

        canvas.drawBitmap(bitmap,matrix,paint);
    }

参考资料:
https://blog.csdn.net/harvic880925/article/details/512539
https://www.jb51.net/article/123593.htm

发表评论

邮箱地址不会被公开。 必填项已用*标注