android 图形处理

来源:互联网 发布:200w数据查20w 编辑:程序博客网 时间:2024/06/05 19:11

声明:此文非本人原创,为整理网络资料加自己的一些注解所得。

一。canvas(画布)
操作类型     相关API     备注
绘制颜色     drawColor, drawRGB, drawARGB     使用单一颜色填充整个画布
绘制基本形状     drawPoint, drawPoints, drawLine, drawLines, drawRect, drawRoundRect, drawOval, drawCircle, drawArc     依次为 点、线、矩形、圆角矩形、椭圆、圆、圆弧
绘制图片     drawBitmap, drawPicture     绘制位图和图片
绘制文本     drawText, drawPosText, drawTextOnPath     依次为 绘制文字、绘制文字时指定每个文字位置、根据路径绘制文字
绘制路径     drawPath     绘制路径,绘制贝塞尔曲线时也需要用到该函数
顶点操作     drawVertices, drawBitmapMesh     通过对顶点操作可以使图像形变,drawVertices直接对画布作用、 drawBitmapMesh只对绘制的Bitmap作用
画布剪裁     clipPath, clipRect     设置画布的显示区域
画布快照     save, restore, saveLayerXxx, restoreToCount, getSaveCount     依次为 保存当前状态、 回滚到上一次保存的状态、 保存图层状态、 回滚到指定状态、 获取保存次数
画布变换     translate, scale, rotate, skew     依次为 位移、缩放、 旋转、错切
Matrix(矩阵)     getMatrix, setMatrix, concat     实际上画布的位移,缩放等操作的都是图像矩阵Matrix, 只不过Matrix比较难以理解和使用,故封装了一些常用的方法。

// 1.创建一个画笔
private Paint mPaint = new Paint();

// 2.初始化画笔
private void initPaint() {
    mPaint.setColor(Color.BLACK);       //设置画笔颜色
    mPaint.setStyle(Paint.Style.FILL);  //设置画笔模式为填充
    mPaint.setStrokeWidth(10f);         //设置画笔宽度为10px
}

二。Matrix类,Matrix是一个3 x 3的矩阵,他对图片的处理分为四个基本类型:

1、Translate————平移变换
2、Scale     ————缩放变换
3、Rotate    ————旋转变换
4、Skew     ————错切变换
在Android的API里对于每一种变换都提供了三种操作方式:set(用于设置Matrix中的值)、post(后乘,根据矩阵的原理,相当于左乘)、pre(先乘,相当于矩阵中的右乘)。默认时,这四种变换都是围绕(0,0)点变换的,当然可以自定义围绕的中心点,通常围绕中心点。
1.平移,在对图片处理的过程中,最常用的就是对图片进行平移操作,该方法为setTranslate(),平移意味着在x轴和y轴上简单地移动图像。setTranslate方法采用两个浮点数作为参数,表示在每个轴上移动的数量。第一个参数是图像将在x轴上移动的数量,而第二个参数是图像将在y轴上移动的数量。在x轴上使用正数进行平移将向右移动图像,而使用负数将向左移动图像。在y轴上使用正数进行平移将向下移动图像,而使用负数将向上移动图像。
2.缩放,Matrix类中另一个有用的方法是setScale方法。它采用两个浮点数作为参数,分别表示在每个轴上所产生的缩放量。第一个参数是x轴的缩放比例,而第二个参数是y轴的缩放比例。如:matrix.setScale(1.5f,1);
3.旋转内置的方法之一是setRotate方法。它采用一个浮点数表示旋转的角度。围绕默认点(0,0),正数将顺时针旋转图像,而负数将逆时针旋转图像,其中默认点是图像的左上角,如:
    Matrix matrix = new Matrix();  
    matrix.setRotate(15);      
也可以使用旋转的角度及围绕的旋转点作为参数调用setRotate方法。选择图像的中心点作为旋转点,如:

    matrix.setRotate(15,bmp.getWidth()/2,bmp.getHeight()/2); 

//按照高度和宽度要求先压缩图片

/*** * 图片的缩放方法 * * @param bgimage   :源图片资源 * @param newWidth  :缩放后宽度 * @param newHeight :缩放后高度 */public static Bitmap zoomImage(Bitmap bgimage, double newWidth,                               double newHeight) {    // 获取这个图片的宽和高    float width = bgimage.getWidth();    float height = bgimage.getHeight();    // 创建操作图片用的matrix对象    Matrix matrix = new Matrix();    // 计算宽高缩放率    float scaleWidth = ((float) newWidth) / width;    float scaleHeight = ((float) newHeight) / height;    // 缩放图片动作    matrix.postScale(scaleWidth, scaleHeight);    Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,            (int) height, matrix, true);    return bitmap;}
//制作成一张384宽度的带有偏移量的图片

/** * 图片位图转换 * @param left 左偏移量 * @param bitmapOrg 图片位图 * @return 打印机图片位图 */public static Bitmap compressPicInOffset(Bitmap bitmapOrg,int left) {    int owidth = bitmapOrg.getWidth();    int oheight = bitmapOrg.getHeight();    LoggerUtils.d("ljy---owidth:" + owidth+",ljy---oheight:" + oheight+",left="+left);    if (owidth + left >= 384) {        int new_height = 48 * 8  * oheight / owidth;        // 定义预转换成的图片的宽度和高度        int newWidth = 48 * 8;        Bitmap targetBmp = Bitmap.createBitmap(newWidth, new_height,Bitmap.Config.RGB_565);        Canvas targetCanvas = new Canvas(targetBmp);        targetCanvas.drawColor(0xffffffff);        targetCanvas.drawBitmap(bitmapOrg, new Rect(0, 0, owidth, oheight),                new Rect(0, 0, newWidth, new_height), null);        return targetBmp;    }    Bitmap createBitmap = Bitmap.createBitmap(384, oheight, Bitmap.Config.RGB_565);    Canvas c = new Canvas(createBitmap);    c.drawColor(-1);    c.drawBitmap(bitmapOrg, left, 0, null);    return createBitmap;}



原创粉丝点击