Skip to content

Instantly share code, notes, and snippets.

@catvinhquang
Created March 28, 2020 08:09
Show Gist options
  • Save catvinhquang/d78ad42c04e8f1a91537e4d3c57e52d8 to your computer and use it in GitHub Desktop.
Save catvinhquang/d78ad42c04e8f1a91537e4d3c57e52d8 to your computer and use it in GitHub Desktop.
package com.example.myapplication;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.Log;
import androidx.core.content.ContextCompat;
import java.io.File;
import java.io.FileOutputStream;
public class VectorDrawableToPng {
// adb -s emulator-5554 shell wm density 420
public static void convert(final Context ctx) {
new Thread(new Runnable() {
@Override
public void run() {
int[] res = new int[]{
R.drawable.a,
R.drawable.b,
R.drawable.c
};
String root = "/sdcard/res/";
new File(root + "drawable/").delete();
new File(root + "drawable/").mkdirs();
new File(root + "drawable-hdpi/").delete();
new File(root + "drawable-hdpi/").mkdirs();
new File(root + "drawable-xhdpi/").delete();
new File(root + "drawable-xhdpi/").mkdirs();
new File(root + "drawable-xxhdpi/").delete();
new File(root + "drawable-xxhdpi/").mkdirs();
new File(root + "drawable-xxxhdpi/").delete();
new File(root + "drawable-xxxhdpi/").mkdirs();
for (int i : res) {
exportDrawable(ctx, i);
}
}
}).start();
}
public static void exportDrawable(Context ctx, int id) {
Drawable d = ContextCompat.getDrawable(ctx, id);
int w = d.getIntrinsicWidth();
int h = d.getIntrinsicHeight();
float ratio = (float) w / h;
String root = "/sdcard/res/";
String name = ctx.getResources().getResourceEntryName(id);
int sW, sH;
Log.e("quang", "exportDrawable: " + name + " " + w + "x" + h);
int min = 36;
try {
// drawable + hdpi: 36
if (w < h) {
sW = min;
sH = (int) (sW / ratio);
} else {
sH = min;
sW = (int) (sH * ratio);
}
Bitmap b = Bitmap.createBitmap(sW, sH, Bitmap.Config.ARGB_8888);
d.setBounds(0, 0, sW, sH);
d.draw(new Canvas(b));
b.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(root + "drawable/" + name + ".png"));
b.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(root + "drawable-hdpi/" + name + ".png"));
// xhdpi: 48
if (w < h) {
sW = (int) (min / 0.75);
sH = (int) (sW / ratio);
} else {
sH = (int) (min / 0.75);
sW = (int) (sH * ratio);
}
b = Bitmap.createBitmap(sW, sH, Bitmap.Config.ARGB_8888);
d.setBounds(0, 0, sW, sH);
d.draw(new Canvas(b));
b.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(root + "drawable-xhdpi/" + name + ".png"));
// xxhdpi: 72
if (w < h) {
sW = min * 2;
sH = (int) (sW / ratio);
} else {
sH = min * 2;
sW = (int) (sH * ratio);
}
b = Bitmap.createBitmap(sW, sH, Bitmap.Config.ARGB_8888);
d.setBounds(0, 0, sW, sH);
d.draw(new Canvas(b));
b.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(root + "drawable-xxhdpi/" + name + ".png"));
// xxxhdpi: 96
if (w < h) {
sW = (int) (min / 0.375);
sH = (int) (sW / ratio);
} else {
sH = (int) (min / 0.375);
sW = (int) (sH * ratio);
}
b = Bitmap.createBitmap(sW, sH, Bitmap.Config.ARGB_8888);
d.setBounds(0, 0, sW, sH);
d.draw(new Canvas(b));
b.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(root + "drawable-xxxhdpi/" + name + ".png"));
} catch (Throwable t) {
t.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment