質問編集履歴

1

コードの追加

2016/12/23 09:24

投稿

pomupomu
pomupomu

スコア15

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- androidアプリ開発にてエラーが良く分かりせん
1
+ androidアプリ開発にてdrawBitmapMeshで画像の変形をしようと思ったのですがエラーがした。
2
2
 
3
3
  どこを直せばよいのでしょうか?
4
4
 
@@ -57,3 +57,159 @@
57
57
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
58
58
 
59
59
  ```
60
+
61
+
62
+
63
+
64
+
65
+
66
+
67
+
68
+
69
+ コードになります。
70
+
71
+ ```java
72
+
73
+ import android.content.Context;
74
+
75
+ import android.graphics.Bitmap;
76
+
77
+ import android.graphics.BitmapFactory;
78
+
79
+ import android.graphics.Canvas;
80
+
81
+ import android.graphics.Color;
82
+
83
+ import android.graphics.Paint;
84
+
85
+ import android.util.AttributeSet;
86
+
87
+ import android.view.MotionEvent;
88
+
89
+ import android.view.View;
90
+
91
+
92
+
93
+
94
+
95
+ public class MainActivity extends View implements View.OnTouchListener{
96
+
97
+
98
+
99
+ private final int MESH_WIDTH = 20;
100
+
101
+ private final int MESH_HEIGHT = 20;
102
+
103
+ private final int MESH_SIZE = (MESH_WIDTH+1)*(MESH_HEIGHT+1)*2;
104
+
105
+
106
+
107
+ private Bitmap mBmp;
108
+
109
+ private float[] mVents;
110
+
111
+ private Paint mPaint;
112
+
113
+
114
+
115
+
116
+
117
+ public MainActivity(Context context, AttributeSet attrs) {
118
+
119
+ super(context, attrs);
120
+
121
+ mBmp = BitmapFactory.decodeResource(getResources(), R.drawable.red);
122
+
123
+ setInitialMesh();
124
+
125
+ setOnTouchListener(this);
126
+
127
+ mPaint = new Paint();
128
+
129
+ mPaint.setStrokeWidth(2);
130
+
131
+ mPaint.setColor(Color.WHITE);
132
+
133
+ }
134
+
135
+
136
+
137
+
138
+
139
+ public void setInitialMesh() {
140
+
141
+ mVents = new float[MESH_SIZE];
142
+
143
+
144
+
145
+ for (int i=0;i<=MESH_HEIGHT;i++) {
146
+
147
+ for (int j=0;j<=MESH_WIDTH;j++) {
148
+
149
+ int pos = i * (MESH_WIDTH + 1) + j;
150
+
151
+ mVents[2 * pos] = j * mBmp.getWidth()/MESH_WIDTH;
152
+
153
+ mVents[2 * pos + 1] = i * mBmp.getHeight()/MESH_HEIGHT;
154
+
155
+ }
156
+
157
+ }
158
+
159
+ }
160
+
161
+
162
+
163
+ @Override
164
+
165
+ public boolean onTouch(View v, MotionEvent event) {
166
+
167
+ if (event.getAction()==MotionEvent.ACTION_MOVE) {
168
+
169
+ stepNearerTouchPoint(event.getX(), event.getY());
170
+
171
+ invalidate();
172
+
173
+ }
174
+
175
+ return true;
176
+
177
+ }
178
+
179
+
180
+
181
+ private void stepNearerTouchPoint(float x, float y) {
182
+
183
+ //メッシュの外周は固定したままにする
184
+
185
+ for (int i=1;i<MESH_HEIGHT;i++) {
186
+
187
+ for (int j=1;j<MESH_WIDTH;j++) {
188
+
189
+ int pos = i * (MESH_WIDTH + 1) + j;
190
+
191
+ float dist = (float)Math.sqrt((mVents[2 * pos] - x) * (mVents[2 * pos] - x) + (mVents[2 * pos + 1] - y) * (mVents[2 * pos + 1] - y));
192
+
193
+ mVents[2 * pos] += (x - mVents[2 * pos]) / dist;
194
+
195
+ mVents[2 * pos + 1] += (y - mVents[2 * pos + 1]) / dist;
196
+
197
+ }
198
+
199
+ }
200
+
201
+ }
202
+
203
+
204
+
205
+ @Override
206
+
207
+ protected void onDraw(Canvas canvas) {
208
+
209
+ canvas.drawBitmapMesh(mBmp, MESH_WIDTH, MESH_HEIGHT-1, mVents, (MESH_WIDTH+1)*2, null, 0, null);
210
+
211
+ }
212
+
213
+ }
214
+
215
+ ```