質問編集履歴

3

表示間違い

2017/10/30 12:24

投稿

S.I
S.I

スコア48

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,6 @@
1
- 4つのボタン(うえ、した、ひだり、みぎ)を押したら猫の画像が一定の距離を移動するアニメーションをつけました。開始の位置が常に一定のため、最初の位置からの移動しかできません。移動先の場所からさらに移動させるプログラムをつけたいです。…!![イメージ説明](e270e44554ccf01e07707a002e1d9da6.jpeg)
1
+ 4つのボタン(うえ、した、ひだり、みぎ)を押したら猫の画像が一定の距離を移動するアニメーションをつけました。開始の位置が常に一定のため、最初の位置からの移動しかできません。移動先の場所からさらに移動させるプログラムをつけたいです。
2
+
3
+
2
4
 
3
5
  MainActivity.java
4
6
 

2

文字の変更

2017/10/30 12:24

投稿

S.I
S.I

スコア48

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- 4つのボタン(うえ、した、ひだり、みぎ)を押したら猫の画像が一定の距離を移動するアニメーションをつけました。開始の位置が常に一定のため、最初の位置からの移動しかできません。移動先の場所からさらに移動させるプログラムをつけたいです。急ぎのためこちらで質問いたしました。ご存知の方がいらっしゃいましたら回答お願いします…!![イメージ説明](e270e44554ccf01e07707a002e1d9da6.jpeg)
1
+ 4つのボタン(うえ、した、ひだり、みぎ)を押したら猫の画像が一定の距離を移動するアニメーションをつけました。開始の位置が常に一定のため、最初の位置からの移動しかできません。移動先の場所からさらに移動させるプログラムをつけたいです。…!![イメージ説明](e270e44554ccf01e07707a002e1d9da6.jpeg)
2
2
 
3
3
  MainActivity.java
4
4
 

1

変更

2017/10/21 13:15

投稿

S.I
S.I

スコア48

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  MainActivity.java
4
4
 
5
- ```JAVA
5
+ ```java
6
6
 
7
7
  public class MainActivity extends AppCompatActivity {
8
8
 
@@ -10,13 +10,17 @@
10
10
 
11
11
  private ImageView imageView;
12
12
 
13
- private Animation animation;
13
+ private TranslateAnimation translateAnimation;
14
-
14
+
15
- private Animation animation2;
15
+ private TranslateAnimation translateAnimation2;
16
-
16
+
17
- private Animation animation3;
17
+ private TranslateAnimation translateAnimation3;
18
-
18
+
19
- private Animation animation4;
19
+ private TranslateAnimation translateAnimation4;
20
+
21
+
22
+
23
+
20
24
 
21
25
 
22
26
 
@@ -32,102 +36,344 @@
32
36
 
33
37
  imageView = (ImageView)findViewById(R.id.imageView);
34
38
 
35
- animation = AnimationUtils.loadAnimation(this, R.anim.translate);
36
-
37
- animation2 = AnimationUtils.loadAnimation(this, R.anim.translate2);
38
-
39
- animation3 = AnimationUtils.loadAnimation(this, R.anim.translate3);
40
-
41
- animation4 = AnimationUtils.loadAnimation(this, R.anim.translate4);
42
-
43
-
44
-
45
-
46
-
47
39
 
48
40
 
49
41
  Button btn1 = (Button)findViewById(R.id.button1);
50
42
 
43
+ btn1.setOnClickListener(new View.OnClickListener() {
44
+
45
+ public void onClick(View view) {
46
+
47
+ startTranslateUp();
48
+
49
+ }
50
+
51
+ });
52
+
51
53
  Button btn2 = (Button)findViewById(R.id.button2);
52
54
 
55
+ btn2.setOnClickListener(new View.OnClickListener() {
56
+
57
+ public void onClick(View view) {
58
+
59
+ startTranslateDown();
60
+
61
+ }
62
+
63
+ });
64
+
53
65
  Button btn3 = (Button)findViewById(R.id.button3);
54
66
 
67
+ btn3.setOnClickListener(new View.OnClickListener() {
68
+
69
+ public void onClick(View view) {
70
+
71
+ startTranslateLeft();
72
+
73
+ }
74
+
75
+ });
76
+
55
77
  Button btn4 = (Button)findViewById(R.id.button4);
56
78
 
57
-
58
-
59
- btn1.setOnClickListener(new View.OnClickListener() {
79
+ btn4.setOnClickListener(new View.OnClickListener() {
60
80
 
61
81
  public void onClick(View view) {
62
82
 
63
- imageView.startAnimation(animation);
83
+ startTranslateRight();
64
84
 
65
85
  }
66
86
 
67
87
  });
68
88
 
69
- btn2.setOnClickListener(new View.OnClickListener() {
70
-
71
- public void onClick(View view) {
72
-
73
- imageView.startAnimation(animation2);
74
-
75
- }
76
-
77
- });
78
-
79
- btn3.setOnClickListener(new View.OnClickListener() {
80
-
81
- public void onClick(View view) {
82
-
83
- imageView.startAnimation(animation3);
84
-
85
- }
86
-
87
- });
88
-
89
- btn4.setOnClickListener(new View.OnClickListener() {
90
-
91
- public void onClick(View view) {
92
-
93
- imageView.startAnimation(animation4);
94
-
95
- }
96
-
97
- });
98
-
99
-
100
-
101
-
89
+
90
+
91
+ }
92
+
93
+
94
+
95
+ private void startTranslateUp(){
96
+
97
+ // 設定を切り替え可能
98
+
99
+ int type = 0;
100
+
101
+ if(type == 0){
102
+
103
+ // TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
104
+
105
+ translateAnimation = new TranslateAnimation(
106
+
107
+ Animation.ABSOLUTE, 0.0f,
108
+
109
+ Animation.ABSOLUTE, 0.0f,
110
+
111
+ Animation.ABSOLUTE, 0.0f,
112
+
113
+ Animation.ABSOLUTE, -300.0f);
114
+
115
+ }
116
+
117
+ else if(type == 1){
118
+
119
+ translateAnimation = new TranslateAnimation(
120
+
121
+ Animation.RELATIVE_TO_SELF, 0.0f,
122
+
123
+ Animation.RELATIVE_TO_SELF, 0.0f,
124
+
125
+ Animation.RELATIVE_TO_SELF, 0.0f,
126
+
127
+ Animation.RELATIVE_TO_SELF, 1.8f);
128
+
129
+ }
130
+
131
+ else if(type ==2){
132
+
133
+ translateAnimation = new TranslateAnimation(
134
+
135
+ Animation.RELATIVE_TO_PARENT, 0.0f,
136
+
137
+ Animation.RELATIVE_TO_PARENT, 0.0f,
138
+
139
+ Animation.RELATIVE_TO_PARENT, 0.0f,
140
+
141
+ Animation.RELATIVE_TO_PARENT, 0.6f);
142
+
143
+ }
144
+
145
+
146
+
147
+ // animation時間 msec
148
+
149
+ translateAnimation.setDuration(1000);
150
+
151
+ // 繰り返し回数
152
+
153
+ translateAnimation.setRepeatCount(0);
154
+
155
+ // animationが終わったそのまま表示にする
156
+
157
+ translateAnimation.setFillAfter(true);
158
+
159
+ //アニメーションの開始
160
+
161
+ imageView.startAnimation(translateAnimation);
162
+
163
+ }
164
+
165
+
166
+
167
+ private void startTranslateDown(){
168
+
169
+ // 設定を切り替え可能
170
+
171
+ int type = 0;
172
+
173
+ if(type == 0){
174
+
175
+ // TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
176
+
177
+ translateAnimation2 = new TranslateAnimation(
178
+
179
+ Animation.ABSOLUTE, 0.0f,
180
+
181
+ Animation.ABSOLUTE, 0.0f,
182
+
183
+ Animation.ABSOLUTE, 0.0f,
184
+
185
+ Animation.ABSOLUTE, 300.0f);
186
+
187
+ }
188
+
189
+ else if(type == 1){
190
+
191
+ translateAnimation2 = new TranslateAnimation(
192
+
193
+ Animation.RELATIVE_TO_SELF, 0.0f,
194
+
195
+ Animation.RELATIVE_TO_SELF, 0.0f,
196
+
197
+ Animation.RELATIVE_TO_SELF, 0.0f,
198
+
199
+ Animation.RELATIVE_TO_SELF, 1.8f);
200
+
201
+ }
202
+
203
+ else if(type ==2){
204
+
205
+ translateAnimation2 = new TranslateAnimation(
206
+
207
+ Animation.RELATIVE_TO_PARENT, 0.0f,
208
+
209
+ Animation.RELATIVE_TO_PARENT, 0.0f,
210
+
211
+ Animation.RELATIVE_TO_PARENT, 0.0f,
212
+
213
+ Animation.RELATIVE_TO_PARENT, 0.6f);
214
+
215
+ }
216
+
217
+
218
+
219
+ // animation時間 msec
220
+
221
+ translateAnimation2.setDuration(1000);
222
+
223
+ // 繰り返し回数
224
+
225
+ translateAnimation2.setRepeatCount(0);
226
+
227
+ // animationが終わったそのまま表示にする
228
+
229
+ translateAnimation2.setFillAfter(true);
230
+
231
+ //アニメーションの開始
232
+
233
+ imageView.startAnimation(translateAnimation2);
234
+
235
+ }
236
+
237
+ private void startTranslateLeft(){
238
+
239
+ // 設定を切り替え可能
240
+
241
+ int type = 0;
242
+
243
+ if(type == 0){
244
+
245
+ // TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
246
+
247
+ translateAnimation3 = new TranslateAnimation(
248
+
249
+ Animation.ABSOLUTE, 0.0f,
250
+
251
+ Animation.ABSOLUTE, -300.0f,
252
+
253
+ Animation.ABSOLUTE, 0.0f,
254
+
255
+ Animation.ABSOLUTE, 0.0f);
256
+
257
+ }
258
+
259
+ else if(type == 1){
260
+
261
+ translateAnimation3 = new TranslateAnimation(
262
+
263
+ Animation.RELATIVE_TO_SELF, 0.0f,
264
+
265
+ Animation.RELATIVE_TO_SELF, 0.0f,
266
+
267
+ Animation.RELATIVE_TO_SELF, 0.0f,
268
+
269
+ Animation.RELATIVE_TO_SELF, 1.8f);
270
+
271
+ }
272
+
273
+ else if(type ==2){
274
+
275
+ translateAnimation3 = new TranslateAnimation(
276
+
277
+ Animation.RELATIVE_TO_PARENT, 0.0f,
278
+
279
+ Animation.RELATIVE_TO_PARENT, 0.0f,
280
+
281
+ Animation.RELATIVE_TO_PARENT, 0.0f,
282
+
283
+ Animation.RELATIVE_TO_PARENT, 0.6f);
284
+
285
+ }
286
+
287
+
288
+
289
+ // animation時間 msec
290
+
291
+ translateAnimation3.setDuration(1000);
292
+
293
+ // 繰り返し回数
294
+
295
+ translateAnimation3.setRepeatCount(0);
296
+
297
+ // animationが終わったそのまま表示にする
298
+
299
+ translateAnimation3.setFillAfter(true);
300
+
301
+ //アニメーションの開始
302
+
303
+ imageView.startAnimation(translateAnimation3);
304
+
305
+ }
306
+
307
+ private void startTranslateRight(){
308
+
309
+ // 設定を切り替え可能
310
+
311
+ int type = 0;
312
+
313
+ if(type == 0){
314
+
315
+ // TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
316
+
317
+ translateAnimation4 = new TranslateAnimation(
318
+
319
+ Animation.ABSOLUTE, 0.0f,
320
+
321
+ Animation.ABSOLUTE, 300.0f,
322
+
323
+ Animation.ABSOLUTE, 0.0f,
324
+
325
+ Animation.ABSOLUTE, 0.0f);
326
+
327
+ }
328
+
329
+ else if(type == 1){
330
+
331
+ translateAnimation4 = new TranslateAnimation(
332
+
333
+ Animation.RELATIVE_TO_SELF, 0.0f,
334
+
335
+ Animation.RELATIVE_TO_SELF, 0.0f,
336
+
337
+ Animation.RELATIVE_TO_SELF, 0.0f,
338
+
339
+ Animation.RELATIVE_TO_SELF, 1.8f);
340
+
341
+ }
342
+
343
+ else if(type ==2){
344
+
345
+ translateAnimation4 = new TranslateAnimation(
346
+
347
+ Animation.RELATIVE_TO_PARENT, 0.0f,
348
+
349
+ Animation.RELATIVE_TO_PARENT, 0.0f,
350
+
351
+ Animation.RELATIVE_TO_PARENT, 0.0f,
352
+
353
+ Animation.RELATIVE_TO_PARENT, 0.6f);
354
+
355
+ }
356
+
357
+
358
+
359
+ // animation時間 msec
360
+
361
+ translateAnimation4.setDuration(1000);
362
+
363
+ // 繰り返し回数
364
+
365
+ translateAnimation4.setRepeatCount(0);
366
+
367
+ // animationが終わったそのまま表示にする
368
+
369
+ translateAnimation4.setFillAfter(true);
370
+
371
+ //アニメーションの開始
372
+
373
+ imageView.startAnimation(translateAnimation4);
102
374
 
103
375
  }
104
376
 
105
377
  }
106
378
 
107
379
  ```
108
-
109
-
110
-
111
- translate.xml
112
-
113
- ```java
114
-
115
- <?xml version="1.0" encoding="utf-8"?>
116
-
117
- <translate xmlns:android="http://schemas.android.com/apk/res/android"
118
-
119
- android:interpolator="@android:anim/decelerate_interpolator"
120
-
121
- android:fromXDelta="0%p"
122
-
123
- android:toXDelta="0%p"
124
-
125
- android:fromYDelta="0%p"
126
-
127
- android:toYDelta="-23%p"
128
-
129
- android:fillAfter="true"
130
-
131
- android:duration="1000"/>
132
-
133
- ```