Bitmap用法总结
1、Drawable → Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
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);
// canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
2、从资源中获取Bitmap
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
3、Bitmap → byte[]
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
4、byte[] → Bitmap
private Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}
5、保存bitmap
static boolean saveBitmap2file(Bitmap bmp,String filename){
CompressFormat format= Bitmap.CompressFormat.JPEG;
int quality = 100;
OutputStream stream = null;
try {
stream = new FileOutputStream("/sdcard/" + filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
e.printStackTrace();
}
return bmp.compress(format, quality, stream);
}
6、将图片按自己的要求缩放
// 图片源
Bitmap bm = BitmapFactory.decodeStream(getResources()
.openRawResource(R.drawable.dog));
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 设置想要的大小
int newWidth = 320;
int newHeight = 480;
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
// 放在画布上
canvas.drawBitmap(newbm, 0, 0, paint);
相关知识链接:http://www.eoeandroid.com/thread-3162-1-1.html
7、bitmap的用法小结
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = 2; //将图片设为原来宽高的1/2,防止内存溢出
Bitmap bm = BitmapFactory.decodeFile("",option);//文件流
URL url = new URL("");
InputStream is = url.openStream();
Bitmap bm = BitmapFactory.decodeStream(is);
android:scaleType:
android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType /
android:scaleType值的意义区别:
CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分
显示
CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长
(宽)
CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片
长/宽等于或小于View的长/宽
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示
FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
FIT_XY / fitXY 把图片 不按比例 扩大/缩小到View的大小显示
MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。
//放大缩小图片
public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidht = ((float)w / width);
float scaleHeight = ((float)h / height);
matrix.postScale(scaleWidht, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,
true);
return newbmp;
}
//将Drawable转化为Bitmap
public static Bitmap drawableToBitmap(Drawable drawable){
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height,
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0,0,width,height);
drawable.draw(canvas);
return bitmap;
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
}
//获得圆角图片的方法
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
//获得带倒影的图片方法
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){
final int reflectionGap = 4;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap,
0, height/2, width, height/2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2),
Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
canvas.drawRect(0, height,width,height + reflectionGap,
deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0,
bitmap.getHeight(), 0, bitmapWithReflection.getHeight()
+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
return bitmapWithReflection;
}
}
相关推荐
AI智能体升级实战:从规则匹配到Function Call,准确率提升86%的技术选型之路
AI智能体升级实战:从规则匹配到Function Call的技术演进 文章记录了AI邮件助手从传统规则匹配升级到Function Call的技术迭代过程。通过案例分析,揭示了规则匹配的三大核心问题:覆盖不全(用户表达千变万化)、组合困难(多条件容易漏掉)、维护噩梦(每个需求都要新增规则)。测试数据显示,规则匹配在复杂查询中的准确率仅51%,严重影响用户体验。 技术团队对比了三种升级方案:1) 继续优化规则匹配(天花板明显);2) 微调专用NLP模型(成本高);3) 采用Function Call(灵活高效)
Bitmap的相关方法汇总
Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。 1.Bitmap的创建 通过BitmapFactory创建 //第一种方式:从资源文件中得到图片 Bitmap rawBitmap1 = BitmapFactory.decodeResource(getResources(), R.dra
java三角套利_三角对冲套利-EA原理全解析
它是两个直盘货币和一个交叉盘货币之间的相互对冲,交易员往往受到外界因素的影响无法严格执行造成亏损。该系统经过大量数据统计分析和严格的科学论证计算,并通过多年实盘交易验证可以达到稳定盈利。希望交易员严格按照交易规则执行。此策略利用交叉外汇对的市场价格与其合成价格的价差来实现套利,还以EUR/USD和GBP为例:1,我们关注三个外汇对:EUR/USD,GBP/USD,以及GBP/EUR2,计算GBP/...
returnBitMap加载网络图片
为什么80%的码农都做不了架构师?>>> ...
避坑指南:Windows下OpenCV摄像头索引混乱问题的3种解决之道
本文详细解析了Windows下OpenCV摄像头索引混乱问题的3种解决方案,包括GUID查询法、DirectShow接口集成和注册表固化技术。针对工业视觉和智能监控场景,提供实战代码和性能优化建议,帮助开发者稳定识别多路摄像头设备,有效解决VideoCapture接口的索引漂移问题。
【Apollo规划模块探秘】—— 场景(Scenario)驱动的决策引擎
本文深入解析了Apollo自动驾驶平台规划模块的核心机制——场景(Scenario)驱动的决策引擎。文章通过类比日常驾驶,阐述了规划模块如何像经验丰富的司机一样,根据不同的路况(如车道跟随、无保护左转)切换不同的“驾驶模式”。重点剖析了场景管理器(ScenarioManager)如何基于感知信息进行场景判断与切换,以及“场景-阶段-任务”双层状态机如何精密协作,实现灵活、高效且安全的轨迹规划。
Android中Bitmap处理注意问题
一、问题的背景和意义 在Android移动应用开发中,对Bitmap的不小心处理,很容易引起程序内存空间耗尽而导致的程序崩溃问题。比如我们常遇到的问题: java.lang.OutofMemoryError: bitmap size exceeds VM budget. 导致该问题的出现,一般由以下几方面原因导致: 引动设备一般存储空间非常有限。当然不同设备分配给应用的内存空间是不...
GEE:面对对象土地利用分类(结合SNIC、GLCM 和机器学习算法)
本文将介绍在Google Earth Engine中结合SNIC、GLCM和机器学习算法的的土地利用/土地覆盖(LULC)分类方法。结果如下图所示,
HMS Core 统一扫码服务
统一扫码服务(Scan Kit)提供便捷的条形码和二维码扫描、解析、生成能力,帮助您快速构建应用内的扫码功能。
MimicKit 人形机器人强化学习全身模仿控制算法使用教程
本文介绍了MimicKit运动模仿算法框架的使用指南。主要内容包括:1)安装IsaacGym和依赖项;2)训练模型命令及参数说明;3)测试模型的方法;4)分布式训练配置;5)使用TensorBoard可视化日志;6)动作数据格式说明。该框架支持DeepMimic、AMP、ASE、ADD等算法,包含PPO、AWR等强化学习算法,适用于物理仿真角色动画控制研究。
金融数字化——详解2025年金融智能体深度应用报告【附全文阅读】
《2025年金融智能体深度应用报告》显示,金融智能体已实现80%部署率,推动行业效率提升30%-50%。报告指出智能体通过"感知-推理-规划-执行-进化"机制突破大模型局限,在风控、客服、投研等100+场景实现自动化。典型案例显示,工商银行信贷审批效率提升60%,蚂蚁数科智能客服解决80%问题。尽管面临系统协同、标准缺失等挑战,未来趋势将向多模态交互、自主进化和ESG投资等方向拓展。
android bitmap 百分比缩放,android 图片的缩放,bitmap的用法
一、将图片按自己的要求缩放// 图片源Bitmap bm =BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.dog));//获得图片的宽高int width =bm.getWidth();int height =bm.getHeight();//设置想要的大小int newWidth= 320;intnewH...
SAP UI5 本地开发工具 fiori-tools-proxy 中间件详解及使用场景
该中间件基于 http-proxy-middleware 库实现,分为 ui5 资源代理和 backend 代理两大模式,并可与 fiori run 或 ui5 serve 命令无缝集成,通过配置 ui5.path、ui5.url 以及 backend.path、backend.url 等参数,实现与目标系统的灵活对接和安全配置 (就后端系统代理而言,fiori-tools-proxy 内置 @sap-ux/backend-proxy-middleware,可将以指定前缀(如。
Android Bitmap.createBitmap() 用法全解析
操作方法示例创建空白 Bitmap旋转缩放平移水平翻转垂直翻转裁剪不仅能创建 Bitmap,还能旋转、缩放、平移、镜像翻转、裁剪,几乎所有图片变换都可以搞定!🚀希望这篇文章能帮你更好地理解的使用!😊。
WINCC7.3破解文件
WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!
LinePhone3.2.7(android端最新版本) sip客户端
SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/
安卓学习 之 bitmap用法
1. 获取资源 从资源文件得到图片 Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.haha); 读取sd卡(通过文件) String SDCarePath=Environment.getExternalStorageDirectory().toString(); String filePa...
pscad IEEE标准系统搭建说明
pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。
Android中Bitmap的各种用法
1、Drawable → Bitmap public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.get
Outlook Express 6 (OE6) for Win7/Win8/Win10
英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。
Android - Bitmap用法
代码直接复制使用,修改为自己需要的参数就可以了代码直接复制使用,修改为自己需要的参数就可以了。
1085




被折叠的 条评论
为什么被折叠?



