Skip to content

Commit efc3bff

Browse files
committed
Update
1 parent fd7db2b commit efc3bff

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

439 KB
Loading

‎CustomView/Advance/[10]Matrix_Method.md‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,61 @@ boolean setPolyToPoly (
410410
int pointCount) // 要使用点的数量 取值范围是: 0到4
411411
```
412412
413-
Poly全称是Polygon,多边形的意思,了解了意思大致就能知道这个方法是做什么用的了,应该与PS中自由变换差不多。
413+
Poly全称是Polygon,多边形的意思,了解了意思大致就能知道这个方法是做什么用的了,应该与PS中自由变换中的扭曲有点类似。
414414
415415
![](http://ww1.sinaimg.cn/large/005Xtdi2jw1f71ppx7q0lg30go0b44ga.gif)
416416
417417
> 从参数我们可以了解到setPolyToPoly最多可以支持4个点,也就是图形的四个角,可以通过这四个角将视图从矩形变换成其他形状。
418418
419419
简单示例:
420420
421+
```java
422+
public class MstrixSetPolyToPolyTest extends View {
423+
424+
private Bitmap mBitmap; // 要绘制的图片
425+
private Matrix mPolyMatrix; // 测试setPolyToPoly用的Matrix
426+
427+
public MstrixSetPolyToPolyTest(Context context) {
428+
super(context);
429+
430+
initBitmapAndMatrix();
431+
}
432+
433+
private void initBitmapAndMatrix() {
434+
mBitmap = BitmapFactory.decodeResource(getResources(),
435+
R.drawable.poly_test);
436+
437+
mPolyMatrix = new Matrix();
438+
439+
440+
float[] src = {0, 0, // 左上
441+
mBitmap.getWidth(), 0, // 右上
442+
mBitmap.getWidth(), mBitmap.getHeight(), // 右下
443+
0, mBitmap.getHeight()}; // 左下
444+
445+
float[] dst = {0, 0, // 左上
446+
mBitmap.getWidth(), 400, // 右上
447+
mBitmap.getWidth(), mBitmap.getHeight() - 200, // 右下
448+
0, mBitmap.getHeight()}; // 左下
449+
450+
// 核心要点
451+
mPolyMatrix.setPolyToPoly(src, 0, dst, 0, src.length >> 1); // src.length >> 1 为位移运算 相当于处以2
452+
453+
// 此处为了更好的显示对图片进行了等比缩放和平移
454+
mPolyMatrix.postScale(0.26f, 0.26f);
455+
mPolyMatrix.postTranslate(0,200);
456+
}
457+
458+
@Override
459+
protected void onDraw(Canvas canvas) {
460+
super.onDraw(canvas);
461+
462+
// 根据Matrix绘制一个变换后的图片
463+
canvas.drawBitmap(mBitmap, mPolyMatrix, null);
464+
}
465+
}
466+
```
467+
421468
422469
423470
### 矩阵相关

0 commit comments

Comments
 (0)