/** * apply background in view. possible type : - COLOR - REF => search for that drawable in * resources - BASE64 => convert base64 to bitmap and apply in view */ public static void applyBackground(View view, DynamicProperty property) { if (view != null) { switch (property.type) { case COLOR: { view.setBackgroundColor(property.getValueColor()); } break; case REF: { view.setBackgroundResource(getDrawableId(view.getContext(), property.getValueString())); } break; case BASE64: { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) view.setBackground(property.getValueBitmapDrawable()); else view.setBackgroundDrawable(property.getValueBitmapDrawable()); } break; case DRAWABLE: { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) view.setBackground(property.getValueGradientDrawable()); else view.setBackgroundDrawable(property.getValueGradientDrawable()); } break; } } }
/** * apply compound property in textView position 0:left, 1:top, 2:right, 3:bottom - REF : drawable * to load as compoundDrawable - BASE64 : decode as base64 and set as CompoundDrawable */ public static void applyCompoundDrawable(View view, DynamicProperty property, int position) { if (view instanceof TextView) { TextView textView = (TextView) view; Drawable[] d = textView.getCompoundDrawables(); switch (property.type) { case REF: { try { d[position] = view.getContext() .getResources() .getDrawable(getDrawableId(view.getContext(), property.getValueString())); } catch (Exception e) { } } break; case BASE64: { d[position] = property.getValueBitmapDrawable(); } break; case DRAWABLE: { d[position] = property.getValueGradientDrawable(); } break; } textView.setCompoundDrawablesWithIntrinsicBounds(d[0], d[1], d[2], d[3]); } }