teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

コード

2015/05/14 12:26

投稿

swordone
swordone

スコア20675

answer CHANGED
@@ -1,3 +1,51 @@
1
1
  Activityにアニメーションの状態を表す数値を保持させて,例えばアニメーションしてなければ0,
2
2
  アニメーションされていればボタンのidを保持させます.(状態の数値は,それぞれ異なれば何でもいい)
3
- そしてonClickでその状態をチェックし,押されたボタンのidと違う時だけ数値変更とアニメーションをすればいいのではないでしょうか.
3
+ そしてonClickでその状態をチェックし,押されたボタンのidと違う時だけ数値変更とアニメーションをすればいいのではないでしょうか.
4
+
5
+ ちょっと元のコードが危ない感じがした(2つのボタンを同じ変数で扱っている)のと,アニメーションのメソッドが2つあったのが気になったので,全体を書いてみました.
6
+ ```lang-java
7
+ public class MainActivity extends Activity implements View.OnClickListener{
8
+ int mode = 0;
9
+ Button btnStart, btnStart1;
10
+ ImageView image;
11
+
12
+ public void onCreate(Bundle savedInstanceState){
13
+ super.onCreate(savedInstanceState);
14
+ setContentView(R.layout.activity_main);
15
+ image = (ImageView) findViewById(R.id.image);
16
+
17
+ btnStart = (Button) findViewById(R.id.start);
18
+ btnStart.setOnClickListener(this);
19
+
20
+ btnStart1 = (Button) findViewById(R.id.start1);
21
+ btnStart1.setOnClickListener(this);
22
+ }
23
+
24
+ public void onClick(View v){
25
+ int id = v.getId();
26
+ if(id != mode){
27
+ mode = id;
28
+ switch(id){
29
+ case R.id.start:
30
+ startColorAnimation(image, 0xFFffffff, 0xFF000000);
31
+ break;
32
+ case R.id.start1:
33
+ startColorAnimation(image, 0xfff53b6d, 0xFF000000);
34
+ break;
35
+ }
36
+ }
37
+ }
38
+
39
+
40
+ public void startColorAnimation(View view, int colorStart, int colorEnd){
41
+
42
+ ValueAnimator colorAnim = ObjectAnimator.ofInt(
43
+ view, "backgroundColor", colorStart, colorEnd);
44
+ colorAnim.setDuration(2000);
45
+ colorAnim.setEvaluator(new ArgbEvaluator());
46
+ colorAnim.setRepeatCount(100);
47
+ colorAnim.setRepeatMode(ValueAnimator.REVERSE);
48
+ colorAnim.start();
49
+ }
50
+ }
51
+ ```