Notice: 函数 WP_Scripts::localize 的调用方法不正确$l10n 参数必须是一个数组。若要将任意数据传递给脚本,请改用 wp_add_inline_script() 函数。 请查阅调试 WordPress来获取更多信息。 (这个消息是在 5.7.0 版本添加的。) in /data/www/appblog/wp-includes/functions.php on line 6131

Android中Bitmap、Drawable、byte[]互转

Bitmap -> Drawable

Drawable drawable = new BitmapDrawable(bmp);

Drawable -> Bitmap

//Drawable资源
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.sample);

//Drawable对象
public Bitmap drawable2Bitmap(Drawable drawable) {
    Config config;
    if (drawable.getOpacity() != PixelFormat.OPAQUE) {
        config = Bitmap.Config.ARGB_8888;
    } else {
        config = Bitmap.Config.RGB_565;
    }
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight(), config);
    Canvas canvas = new Canvas(bitmap);
    // canvas.setBitmap(bitmap);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                             drawable.getIntrinsicHeight());
    drawable.draw(canvas);
    return bitmap;
}

Bitmap -> byte[]

private byte[] bitmap2Bytes(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    return baos.toByteArray();
}

byte[] -> Bitmap

private Bitmap bytes2Bitmap(byte[] bytes) {
    if (bytes.length != 0) {
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
    } else {
        return null;
    }
}
上一篇 Android解决帧动画OOM的组件FrameAnimDrawable
下一篇 Android Mail开发库