android:background="@color/color_tabbar_background"
public class BasicActivity extends Activity { private ResourceManager manager = null; @Override public Resources getResources() { if (App.checkCurrentSkin()) { if (manager == null) { manager = new ResourceManager(super.getResources()); } return manager; } else return super.getResources(); } }
public class ResourceManager extends Resources { Skin mSkin = App.getCurrentSkin(); Method loadDrawable; Method loadColorStateList; Resources mBaseResources; public ResourceManager(Resources baseResources) { super(baseResources.getAssets(), baseResources.getDisplayMetrics(), baseResources.getConfiguration()); mBaseResources = baseResources; Method[] methods = Resources.class.getDeclaredMethods(); for (Method method : methods) { if (method.getName().equals("loadDrawable")) { loadDrawable = method; loadDrawable.setAccessible(true); } else if (method.getName().equals("loadColorStateList")) { loadColorStateList = method; loadColorStateList.setAccessible(true); } } }
@Override public int getColor(int id) throws NotFoundException { String color; switch (id) { case R.color.chat_in: color = mSkin.getControlWithType(SkinConstants.SKIN_CONTROL_RECEIVE_MESSAGE_BUBLE).getBackgroundColor(); // #b7c0c7 return Color.parseColor(color); case R.color.chat_send: color = mSkin.getControlWithType(SkinConstants.SKIN_CONTROL_SEND_MESSAGE_BUBLE).getBackgroundColor(); return Color.parseColor(color); ….
public Drawable loadDrawable(TypedValue value, int id) { Drawable d = null; String color; String colorSel; GradientDrawable result; switch (value.resourceId) { case R.color.color_background_main: color = mSkin.getControlWithType(SkinConstants.SKIN_CONTROL_VIEW).getBackgroundColor(); return new ColorDrawable(Color.parseColor(color)); case R.drawable.button_blue_selector: color = mSkin.getControlWithType(SkinConstants.SKIN_CONTROL_BUTTON_BLUE).getBackgroundColor(); colorSel = mSkin.getControlWithType(SkinConstants.SKIN_CONTROL_BUTTON_BLUE).getHighlightedBackgroundColor(); return createSelector(Color.parseColor(color), getDarkerColor(color), colorSel); case R.drawable.chat_in: color = mSkin.getControlWithType(SkinConstants.SKIN_CONTROL_RECEIVE_MESSAGE_BUBLE).getBackgroundColor(); result = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{Color.parseColor(color), Color.parseColor(color)}); result.setCornerRadius(25); return result; case R.drawable.list_view_selector: colorSel = mSkin.getControlWithType(SkinConstants.SKIN_CONTROL_TABLE_VIEW_CELL) .getHighlightedBackgroundColor(); ColorDrawable drawable = new ColorDrawable(Color.parseColor(colorSel)); StateListDrawable listDrawable = new StateListDrawable(); listDrawable.addState(new int[]{android.R.attr.state_pressed}, drawable); return listDrawable; case R.drawable.button_last_menu_selector: color = mSkin.getControlWithType(SkinConstants.SKIN_CONTROL_TABLE_VIEW_CELL).getBackgroundColor(); colorSel = mSkin.getControlWithType(SkinConstants.SKIN_CONTROL_TABLE_VIEW_CELL) .getHighlightedBackgroundColor(); return createSelectorForDifferentCorers(Color.parseColor(color), getDarkerColor(color), colorSel, 0, 0, 15, 15); default: break; } try { d = (Drawable) loadDrawable.invoke(mBaseResources, value, id); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return d; }
@Override public ColorStateList getColorStateList(int id) throws NotFoundException { String color; String colorSel; ColorStateList res; switch (id) { case R.drawable.tab_text_selector: color = mSkin.getControlWithType(SkinConstants.SKIN_CONTROL_TABBAR_ITEM).getTextColor(); colorSel = mSkin.getControlWithType(SkinConstants.SKIN_CONTROL_TABBAR_ITEM).getHighlightedTextColor(); res = new ColorStateList(new int[][]{new int[]{android.R.attr.state_selected}, new int[]{-android.R.attr.state_selected}}, new int[]{Color.parseColor(colorSel), Color.parseColor(color)}); return res; default: break; } return super.getColorStateList(id); }
private Drawable createSelectorForDifferentCorers(int startColor, int endColor, String colorSel, float topLeft, float topRight, float bottomRight, float bottomLeft) { StateListDrawable drawable = new StateListDrawable(); GradientDrawable gradientDrawable1 = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{startColor, endColor}); GradientDrawable gradientDrawable2 = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{ Color.parseColor(colorSel), getDarkerColor(colorSel)}); gradientDrawable1.setShape(GradientDrawable.RECTANGLE); gradientDrawable1.setCornerRadii(new float[]{topLeft, topLeft, topRight, topRight, bottomRight, bottomRight, bottomLeft, bottomLeft}); gradientDrawable1.setStroke(1, endColor); gradientDrawable2.setShape(GradientDrawable.RECTANGLE); gradientDrawable2.setCornerRadii(new float[]{topLeft, topLeft, topRight, topRight, bottomRight, bottomRight, bottomLeft, bottomLeft}); gradientDrawable2.setStroke(1, endColor); drawable.addState(new int[]{android.R.attr.state_pressed}, gradientDrawable2); drawable.addState(new int[]{android.R.attr.state_checked}, gradientDrawable2); drawable.addState(new int[]{-android.R.attr.state_selected}, gradientDrawable1); return drawable; }
Drawable example = ImageManager.blendColorDrawable(this, R.drawable.arrow, R.color.color_2_end);
public static Drawable blendColorDrawable(Context context, int baseId, int colorId) { Resources res = context.getResources(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; Bitmap base = BitmapFactory.decodeResource(res, baseId, options); Bitmap blend = Bitmap.createBitmap(base.getWidth(), base.getHeight(), Config.ARGB_8888); blend.eraseColor(context.getResources().getColor(colorId)); Bitmap result = base.copy(Config.ARGB_8888, true); Paint p = new Paint(); p.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY)); p.setShader(new BitmapShader(blend, TileMode.CLAMP, TileMode.CLAMP)); Canvas c = new Canvas(); c.setBitmap(result); c.drawBitmap(base, 0, 0, null); c.drawRect(0, 0, base.getWidth(), base.getHeight(), p); return new BitmapDrawable(context.getResources(), result);
Source: https://habr.com/ru/post/165787/