質問編集履歴
3
表示間違い
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
4つのボタン(うえ、した、ひだり、みぎ)を押したら猫の画像が一定の距離を移動するアニメーションをつけました。開始の位置が常に一定のため、最初の位置からの移動しかできません。移動先の場所からさらに移動させるプログラムをつけたいです。
|
1
|
+
4つのボタン(うえ、した、ひだり、みぎ)を押したら猫の画像が一定の距離を移動するアニメーションをつけました。開始の位置が常に一定のため、最初の位置からの移動しかできません。移動先の場所からさらに移動させるプログラムをつけたいです。
|
2
|
+
|
2
3
|
MainActivity.java
|
3
4
|
```java
|
4
5
|
public class MainActivity extends AppCompatActivity {
|
2
文字の変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
4つのボタン(うえ、した、ひだり、みぎ)を押したら猫の画像が一定の距離を移動するアニメーションをつけました。開始の位置が常に一定のため、最初の位置からの移動しかできません。移動先の場所からさらに移動させるプログラムをつけたいです。
|
1
|
+
4つのボタン(うえ、した、ひだり、みぎ)を押したら猫の画像が一定の距離を移動するアニメーションをつけました。開始の位置が常に一定のため、最初の位置からの移動しかできません。移動先の場所からさらに移動させるプログラムをつけたいです。…!
|
2
2
|
MainActivity.java
|
3
3
|
```java
|
4
4
|
public class MainActivity extends AppCompatActivity {
|
1
変更
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,67 +1,190 @@
|
|
1
1
|
4つのボタン(うえ、した、ひだり、みぎ)を押したら猫の画像が一定の距離を移動するアニメーションをつけました。開始の位置が常に一定のため、最初の位置からの移動しかできません。移動先の場所からさらに移動させるプログラムをつけたいです。急ぎのためこちらで質問いたしました。ご存知の方がいらっしゃいましたら回答お願いします…!
|
2
2
|
MainActivity.java
|
3
|
-
```
|
3
|
+
```java
|
4
4
|
public class MainActivity extends AppCompatActivity {
|
5
5
|
|
6
6
|
private ImageView imageView;
|
7
|
-
private
|
7
|
+
private TranslateAnimation translateAnimation;
|
8
|
-
private
|
8
|
+
private TranslateAnimation translateAnimation2;
|
9
|
-
private
|
9
|
+
private TranslateAnimation translateAnimation3;
|
10
|
-
private
|
10
|
+
private TranslateAnimation translateAnimation4;
|
11
11
|
|
12
|
+
|
13
|
+
|
12
14
|
@Override
|
13
15
|
protected void onCreate(Bundle savedInstanceState) {
|
14
16
|
super.onCreate(savedInstanceState);
|
15
17
|
setContentView(R.layout.activity_main);
|
16
18
|
|
17
19
|
imageView = (ImageView)findViewById(R.id.imageView);
|
18
|
-
animation = AnimationUtils.loadAnimation(this, R.anim.translate);
|
19
|
-
animation2 = AnimationUtils.loadAnimation(this, R.anim.translate2);
|
20
|
-
animation3 = AnimationUtils.loadAnimation(this, R.anim.translate3);
|
21
|
-
animation4 = AnimationUtils.loadAnimation(this, R.anim.translate4);
|
22
20
|
|
23
|
-
|
24
|
-
|
25
21
|
Button btn1 = (Button)findViewById(R.id.button1);
|
26
|
-
Button btn2 = (Button)findViewById(R.id.button2);
|
27
|
-
Button btn3 = (Button)findViewById(R.id.button3);
|
28
|
-
Button btn4 = (Button)findViewById(R.id.button4);
|
29
|
-
|
30
22
|
btn1.setOnClickListener(new View.OnClickListener() {
|
31
23
|
public void onClick(View view) {
|
32
|
-
|
24
|
+
startTranslateUp();
|
33
25
|
}
|
34
26
|
});
|
27
|
+
Button btn2 = (Button)findViewById(R.id.button2);
|
35
28
|
btn2.setOnClickListener(new View.OnClickListener() {
|
36
29
|
public void onClick(View view) {
|
37
|
-
|
30
|
+
startTranslateDown();
|
38
31
|
}
|
39
32
|
});
|
33
|
+
Button btn3 = (Button)findViewById(R.id.button3);
|
40
34
|
btn3.setOnClickListener(new View.OnClickListener() {
|
41
35
|
public void onClick(View view) {
|
42
|
-
|
36
|
+
startTranslateLeft();
|
43
37
|
}
|
44
38
|
});
|
39
|
+
Button btn4 = (Button)findViewById(R.id.button4);
|
45
40
|
btn4.setOnClickListener(new View.OnClickListener() {
|
46
41
|
public void onClick(View view) {
|
47
|
-
|
42
|
+
startTranslateRight();
|
48
43
|
}
|
49
44
|
});
|
50
45
|
|
46
|
+
}
|
51
47
|
|
48
|
+
private void startTranslateUp(){
|
49
|
+
// 設定を切り替え可能
|
50
|
+
int type = 0;
|
51
|
+
if(type == 0){
|
52
|
+
// TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
|
53
|
+
translateAnimation = new TranslateAnimation(
|
54
|
+
Animation.ABSOLUTE, 0.0f,
|
55
|
+
Animation.ABSOLUTE, 0.0f,
|
56
|
+
Animation.ABSOLUTE, 0.0f,
|
57
|
+
Animation.ABSOLUTE, -300.0f);
|
58
|
+
}
|
59
|
+
else if(type == 1){
|
60
|
+
translateAnimation = new TranslateAnimation(
|
61
|
+
Animation.RELATIVE_TO_SELF, 0.0f,
|
62
|
+
Animation.RELATIVE_TO_SELF, 0.0f,
|
63
|
+
Animation.RELATIVE_TO_SELF, 0.0f,
|
64
|
+
Animation.RELATIVE_TO_SELF, 1.8f);
|
65
|
+
}
|
66
|
+
else if(type ==2){
|
67
|
+
translateAnimation = new TranslateAnimation(
|
68
|
+
Animation.RELATIVE_TO_PARENT, 0.0f,
|
69
|
+
Animation.RELATIVE_TO_PARENT, 0.0f,
|
70
|
+
Animation.RELATIVE_TO_PARENT, 0.0f,
|
71
|
+
Animation.RELATIVE_TO_PARENT, 0.6f);
|
72
|
+
}
|
73
|
+
|
74
|
+
// animation時間 msec
|
75
|
+
translateAnimation.setDuration(1000);
|
76
|
+
// 繰り返し回数
|
77
|
+
translateAnimation.setRepeatCount(0);
|
78
|
+
// animationが終わったそのまま表示にする
|
79
|
+
translateAnimation.setFillAfter(true);
|
80
|
+
//アニメーションの開始
|
81
|
+
imageView.startAnimation(translateAnimation);
|
52
82
|
}
|
83
|
+
|
84
|
+
private void startTranslateDown(){
|
85
|
+
// 設定を切り替え可能
|
86
|
+
int type = 0;
|
87
|
+
if(type == 0){
|
88
|
+
// TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
|
89
|
+
translateAnimation2 = new TranslateAnimation(
|
90
|
+
Animation.ABSOLUTE, 0.0f,
|
91
|
+
Animation.ABSOLUTE, 0.0f,
|
92
|
+
Animation.ABSOLUTE, 0.0f,
|
93
|
+
Animation.ABSOLUTE, 300.0f);
|
94
|
+
}
|
95
|
+
else if(type == 1){
|
96
|
+
translateAnimation2 = new TranslateAnimation(
|
97
|
+
Animation.RELATIVE_TO_SELF, 0.0f,
|
98
|
+
Animation.RELATIVE_TO_SELF, 0.0f,
|
99
|
+
Animation.RELATIVE_TO_SELF, 0.0f,
|
100
|
+
Animation.RELATIVE_TO_SELF, 1.8f);
|
101
|
+
}
|
102
|
+
else if(type ==2){
|
103
|
+
translateAnimation2 = new TranslateAnimation(
|
104
|
+
Animation.RELATIVE_TO_PARENT, 0.0f,
|
105
|
+
Animation.RELATIVE_TO_PARENT, 0.0f,
|
106
|
+
Animation.RELATIVE_TO_PARENT, 0.0f,
|
107
|
+
Animation.RELATIVE_TO_PARENT, 0.6f);
|
108
|
+
}
|
109
|
+
|
110
|
+
// animation時間 msec
|
111
|
+
translateAnimation2.setDuration(1000);
|
112
|
+
// 繰り返し回数
|
113
|
+
translateAnimation2.setRepeatCount(0);
|
114
|
+
// animationが終わったそのまま表示にする
|
115
|
+
translateAnimation2.setFillAfter(true);
|
116
|
+
//アニメーションの開始
|
117
|
+
imageView.startAnimation(translateAnimation2);
|
53
118
|
}
|
119
|
+
private void startTranslateLeft(){
|
120
|
+
// 設定を切り替え可能
|
121
|
+
int type = 0;
|
122
|
+
if(type == 0){
|
123
|
+
// TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
|
124
|
+
translateAnimation3 = new TranslateAnimation(
|
125
|
+
Animation.ABSOLUTE, 0.0f,
|
126
|
+
Animation.ABSOLUTE, -300.0f,
|
127
|
+
Animation.ABSOLUTE, 0.0f,
|
128
|
+
Animation.ABSOLUTE, 0.0f);
|
54
|
-
|
129
|
+
}
|
130
|
+
else if(type == 1){
|
131
|
+
translateAnimation3 = new TranslateAnimation(
|
132
|
+
Animation.RELATIVE_TO_SELF, 0.0f,
|
133
|
+
Animation.RELATIVE_TO_SELF, 0.0f,
|
134
|
+
Animation.RELATIVE_TO_SELF, 0.0f,
|
135
|
+
Animation.RELATIVE_TO_SELF, 1.8f);
|
136
|
+
}
|
137
|
+
else if(type ==2){
|
138
|
+
translateAnimation3 = new TranslateAnimation(
|
139
|
+
Animation.RELATIVE_TO_PARENT, 0.0f,
|
140
|
+
Animation.RELATIVE_TO_PARENT, 0.0f,
|
141
|
+
Animation.RELATIVE_TO_PARENT, 0.0f,
|
142
|
+
Animation.RELATIVE_TO_PARENT, 0.6f);
|
143
|
+
}
|
55
144
|
|
56
|
-
|
145
|
+
// animation時間 msec
|
57
|
-
```java
|
58
|
-
<?xml version="1.0" encoding="utf-8"?>
|
59
|
-
<translate xmlns:android="http://schemas.android.com/apk/res/android"
|
60
|
-
android:interpolator="@android:anim/decelerate_interpolator"
|
61
|
-
|
146
|
+
translateAnimation3.setDuration(1000);
|
147
|
+
// 繰り返し回数
|
148
|
+
translateAnimation3.setRepeatCount(0);
|
149
|
+
// animationが終わったそのまま表示にする
|
62
|
-
|
150
|
+
translateAnimation3.setFillAfter(true);
|
151
|
+
//アニメーションの開始
|
63
|
-
|
152
|
+
imageView.startAnimation(translateAnimation3);
|
153
|
+
}
|
64
|
-
|
154
|
+
private void startTranslateRight(){
|
65
|
-
|
155
|
+
// 設定を切り替え可能
|
156
|
+
int type = 0;
|
157
|
+
if(type == 0){
|
158
|
+
// TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
|
66
|
-
|
159
|
+
translateAnimation4 = new TranslateAnimation(
|
160
|
+
Animation.ABSOLUTE, 0.0f,
|
161
|
+
Animation.ABSOLUTE, 300.0f,
|
162
|
+
Animation.ABSOLUTE, 0.0f,
|
163
|
+
Animation.ABSOLUTE, 0.0f);
|
164
|
+
}
|
165
|
+
else if(type == 1){
|
166
|
+
translateAnimation4 = new TranslateAnimation(
|
167
|
+
Animation.RELATIVE_TO_SELF, 0.0f,
|
168
|
+
Animation.RELATIVE_TO_SELF, 0.0f,
|
169
|
+
Animation.RELATIVE_TO_SELF, 0.0f,
|
170
|
+
Animation.RELATIVE_TO_SELF, 1.8f);
|
171
|
+
}
|
172
|
+
else if(type ==2){
|
173
|
+
translateAnimation4 = new TranslateAnimation(
|
174
|
+
Animation.RELATIVE_TO_PARENT, 0.0f,
|
175
|
+
Animation.RELATIVE_TO_PARENT, 0.0f,
|
176
|
+
Animation.RELATIVE_TO_PARENT, 0.0f,
|
177
|
+
Animation.RELATIVE_TO_PARENT, 0.6f);
|
178
|
+
}
|
179
|
+
|
180
|
+
// animation時間 msec
|
181
|
+
translateAnimation4.setDuration(1000);
|
182
|
+
// 繰り返し回数
|
183
|
+
translateAnimation4.setRepeatCount(0);
|
184
|
+
// animationが終わったそのまま表示にする
|
185
|
+
translateAnimation4.setFillAfter(true);
|
186
|
+
//アニメーションの開始
|
187
|
+
imageView.startAnimation(translateAnimation4);
|
188
|
+
}
|
189
|
+
}
|
67
190
|
```
|