private static int LENGTH = 50;
private static String[] datas = new String[LENGTH];
private ListView mListView;
private Spinner mSpinner;
private TextView txtView01;
private TextView txtView02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_statelist_drawable);
findView();
initData();
initListView();
initSpinner();
}
private void initSpinner() {
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, android.R.id.text1, datas);
spinnerAdapter.setDropDownViewResource(R.layout.item_listview);
mSpinner.setAdapter(spinnerAdapter);
}
private void initListView() {
ArrayAdapter<String> adapterList = new ArrayAdapter<>(this,
R.layout.item_listview, datas);
mListView.setAdapter(adapterList);
mListView.setItemChecked(0, true);
}
private void findView() {
mListView = (ListView) findViewById(R.id.listView);
mSpinner = (Spinner) findViewById(R.id.spinner);
txtView01 = (TextView) findViewById(R.id.txtView01);
txtView02 = (TextView) findViewById(R.id.txtView02);
StateListDrawable mStateDrawable = new StateListDrawable();
int colorDisabled = 0xffdf2d2d;
int colorPressed = 0xffdfff2d;
int strokeColor = 0xff23ff55;
int strokeWidth = 4;
float [] radii = {10,10,10,10,10,10,10,10};
GradientDrawable normal = createDrawable(colorDisabled, strokeColor, strokeWidth, radii, false, 0, 0);
GradientDrawable disabled = createDrawable(colorDisabled, strokeColor, strokeWidth, radii, false, 0, 0);
GradientDrawable pressed = createDrawable(colorPressed, strokeColor, strokeWidth, radii, false, 0, 0);
GradientDrawable focus = createDrawable(colorPressed, strokeColor, strokeWidth, radii, false, 0, 0);
GradientDrawable checked = createDrawable(colorPressed, strokeColor, strokeWidth, radii, false, 0, 0);
mStateDrawable.addState(new int[] { -android.R.attr.state_enabled },disabled);
mStateDrawable.addState(new int[]{android.R.attr.state_focused}, focus);
mStateDrawable.addState(new int[]{android.R.attr.state_pressed}, pressed);
mStateDrawable.addState(new int[]{android.R.attr.state_enabled}, normal);
mStateDrawable.addState(new int[]{android.R.attr.state_checked}, checked);
mStateDrawable.addState(new int[]{}, normal);
txtView02.setBackgroundDrawable(mStateDrawable);
}
private GradientDrawable createDrawable(int color, int strokeColor,int strokeWidth,float[] radii,boolean isDash, float dashWidth,float dashGap) {
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(color);
drawable.setCornerRadii(radii);
if (!isDash) {
drawable.setStroke(strokeWidth, strokeColor);
} else {
drawable.setStroke(strokeWidth, strokeColor, dashWidth, dashGap);
}
return drawable;
}
private void initData() {
for (int i = 0; i < LENGTH; i++) {
datas[i] = i + "";
}
}