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