Skip to content

Instantly share code, notes, and snippets.

@jaimet
Forked from mridang/ImageBuilder.java
Created August 17, 2018 21:57
Show Gist options
  • Save jaimet/0994a946d0069d9a36c7c7f1e5126810 to your computer and use it in GitHub Desktop.
Save jaimet/0994a946d0069d9a36c7c7f1e5126810 to your computer and use it in GitHub Desktop.
Gasflow Notification Icon Generator
package com.mridang.dashbar;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Typeface;
import android.os.Environment;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Generates images for notification icons used in Gasflow by drawing on a 2D Canvas and then
* exporting the rendered image as PNG to the external storage directory.
* <br />
* Once generated, all the images can be pulled locally over ADB by using the following command
* <code>adb pull /sdcard/Speedo/</code>
*/
public class ImageBuilder {
public static void generateImages(String directory, int size) {
File speedo = new File(Environment.getExternalStorageDirectory().toString(), "Speedo");
if (!speedo.exists()) {
//noinspection ResultOfMethodCallIgnored
speedo.mkdir();
}
File resources = new File(speedo, directory);
if (!resources.exists()) {
//noinspection ResultOfMethodCallIgnored
resources.mkdir();
}
for (int i = 0; i <= 1180; i++) {
String fname = "";
String value = "";
String bytes = "";
if (i < 1000) {
bytes = "KB/s";
value = String.valueOf(i);
fname = String.format("wkb%03d.png", i);
} else if (i < 1090) {
bytes = "MB/s";
value = String.format("%.1f", (10 + (i - 1000)) / 10D);
int speed = 10 + (i - 1000);
fname = String.format("wmb%03d.png", speed);
} else if (i < 1180) {
bytes = "MB/s";
value = String.valueOf(i - 1090 + 10);
int speed = 100 + (i - 1090);
fname = String.format("wmb%03d.png", speed);
} else {
bytes = "MB/s";
value = "99+";
int speed = 100 + (i - 1090);
fname = String.format("wmb%03d.png", speed);
}
System.out.println("Rendering " + value + bytes + " to " + fname);
Bitmap bitmap = render(size, value, bytes);
try {
File image = new File(resources, fname);
if (image.createNewFile()) {
FileOutputStream png = new FileOutputStream(image);
try {
bitmap.compress(Bitmap.CompressFormat.PNG, 90, png);
} finally {
png.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static Bitmap render(int size, String value, String bytes) {
Paint paint = new Paint();
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
paint.setColor(Color.TRANSPARENT);
canvas.drawRect(0, 0, size, size, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
paint.setTextSize(size * 0.61F);
paint.setAlpha(0);
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.CENTER);
paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
paint.setColor(Color.RED);
canvas.drawText(value, size / 2, size / 2, paint);
paint.setTextSize(size * 0.4F);
paint.setAlpha(0);
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.CENTER);
paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
paint.setColor(Color.RED);
canvas.drawText(bytes, size / 2, size, paint);
return bitmap;
}
/**
* Main method that generates the images for each of the DPI resolutions into the respective
* DPI directory
*
* @param args None
*/
public static void main(String[] args) {
generateImages("drawable-mdpi", 24);
generateImages("drawable-hdpi", 36);
generateImages("drawable-xhdpi", 48);
generateImages("drawable-xxhdpi", 72);
generateImages("drawable-xxxhdpi", 96);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment