|
| 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