程式碼實現drawable selector效果
阿新 • • 發佈:2018-12-31
專案中經常遇到一個按鈕需要不同的狀態效果,如點選效果、普通效果、禁用效果等,如果給每個控制元件都在drawable下新建個xml樣式檔案,那一個工程下來,就會有太多的xml檔案, 所以通過程式碼實現樣式效果,一勞永逸。
程式碼:
多張圖片樣式:
畫的多個shape樣式: drawable下的 selector 對應 GradientDrawable類<span style="font-size:14px;">public StateListDrawable createStateListDrawable() { StateListDrawable state = new StateListDrawable(); state.addState(new int[]{-android.R.attr.state_enabled}, mAttrs.getDisableDrawable()); state.addState(new int[]{android.R.attr.state_checked}, mAttrs.getCheckedDrawable()); state.addState(new int[]{android.R.attr.state_pressed}, mAttrs.getPressedDrawable()); state.addState(new int[]{}, mAttrs.getNormalDrawable()); return state; }</span>
<span style="font-size:14px;">/** * 形狀狀態 * @author thduan * 2015-12-29 15:12:19 */ public class ShapeStateListCreator { private ShapeAttrs mAttrs; public ShapeStateListCreator(ShapeAttrs attrs) { mAttrs = attrs; } public StateListDrawable createStateListDrawable() { DrawableAttrs drawableAttrs = new DrawableAttrs(); drawableAttrs.setNormalDrawable(normalDrawable()); drawableAttrs.setPressedDrawable(pressedDrawable()); drawableAttrs.setCheckedDrawable(checkedDrawable()); drawableAttrs.setDisableDrawable(disableDrawable()); return new DrawableStateListCreator(drawableAttrs).createStateListDrawable(); } private GradientDrawable disableDrawable() { if(mAttrs.hasDisableState()) { return newGradientDrawable(mAttrs.getDisableArgb(), mAttrs.getStrokeDisableArgb()); }else { return null; } } private GradientDrawable pressedDrawable() { if(mAttrs.hasPressedState()) { return newGradientDrawable(mAttrs.getPressedArgb(), mAttrs.getStrokePressedArgb()); }else { return null; } } private GradientDrawable checkedDrawable() { if(mAttrs.hasCheckedState()) { return newGradientDrawable(mAttrs.getCheckedArgb(), mAttrs.getStrokeCheckedArgb()); }else { return null; } } private GradientDrawable normalDrawable() { if(mAttrs.hasNormalState()) { return newGradientDrawable(mAttrs.getNormalArgb(), mAttrs.getStrokeNormalArgb()); }else { return null; } } private GradientDrawable newGradientDrawable(int color, int strokeColor) { GradientDrawable drawable = new GradientDrawable(); drawable.setShape(mAttrs.getShapeType()); drawable.setColor(color); drawable.setStroke(mAttrs.getStrokeWidth(), strokeColor); if(mAttrs.getCornerRadii() != null && mAttrs.getCornerRadii().length == 8) { drawable.setCornerRadii(mAttrs.getCornerRadii()); } return drawable; } } </span>
activity中使用:
ImageView imageView = (ImageView) findViewById(R.id.imageView); imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { } }); DrawableTools.setImageDrawableWithStateList(imageView, new ShapeAttrs() //畫形狀 .setNormalArgb("#D682A3") //普通時的顏色 .setPressedArgb("#A8D499") //按下時的顏色 .setStrokeWidth(4) //如果不希望有邊框,不設定該屬性即可,或設定為0 .shapeOval() //形狀為圓形, 還可以設定為矩形 .setStrokeNormalArgb("#0000ff") //普通時邊框的顏色 .setStrokePressedArgb("#F9DF92") //按下時邊框的顏色 );
工程程式碼上傳到csdn了,下載地址:http://download.csdn.net/detail/duantihi/9398346