Created
March 21, 2015 20:46
-
-
Save alexzaitsev/b2eb80cc76af66c98977 to your computer and use it in GitHub Desktop.
Android Material Shape Generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<color name="material_red">#F44336</color> | |
<color name="material_pink">#E91E63</color> | |
<color name="material_purple">#9C27B0</color> | |
<color name="material_deep_purple">#673AB7</color> | |
<color name="material_indigo">#3F51B5</color> | |
<color name="material_blue">#2196F3</color> | |
<color name="material_light_blue">#03A9F4</color> | |
<color name="material_cyan">#00BCD4</color> | |
<color name="material_teal">#009688</color> | |
<color name="material_green">#4CAF50</color> | |
<color name="material_light_green">#8BC34A</color> | |
<color name="material_lime">#CDDC39</color> | |
<color name="material_yellow">#FFEB3B</color> | |
<color name="material_amber">#FFC107</color> | |
<color name="material_orange">#FF9800</color> | |
<color name="material_deep_orange">#FF5722</color> | |
<color name="material_brown">#795548</color> | |
<color name="material_grey">#9E9E9E</color> | |
<color name="material_blue_grey">#607D8B</color> | |
</resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.graphics.drawable.Drawable; | |
import android.graphics.drawable.ShapeDrawable; | |
import android.graphics.drawable.shapes.OvalShape; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Random; | |
import pro.alex_zaitsev.currency.R; | |
/** | |
* @author A.Zaitsev | |
*/ | |
public class MaterialShapeGenerator { | |
private static final int[] MATERIAL_COLOR_IDS = new int[]{ | |
R.color.material_red, | |
R.color.material_pink, | |
R.color.material_purple, | |
R.color.material_deep_purple, | |
R.color.material_indigo, | |
R.color.material_blue, | |
R.color.material_light_blue, | |
R.color.material_cyan, | |
R.color.material_teal, | |
R.color.material_green, | |
R.color.material_light_green, | |
R.color.material_lime, | |
R.color.material_yellow, | |
R.color.material_amber, | |
R.color.material_orange, | |
R.color.material_deep_orange, | |
R.color.material_brown, | |
R.color.material_grey, | |
R.color.material_blue_grey | |
}; | |
private int[] materialColors; | |
private Random random; | |
private Map<Integer, Drawable> drawableCache = new HashMap<>(); | |
public MaterialShapeGenerator(Context context) { | |
materialColors = new int[MATERIAL_COLOR_IDS.length]; | |
for (int i = 0; i < MATERIAL_COLOR_IDS.length; i++) { | |
materialColors[i] = context.getResources().getColor(MATERIAL_COLOR_IDS[i]); | |
} | |
random = new Random(System.currentTimeMillis()); | |
} | |
private int generateRandomMaterialColor() { | |
int index = random.nextInt(materialColors.length); | |
return materialColors[index]; | |
} | |
public Drawable generateCircleDrawable(int color) { | |
ShapeDrawable drawable = new ShapeDrawable(new OvalShape()); | |
drawable.getPaint().setColor(color); | |
return drawable; | |
} | |
public Drawable generateAndCacheCircleDrawable() { | |
int materialColor = generateRandomMaterialColor(); | |
Drawable cachedDrawable = drawableCache.get(materialColor); | |
if (cachedDrawable == null) { | |
drawableCache.put(materialColor, cachedDrawable = generateCircleDrawable(materialColor)); | |
} | |
return cachedDrawable; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment