My current task is to set background color for several linear layouts according to the seek bar changes. I'm trying to store layouts in a linked HashMap
as keys and use arrays with rgb integers as their values. I'm also using random class to set the default rgb values.
I've been thinking for a while about what structure to choose and decided to use linked hash map to have easy access to the rgb values in onStopTrackingTouch method and change them from default after seek bar has been moved.
But seems that I'm moving in a wrong direction. I stuck in lots of types and this code doesn't work, but it isn't the main question. What I'm really doubt about is that this structure will work for me. Is there any way to make it simplier?
public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {
private LinearLayout mTopLeftLayout;
private LinearLayout mBottomLeftLayout;
private LinearLayout mTopRigthLayout;
private LinearLayout mMiddleRightLayout;
private LinearLayout mBottomRightLayout;
LinkedHashMap<View, ArrayList<Integer[]>> colorMap = new LinkedHashMap<View, ArrayList<Integer[]>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SeekBar seekBar = (SeekBar)findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(this);
mTopLeftLayout = (LinearLayout)findViewById(R.id.topLeftLayout);
mBottomLeftLayout = (LinearLayout)findViewById(R.id.bottomLeftLayout);
colorMap.put(mBottomLeftLayout, null);
mTopRigthLayout = (LinearLayout)findViewById(R.id.topRigthLayout);
colorMap.put(mTopRigthLayout, null);
mBottomRightLayout = (LinearLayout)findViewById(R.id.bottomRightLayout);
colorMap.put(mBottomLeftLayout, null);
setRandomBackground();
}
private void setRandomBackground(){
Random rnd = new Random();
for (Map.Entry<View, ArrayList<Integer[]>> color: colorMap.entrySet()){
ArrayList<Integer[]> value = color.getValue();
for (int i=0;i<3; i++) {
value.add(rnd.nextInt(256));
}
int backgroundColor = Color.rgb(value[1], value[2], value[3]);
color.setBackgroundColor(color);
}
}
//seekBar methods goes here
}