請設計一個應用程式,內有三個SeekBar,並分別代表著三原色(紅、綠、藍),值為0~255,只要
滑動其中一個SeekBar,背景的顏色就會跟著改變,
(結束滑動時,會顯示目前滑動顏色的數值為多 少,)
方法三:
(結束滑動時,會顯示目前滑動顏色的數值為多 少,)
方法三:
public class MainActivity extends AppCompatActivity { private ConstraintLayout constraintLayout; private SeekBar sbRed, sbGreen, sbBlue; private int Red, Green, Blue; private SeekBar.OnSeekBarChangeListener listener1= new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean b) { switch (seekBar.getId()){ case R.id.redBar: Red=progress; break; case R.id.greenBar: Green=progress; break; case R.id.blueBar: Blue=progress; break; } constraintLayout.setBackgroundColor(Color.rgb(Red, Green, Blue)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); changeView(); } private void changeView(){ constraintLayout = (ConstraintLayout) findViewById(R.id.constraintLo); sbRed = (SeekBar) findViewById(R.id.redBar); sbRed.setOnSeekBarChangeListener(listener1); sbGreen = (SeekBar) findViewById(R.id.greenBar); sbGreen.setOnSeekBarChangeListener(listener1); sbBlue = (SeekBar) findViewById(R.id.blueBar); sbBlue.setOnSeekBarChangeListener(listener1); } }
留言
張貼留言