Android Drawable 和String 相互转化
在我们经常应用开发中,经常用到将drawable和string相互转化。注意这情况最好用于小图片入icon等。
- public synchronized Drawable byteToDrawable(String icon) {
- byte[] img=Base64.decode(icon.getBytes(), Base64.DEFAULT);
- Bitmap bitmap;
- if (img != null) {
- bitmap = BitmapFactory.decodeByteArray(img,0, img.length);
- @SuppressWarnings("deprecation")
- Drawable drawable = new BitmapDrawable(bitmap);
- return drawable;
- }
- return null;
- }
- public synchronized String drawableToByte(Drawable drawable) {
- if (drawable != null) {
- Bitmap bitmap = Bitmap
- .createBitmap(
- drawable.getIntrinsicWidth(),
- drawable.getIntrinsicHeight(),
- drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
- : Bitmap.Config.RGB_565);
- Canvas canvas = new Canvas(bitmap);
- drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
- drawable.getIntrinsicHeight());
- drawable.draw(canvas);
- int size = bitmap.getWidth() * bitmap.getHeight() * 4;
- // 创建一个字节数组输出流,流的大小为size
- ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
- // 设置位图的压缩格式,质量为100%,并放入字节数组输出流中
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
- // 将字节数组输出流转化为字节数组byte[]
- byte[] imagedata = baos.toByteArray();
- String icon= Base64.encodeToString(imagedata, Base64.DEFAULT);
- return icon;
- }
- return null;
- }
Android Drawable 和String 相互转化的更多相关文章
- 【转】Android Drawable Resource学习(十一)、RotateDrawable
对另一个drawable资源,基于当前的level,进行旋转的drawable. 文件位置: res/drawable/filename.xml文件名即资源名 编译数据类型: 指向 RotateDra ...
- Android Drawable绘图学习笔记(转)
如何获取 res 中的资源 数据包package:android.content.res 主要类:Resources Android SDK中的简介:Class for accessing an ap ...
- Android Drawable Mipmap Vector使用及Vector兼容
原文地址:http://blog.csdn.net/eclipsexys/article/details/51838119 http://blog.csdn.net/qq_15545283/artic ...
- Android Drawable的9种子类 介绍
原文: Android Drawable的9种子类 介绍 Drawable 在android里面 就是代表着图像,注意是图像 而不是图片. 图片是图像的子集.图像除了可以包含图片以外 还可以包含颜 ...
- CString 与 std::string 相互转化
MFC中CString 与 std::string 相互转化 CString实际是CStringT, 也就是模板类, 在UNICODE环境下,实际是CStringW, 在多字符集环境下,实际是CStr ...
- Android Drawable 与 LayerList综合汇总
先看需求.要求这样的效果 上代码 <?xml version="1.0" encoding="utf-8"? > <layer-list xm ...
- 【转】【Android】Android Drawable Shape 组合画田字格
使用layer-list组合多个Shap <?xml version="1.0" encoding="utf-8"?> <layer-list ...
- Android Studio插件:Android Drawable Importer
Android Drawable Importer 为了在不同分辨率的设备上更好的展示图片的效果,我们往往需要在 res/drawable 中添加不同分辨率的图片.有时我们可能手里只有一份分辨率的图片 ...
- 17.Letter Combinations of a Phone Number (char* 和 string 相互转化)
leetcode 第17题 分析 char*和string相互转化 char*(或者char)转string 可以看看string的构造函数 default (1) string(); copy (2 ...
随机推荐
- iOS - 打电话, 发短信
电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...
- WebViewJavascriptBridge
上一篇文章介绍了通过UIWebView实现了OC与JS交互的可能性及实现的原理,并且简单的实现了一个小的示例DEMO,当然也有一部分遗留问题,使用原生实现过程比较繁琐,代码难以维护.这篇文章主要介绍下 ...
- 在ios开发中有多少常用的加密解密方式(备用)
最常用的是MD5和base64编码,还有DES 3DES AES加密 ios怎么实现RAS加密解密 最近几天折腾了一下如何在iOS上使用RSA来加密.iOS上并没有直接的RSA加密API.但是iOS提 ...
- asp.net中Web使用Socket
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- 关于Matlab作图的若干问题
看到了北京一则新闻,想到如何测试双向镜子?百度之. 只要做以下简单的测试:把你的指甲尖放在镜子表面,如果在指甲尖与倒映图像之间有间隙,那就是真的镜子.然而,如果你 ...
- IronPython脚本调用C#dll示例
上篇Python脚本调用C#代码数据交互示例(hello world)介绍了与C#紧密结合的示例,这里还将提供一个与C#结合更紧密的示例,直接调用C#编写的DLL. 我们还是沿用了上篇文章的 ...
- mysql注入攻击及防范
一.注入攻击种类 1. GET注入 输入参数通过URL发送. 2. POST注入 输入参数通过HTTP正文发送 3. COOKIE注入 ...
- CISCO的HTTP/HTTPS/SSH配置测试完成
按实验一步一步,倒是很容易的,也理解罗~~ START-CONFIG粗配置文件如下: r1#show run Building configuration... Current configurati ...
- 嵌套滚动demo
https://github.com/luv135/NestedScrollingDemo https://github.com/ggajews/nestedscrollingchildviewdem ...
- 【HDOJ】1180 诡异的楼梯
bfs+优先队列.wa了N次,才发现可以停留等待楼梯变换方向. #include <iostream> #include <queue> #include <cstdio ...