A simpler mechanism to custom statelistdrawables
private Button newbutton(){
Button new = new Button(context);
new.setBackgroundDrawable(custombtnstate());
}
private StateListDrawable custombtnstate(){
StateListDrawable draw = new StateListDrawable();
Drawable bkgd = new Drawable(){
@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawColor(Color.rgb(22, 33, 44)); //Custom color 1
}
@Override
public int getOpacity() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setAlpha(int alpha) {
// TODO Auto-generated method stub
}
@Override
public void setColorFilter(ColorFilter cf) {
// TODO Auto-generated method stub
}
};
Drawable bkgd1 = new Drawable(){
@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawColor(Color.rgb(150, 180, 190));//Custom color 2
}
@Override
public int getOpacity() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setAlpha(int alpha) {
// TODO Auto-generated method stub
}
@Override
public void setColorFilter(ColorFilter cf) {
// TODO Auto-generated method stub
}
};
//Essential
int stateFocused = android.R.attr.state_focused;
int statePressed = android.R.attr.state_pressed;
int stateSelected = android.R.attr.state_selected;
draw.addState(new int[] {statePressed}, bkgd1);
draw.addState(new int[] {stateSelected}, bkgd1);
draw.addState(new int[] {stateFocused}, bkgd1);
draw.addState(new int[] {-stateFocused, -statePressed, -stateSelected}, bkgd);
return draw;
}
private Button newbutton(){
Button new = new Button(context);
new.setBackgroundDrawable(custombtnstate());
}
private StateListDrawable custombtnstate(){
StateListDrawable draw = new StateListDrawable();
Drawable bkgd = new Drawable(){
@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawColor(Color.rgb(22, 33, 44)); //Custom color 1
}
@Override
public int getOpacity() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setAlpha(int alpha) {
// TODO Auto-generated method stub
}
@Override
public void setColorFilter(ColorFilter cf) {
// TODO Auto-generated method stub
}
};
Drawable bkgd1 = new Drawable(){
@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawColor(Color.rgb(150, 180, 190));//Custom color 2
}
@Override
public int getOpacity() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void setAlpha(int alpha) {
// TODO Auto-generated method stub
}
@Override
public void setColorFilter(ColorFilter cf) {
// TODO Auto-generated method stub
}
};
//Essential
int stateFocused = android.R.attr.state_focused;
int statePressed = android.R.attr.state_pressed;
int stateSelected = android.R.attr.state_selected;
draw.addState(new int[] {statePressed}, bkgd1);
draw.addState(new int[] {stateSelected}, bkgd1);
draw.addState(new int[] {stateFocused}, bkgd1);
draw.addState(new int[] {-stateFocused, -statePressed, -stateSelected}, bkgd);
return draw;
}
No comments:
Post a Comment