カテゴリー
Android Java

Androidで背景色のTransition

public class MainActivity extends AppCompatActivity {
  Button button;
  LinearLayout layout;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    layout = new LinearLayout(this);
    layout.setGravity(Gravity.CENTER);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

    layout.setLayoutParams(params);

    button = new Button(this);
    button.setOnClickListener(new OnButtonClicked());
    layout.addView(button);
    setContentView(layout);
  }

  private class OnButtonClicked implements View.OnClickListener {
    @Override
    public void onClick(View v) {
      layout.setBackgroundColor(Color.RED);
      ColorDrawable[] colors = {new ColorDrawable(Color.RED), new ColorDrawable(Color.WHITE)};
      TransitionDrawable transition = new TransitionDrawable(colors);
      layout.setBackground(transition);
      transition.startTransition(2000);
      Log.d("MyTest", "clicked");
    }
  }
}