Skip to content

Commit f5f8017

Browse files
committed
Add Test Code
1 parent 81f66e9 commit f5f8017

2 files changed

Lines changed: 342 additions & 0 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.gcssloop.canvas;
2+
3+
import android.content.Context;
4+
import android.graphics.Bitmap;
5+
import android.graphics.BitmapFactory;
6+
import android.graphics.Canvas;
7+
import android.graphics.Matrix;
8+
import android.graphics.Paint;
9+
import android.util.AttributeSet;
10+
import android.view.MotionEvent;
11+
import android.view.View;
12+
13+
import com.gcssloop.view.utils.CanvasAidUtils;
14+
15+
/**
16+
* Author: GcsSloop
17+
* <p>
18+
* Created Date: 16/8/26
19+
* <p>
20+
* Copyright (C) 2016 GcsSloop.
21+
* <p>
22+
* GitHub: https://github.com/GcsSloop
23+
*/
24+
public class SetPolyToPoly extends View{
25+
private static final String TAG = "SetPolyToPoly";
26+
27+
private int testPoint = 0;
28+
private int triggerRadius = 180; // 触发半径为180px
29+
30+
private Bitmap mBitmap; // 要绘制的图片
31+
private Matrix mPolyMatrix; // 测试setPolyToPoly用的Matrix
32+
33+
private float[] src = new float[8];
34+
private float[] dst = new float[8];
35+
36+
private Paint pointPaint;
37+
38+
public SetPolyToPoly(Context context) {
39+
this(context, null);
40+
}
41+
42+
public SetPolyToPoly(Context context, AttributeSet attrs) {
43+
this(context, attrs, 0);
44+
}
45+
46+
public SetPolyToPoly(Context context, AttributeSet attrs, int defStyleAttr) {
47+
super(context, attrs, defStyleAttr);
48+
initBitmapAndMatrix();
49+
}
50+
51+
private void initBitmapAndMatrix() {
52+
mBitmap = BitmapFactory.decodeResource(getResources(),
53+
R.drawable.poly_test2);
54+
55+
float[] temp = {0, 0, // 左上
56+
mBitmap.getWidth(), 0, // 右上
57+
mBitmap.getWidth(), mBitmap.getHeight(), // 右下
58+
0, mBitmap.getHeight()}; // 左下
59+
src = temp.clone();
60+
dst = temp.clone();
61+
62+
pointPaint = new Paint();
63+
pointPaint.setAntiAlias(true);
64+
pointPaint.setStrokeWidth(50);
65+
pointPaint.setColor(0xffd19165);
66+
pointPaint.setStrokeCap(Paint.Cap.ROUND);
67+
68+
mPolyMatrix = new Matrix();
69+
mPolyMatrix.setPolyToPoly(src, 0, src, 0, 4);
70+
}
71+
72+
@Override
73+
public boolean onTouchEvent(MotionEvent event) {
74+
75+
switch (event.getAction()){
76+
case MotionEvent.ACTION_MOVE:
77+
float tempX = event.getX();
78+
float tempY = event.getY();
79+
80+
// 根据触控位置改变dst
81+
for (int i=0; i<testPoint*2; i+=2 ) {
82+
if (Math.abs(tempX - dst[i]) <= triggerRadius && Math.abs(tempY - dst[i+1]) <= triggerRadius){
83+
dst[i] = tempX-100;
84+
dst[i+1] = tempY-100;
85+
break; // 防止两个点的位置重合
86+
}
87+
}
88+
89+
resetPolyMatrix(testPoint);
90+
invalidate();
91+
break;
92+
}
93+
94+
return true;
95+
}
96+
97+
public void resetPolyMatrix(int pointCount){
98+
mPolyMatrix.reset();
99+
// 核心要点
100+
mPolyMatrix.setPolyToPoly(src, 0, dst, 0, pointCount);
101+
}
102+
103+
@Override
104+
protected void onDraw(Canvas canvas) {
105+
canvas.translate(100,100);
106+
107+
// 绘制坐标系
108+
CanvasAidUtils.setCoordinateLen(900, 0, 1200, 0);
109+
CanvasAidUtils.drawCoordinateSpace(canvas);
110+
111+
// 根据Matrix绘制一个变换后的图片
112+
canvas.drawBitmap(mBitmap, mPolyMatrix, null);
113+
114+
float[] dst = new float[8];
115+
mPolyMatrix.mapPoints(dst,src);
116+
117+
// 绘制触控点
118+
for (int i=0; i<testPoint*2; i+=2 ) {
119+
canvas.drawPoint(dst[i], dst[i+1],pointPaint);
120+
}
121+
}
122+
123+
public void setTestPoint(int testPoint) {
124+
this.testPoint = testPoint > 4 || testPoint < 0 ? 4 : testPoint;
125+
dst = src.clone();
126+
resetPolyMatrix(this.testPoint);
127+
invalidate();
128+
}
129+
}
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
# Matrix setPolyTOPoly 测试代码
2+
3+
## SetPolyToPoly.java
4+
5+
```java
6+
public class SetPolyToPoly extends View{
7+
private static final String TAG = "SetPolyToPoly";
8+
9+
private int testPoint = 0;
10+
private int triggerRadius = 180; // 触发半径为180px
11+
12+
private Bitmap mBitmap; // 要绘制的图片
13+
private Matrix mPolyMatrix; // 测试setPolyToPoly用的Matrix
14+
15+
private float[] src = new float[8];
16+
private float[] dst = new float[8];
17+
18+
private Paint pointPaint;
19+
20+
public SetPolyToPoly(Context context) {
21+
this(context, null);
22+
}
23+
24+
public SetPolyToPoly(Context context, AttributeSet attrs) {
25+
this(context, attrs, 0);
26+
}
27+
28+
public SetPolyToPoly(Context context, AttributeSet attrs, int defStyleAttr) {
29+
super(context, attrs, defStyleAttr);
30+
initBitmapAndMatrix();
31+
}
32+
33+
private void initBitmapAndMatrix() {
34+
mBitmap = BitmapFactory.decodeResource(getResources(),
35+
R.drawable.poly_test2);
36+
37+
float[] temp = {0, 0, // 左上
38+
mBitmap.getWidth(), 0, // 右上
39+
mBitmap.getWidth(), mBitmap.getHeight(), // 右下
40+
0, mBitmap.getHeight()}; // 左下
41+
src = temp.clone();
42+
dst = temp.clone();
43+
44+
pointPaint = new Paint();
45+
pointPaint.setAntiAlias(true);
46+
pointPaint.setStrokeWidth(50);
47+
pointPaint.setColor(0xffd19165);
48+
pointPaint.setStrokeCap(Paint.Cap.ROUND);
49+
50+
mPolyMatrix = new Matrix();
51+
mPolyMatrix.setPolyToPoly(src, 0, src, 0, 4);
52+
}
53+
54+
@Override
55+
public boolean onTouchEvent(MotionEvent event) {
56+
57+
switch (event.getAction()){
58+
case MotionEvent.ACTION_MOVE:
59+
float tempX = event.getX();
60+
float tempY = event.getY();
61+
62+
// 根据触控位置改变dst
63+
for (int i=0; i<testPoint*2; i+=2 ) {
64+
if (Math.abs(tempX - dst[i]) <= triggerRadius && Math.abs(tempY - dst[i+1]) <= triggerRadius){
65+
dst[i] = tempX-100;
66+
dst[i+1] = tempY-100;
67+
break; // 防止两个点的位置重合
68+
}
69+
}
70+
71+
resetPolyMatrix(testPoint);
72+
invalidate();
73+
break;
74+
}
75+
76+
return true;
77+
}
78+
79+
public void resetPolyMatrix(int pointCount){
80+
mPolyMatrix.reset();
81+
// 核心要点
82+
mPolyMatrix.setPolyToPoly(src, 0, dst, 0, pointCount);
83+
}
84+
85+
@Override
86+
protected void onDraw(Canvas canvas) {
87+
canvas.translate(100,100);
88+
89+
// 绘制坐标系
90+
CanvasAidUtils.setCoordinateLen(900, 0, 1200, 0);
91+
CanvasAidUtils.drawCoordinateSpace(canvas);
92+
93+
// 根据Matrix绘制一个变换后的图片
94+
canvas.drawBitmap(mBitmap, mPolyMatrix, null);
95+
96+
float[] dst = new float[8];
97+
mPolyMatrix.mapPoints(dst,src);
98+
99+
// 绘制触控点
100+
for (int i=0; i<testPoint*2; i+=2 ) {
101+
canvas.drawPoint(dst[i], dst[i+1],pointPaint);
102+
}
103+
}
104+
105+
public void setTestPoint(int testPoint) {
106+
this.testPoint = testPoint > 4 || testPoint < 0 ? 4 : testPoint;
107+
dst = src.clone();
108+
resetPolyMatrix(this.testPoint);
109+
invalidate();
110+
}
111+
}
112+
```
113+
114+
*****
115+
116+
## 布局文件
117+
118+
``` xml
119+
<?xml version="1.0" encoding="utf-8"?>
120+
<RelativeLayout
121+
xmlns:android="http://schemas.android.com/apk/res/android"
122+
xmlns:tools="http://schemas.android.com/tools"
123+
android:layout_width="match_parent"
124+
android:layout_height="match_parent"
125+
android:gravity="center"
126+
tools:context="com.gcssloop.canvas.MainActivity">
127+
128+
<com.gcssloop.canvas.SetPolyToPoly
129+
android:id="@+id/poly"
130+
android:layout_width="match_parent"
131+
android:layout_height="match_parent"
132+
android:layout_marginBottom="60dp"/>
133+
134+
<LinearLayout
135+
android:orientation="horizontal"
136+
android:layout_alignParentBottom="true"
137+
android:layout_width="match_parent"
138+
android:padding="10dp"
139+
android:gravity="center"
140+
android:layout_height="60dp">
141+
142+
<RadioGroup
143+
android:id="@+id/group"
144+
android:orientation="horizontal"
145+
android:gravity="center"
146+
android:layout_width="wrap_content"
147+
android:layout_height="wrap_content">
148+
<RadioButton
149+
android:checked="true"
150+
android:id="@+id/point0"
151+
android:text="0"
152+
android:layout_width="wrap_content"
153+
android:layout_height="wrap_content"/>
154+
<RadioButton
155+
android:id="@+id/point1"
156+
android:text="1"
157+
android:layout_width="wrap_content"
158+
android:layout_height="wrap_content"/>
159+
<RadioButton
160+
android:id="@+id/point2"
161+
android:text="2"
162+
android:layout_width="wrap_content"
163+
android:layout_height="wrap_content"/>
164+
<RadioButton
165+
android:id="@+id/point3"
166+
android:text="3"
167+
android:layout_width="wrap_content"
168+
android:layout_height="wrap_content"/>
169+
<RadioButton
170+
android:id="@+id/point4"
171+
android:text="4"
172+
android:layout_width="wrap_content"
173+
android:layout_height="wrap_content"/>
174+
</RadioGroup>
175+
</LinearLayout>
176+
177+
</RelativeLayout>
178+
```
179+
180+
*****
181+
182+
## MainActivity
183+
184+
``` java
185+
setContentView(R.layout.activity_main);
186+
187+
final SetPolyToPoly poly = (SetPolyToPoly) findViewById(R.id.poly);
188+
189+
RadioGroup group = (RadioGroup) findViewById(R.id.group);
190+
assert group != null;
191+
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
192+
@Override
193+
public void onCheckedChanged(RadioGroup group, int checkedId) {
194+
switch (group.getCheckedRadioButtonId()){
195+
case R.id.point0: poly.setTestPoint(0); break;
196+
case R.id.point1: poly.setTestPoint(1); break;
197+
case R.id.point2: poly.setTestPoint(2); break;
198+
case R.id.point3: poly.setTestPoint(3); break;
199+
case R.id.point4: poly.setTestPoint(4); break;
200+
}
201+
}
202+
});
203+
```
204+
205+
*****
206+
207+
## 依赖的库
208+
209+
绘制坐标系部分依赖了一个开源库。
210+
211+
* [ViewSupport](https://github.com/GcsSloop/ViewSupport )
212+
213+

0 commit comments

Comments
 (0)