一. 将图片资源文件的位图画到新的位图上

1
2
3
4
5
6
7
8
9
10
11
//根据图片生成原位图,不能直接对原位图进行操作
Bitmap orgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.head);

//创建了一个和原位图一样大小,基本设置一样的空白位图
Bitmap copBitmap = Bitmap.createBitmap(orgBitmap.getWidth(), orgBitmap.getHeight(), orgBitmap.getConfig());

//根据matrix将原位图画到空白位图里
Canvas canvas = new Canvas(copBitmap);
Matrix matrix = new Matrix();
Paint paint = new Paint();
canvas.drawBitmap(orgBitmap,matrix,paint);

1.获得一个图片资源的源位图

  • BitmapFactory: 管理位图

  • decodeResource: 利用工程资源路径下的资源文件生成一个位图

  • getResources(): 工程的资源路径

  • R.drawable.name: 资源路径下drawable文件里面的一个文件的资源id,name是资源文件的名称

1
Bitmap orgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.head);

2.创建位图

  • Bitmap createBitmap (Bitmap src) — 从原位图src复制出一个新的位图,和原始位图相同
  • Bitmap createBitmap (int width, int height, Bitmap.Config config) — 根据参数创建新位图 (和原位图同样的宽高和密度)
1
Bitmap copBitmap = Bitmap.createBitmap(orgBitmap.getWidth(), orgBitmap.getHeight(), orgBitmap.getConfig());

3.以位图为背景,创建画布

  • Canvas 本意是画布的意思,但是在Android中,它就只是一个媒介,可以通过Canvas来调用drawRect,drawCircle等等,但是实际上画的这些东西最终展现的时候都是像素,但是只有Bitmap才能保存像素,而Canvas并不行,所以在创建Canvas的时候,就必须传递一个Bitmap,用来承载画的内容。
  • drawBitmap,依据matrix将原位图利用画板介质画在创建的新的位图上。
1
2
3
4
Canvas canvas = new Canvas(copBitmap);//画板
Paint paint = new Paint();//画笔
Matrix matrix = new Matrix();//矩阵
canvas.drawBitmap(orgBitmap,matrix,paint);

二. 使用 Matrix类对图片实现形变后, 再进行显示

1.平移

  • setTranslate(float dx, float dy),Set the matrix to translate by (dx, dy)。
1
2
3
4
5
6
7
8
9
10
11
//根据matrix将原位图画到空白位图里
Canvas canvas = new Canvas(copBitmap);
Matrix matrix = new Matrix();
//平移 dx 是x方向平移的大小,dy 是y方向平移的大小
matrix.setTranslate(500,500);
Paint paint = new Paint();
canvas.drawBitmap(orgBitmap,matrix,paint);

//设置图片
ImageView imgView = (ImageView) findViewById(R.id.img);
imgView.setImageBitmap(copBitmap);

2.伸缩

  • setScale(float sx, float sy, float px, float py),Set the matrix to scale by sx and sy, with a pivot point at (px, py). The pivot point is the coordinate that should remain unchanged by the specified transformation.
  • setScale(float sx, float sy),Set the matrix to scale by sx and sy.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    //根据matrix将原位图画到空白位图里
    Canvas canvas = new Canvas(copBitmap);
    Matrix matrix = new Matrix();
    //伸缩 sx 是x方向伸缩的大小, sy是y方向伸缩的大小
    matrix.setScale(0.5f,0.5f);
    Paint paint = new Paint();
    canvas.drawBitmap(orgBitmap,matrix,paint);

    //设置图片
    ImageView imgView = (ImageView) findViewById(R.id.img);
    imgView.setImageBitmap(copBitmap);

3.旋转

  • setRotate(float degrees, float px, float py),et the matrix to rotate by the specified number of degrees, with a pivot point at (px, py).The pivot point is the coordinate that should remain unchanged by the specified
  • setRotate(float degrees),Set the matrix to rotate about (0,0) by the specified number of degrees.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    //根据matrix将原位图画到空白位图里
    Canvas canvas = new Canvas(copBitmap);
    Matrix matrix = new Matrix();
    //旋转 degrees 是旋转的度数,px、py 是旋转的坐标点
    matrix.setRotate(45,0,0);
    Paint paint = new Paint();
    canvas.drawBitmap(orgBitmap,matrix,paint);

    //设置图片
    ImageView imgView = (ImageView) findViewById(R.id.img);
    imgView.setImageBitmap(copBitmap);

4.重置

1
2
3
4
/** Set the matrix to identity */
public void reset() {
nReset(native_instance);
}

注意:每一个方法例如,setTranslate,必定还有对应的 postTranslate。

  • set 相当于对matrix对象先重置reset(), 再施加形变操作。
  • post 相当于在已有的matrix对象的基础上, 再添加一个新的形变操作。
  • 还有一类是 pre 方法, 相当于在已有的matrix对象的基础上, 在之前添加一个新的形变操作, 在实际开发中, 用 pre 这类方法的地方并不多。

源码

https://github.com/xiaoshitounen/Bitmap

参考文章

Bitmap createBitmap 相关参数

自定义view之Canvas构造 new Canvas(Bitmap)中对canvas和bitmap的关系

Bitmap Drawble Canvas

canvas.drawBitmap()的三个最常用的调用方法, 以及Matrix类的使用