位置: 编程技术 - 正文

Android Bitmap总结(android/bitmap.h)

编辑:rootadmin

推荐整理分享Android Bitmap总结(android/bitmap.h),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:android bitmap recycle,android bitmap recycle,android中bitmap,android bitmapdrawable,android inbitmap,android bitmapdrawable,android/bitmap.h,android bitmapfactory,内容如对您有帮助,希望把文章链接给更多的朋友!

BitMap类: public void recycle()——回收位图占用的内存空间,把位图标记为Dead public final boolean isRecycled() ——判断位图内存是否已释放 public final int getWidth()——获取位图的宽度 public final int getHeight()——获取位图的高度 public final boolean isMutable()——图片是否可修改 public int getScaledWidth(Canvas canvas)——获取指定密度转换后的图像的宽度 public int getScaledHeight(Canvas canvas)——获取指定密度转换后的图像的高度 public boolean compress(CompressFormat format, int quality, OutputStream stream)——按指定的图片格式以及画质,将图片转换为输出流。 format:Bitmap.CompressFormat.PNG或Bitmap.CompressFormat.JPEG quality:画质,0-.0表示最低画质压缩,以最高画质压缩。对于PNG等无损格式的图片,会忽略此项设置。

常用的静态方法: public static Bitmap createBitmap(Bitmap src) ——以src为原图生成不可变得新图像 public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)——以src为原图,创建新的图像,指定新图像的高宽以及是否可变。 public static Bitmap createBitmap(int width, int height, Config config)——创建指定格式、大小的位图 public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)以source为原图,创建新的图片,指定起始坐标以及新图像的高宽。 public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

BitmapFactory工厂类: Option 参数类: public boolean inJustDecodeBounds——如果设置为true,不获取图片,不分配内存,但会返回图片的高度宽度信息。 public int inSampleSize——图片缩放的倍数。如果设为4,则宽和高都为原来的1/4,则图是原来的1/。 public int outWidth——获取图片的宽度值 public int outHeight——获取图片的高度值 —————————————————————————————— public int inDensity——用于位图的像素压缩比 public int inTargetDensity——用于目标位图的像素压缩比(要生成的位图) public boolean inScaled——设置为true时进行图片压缩,从inDensity到inTargetDensity。

读取一个文件路径得到一个位图。如果指定文件为空或者不能解码成文件,则返回NULL。 public static Bitmap decodeFile(String pathName, Options opts) public static Bitmap decodeFile(String pathName) 读取一个资源文件得到一个位图。如果位图数据不能被解码,或者opts参数只请求大小信息时,则返回NuLL。 (即当Options.inJustDecodeBounds=true,只请求图片的大小信息。) public static Bitmap decodeResource(Resources res, int id) public static Bitmap decodeResource(Resources res, int id, Options opts) 从输入流中解码位图 public static Bitmap decodeStream(InputStream is) 从字节数组中解码生成不可变的位图 public static Bitmap decodeByteArray(byte[] data, int offset, int length)

BitmapDrawable类:继承于Drawable,你可以从文件路径、输入流、XML文件以及Bitmap中创建。 常用的构造函数: Resources res=getResources();//获取资源 public BitmapDrawable(Resources res)——创建一个空的drawable。(Response用来指定初始时所用的像素密度)替代public BitmapDrawable()方法(此方法不处理像素密度) public BitmapDrawable(Resources res, Bitmap bitmap)——Create drawable from a bitmap public BitmapDrawable(Resources res, String filepath)——Create a drawable by opening a given file path and decoding the bitmap. public BitmapDrawable(Resources res, java.io.InputStream is)——Create a drawable by decoding a bitmap from the given input stream.

转: 首先介绍我们最常用的Bitmap(位图)。位图是我们开发中最常用的资源,毕竟一个漂亮的界面对用户是最有吸引力的。按照对位图的操作,分为以下几个功能分别介绍: 从资源中获取位图 获取位图的信息 显示位图 位图缩放 位图旋转 1. 从资源中获取位图 可以使用BitmapDrawable或者BitmapFactory来获取资源中的位图。 当然,首先需要获取资源: Resources res=getResources(); 使用BitmapDrawable获取位图 1. 使用BitmapDrawable (InputStream is)构造一个BitmapDrawable; 2. 使用BitmapDrawable类的getBitmap()获取得到位图; 通过Resource的函数:InputStream openRawResource(int id)获取得到资源文件的数据流后,也可以通过2种方法来获取Bitmap,如下: 使用BitmapDrawable (A Drawable that wraps a bitmap and can be tiled, stretched, or aligned.) 使用BitmapDrawable (InputStream is)构造一个BitmapDrawable; 使用BitmapDrawable类的getBitmap()获取得到位图; BitmapDrawable也提供了显示位图等操作。 Java代码: InputStream is=res.openRawResource(R.drawable.pic); // 读取资源文件获取输入流 BitmapDrawable bmpDraw=new BitmapDrawable(is); Bitmap bmp=bmpDraw.getBitmap(); 或者: BitmapDrawable bmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.pic); Bitmap bmp=bmpDraw.getBitmap();

Android Bitmap总结(android/bitmap.h)

使用BitmapFactory (Creates Bitmap objects from various sources, including files, streams, and byte-arrays.) 使用BitmapFactory类decodeStream(InputStream is)解码位图资源,获取位图。 使用BitmapFactory类Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic); 方法解码位图资源。 BitmapFactory的所有函数都是static,这个辅助类可以通过资源ID、路径、文件、数据流等方式来获取位图。 以上方法在编程的时候可以自由选择,在Android SDK中说明可以支持的图片格式如下:png (preferred), jpg (acceptable), gif (discouraged),虽然bmp格式没有明确说明,但是在Android SDK Support Media Format中是明确说明了。 代码: 方法1:

获取位图的信息 要获取位图信息,比如位图大小、是否包含透明度、颜色格式等,获取得到Bitmap就迎刃而解了,这些信息在Bitmap的函数中可以轻松获取到。Android SDK中对Bitmap有详细说明,阅读起来也比较容易,不在此详细说明,这里只是辅助说明以下2点: 在Bitmap中对RGB颜色格式使用Bitmap.Config定义,仅包括ALPHA_8、ARGB_、ARGB_、RGB_,缺少了一些其他的,比如说RGB_,在开发中可能需要注意这个小问题; Bitmap还提供了compress()接口来压缩图片,不过AndroidSAK只支持PNG、JPG格式的压缩;其他格式的需要Android开发人员自己补充了。

显示位图 显示位图可以使用核心类Canvas,通过Canvas类的drawBirmap()显示位图,或者借助于BitmapDrawable来将Bitmap绘制到Canvas。当然,也可以通过BitmapDrawable将位图显示到View中。 转换为BitmapDrawable对象显示位图 // 获取位图 Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic); // 转换为BitmapDrawable对象 BitmapDrawable bmpDraw=new BitmapDrawable(bmp); // 显示位图 ImageView iv2 = (ImageView)findViewById(R.id.ImageView); iv2.setImageDrawable(bmpDraw); 使用Canvas类显示位图 这儿采用一个继承自View的子类Panel,在子类的OnDraw中显示 Java代码 public class MainActivity extends Activity {

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new Panel(this)); }

class Panel extends View{ public Panel(Context context) { super(context); } public void onDraw(Canvas canvas){ Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic); canvas.drawColor(Color.BLACK); canvas.drawBitmap(bmp, , , null); } } }

位图缩放 (1)将一个位图按照需求重画一遍,画后的位图就是我们需要的了,与位图的显示几乎一样:drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)。 (2)在原有位图的基础上,缩放原位图,创建一个新的位图:CreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) (3)借助Canvas的scale(float sx, float sy) (Preconcat the current matrix with the specified scale.),不过要注意此时整个画布都缩放了。 (4)借助Matrix: Java代码 Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic); Matrix matrix=new Matrix(); matrix.postScale(0.2f, 0.2f); Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(), bmp.getHeight(),matrix,true); canvas.drawColor(Color.BLACK); canvas.drawBitmap(dstbmp, , , null);

位图旋转 同样,位图的旋转也可以借助Matrix或者Canvas来实现。Matrix在线性代数中都学习过,Android SDK提供了Matrix类,可以通过各种接口来设置矩阵。结合上面的例子程序,将位图缩放例子程序在显示位图的时候前,增加位图旋转功能,修改代码如下: Matrix matrix = new Matrix(); //matrix.postScale(0.5f, 0.5f); matrix.setRotate(,,); canvas.drawBitmap(mbmpTest, matrix, mPaint); 旋转后的位图显示如下:

除了这种方法之外,我们也可以在使用Bitmap提供的函数如下: public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter),在原有位图旋转的基础上,创建新位图。 总结说明 对位图的操作,结合Android SDK中的类,详细的介绍完了。最后还需要强调的是:这篇文章只是对Android SDK中代码阅读分析,它代替不了你阅读Android SDK,深入的学习还是要仔细的阅读Android SDK。

转载自:

Scroller的使用 我也不知道怎么就稀里糊涂的看起来AndroidUI方面的东东了,偶尔感到无聊就看不下去了。开个博客记录一下吧,以后忘记了也方便翻一翻。Scroller在androi

Fused Location Provider Inthehighdayoflocation-basedmobiledealsandoffers,thequestionofdefininguser’slocationforspecificservices,collectingstatisticsorkeepingtrackofauser’smovementforspecificpurposesremainsasrelevantaseve

关于新版ADT每次创建项目都出现appcompat_v7的问题 做Android开发的朋友最近会发现,更新ADT至.6.0版本之后,创建新的安装项目,会出现appcompat_v7的内容,而且项目会报错。这到底是怎么回事呢?原来appc

标签: android/bitmap.h

本文链接地址:https://www.jiuchutong.com/biancheng/376663.html 转载请保留说明!

上一篇:Android-局部刷新ListView数据信息的自定义选择列表(android 刷新view)

下一篇:Scroller的使用(scrollcontroller)

  • 特殊性税务处理弥补亏损限额
  • 当月认证发票进项税转出能不能第二个月转出
  • 扶贫入股分红能领多久
  • 超市小票能否入库
  • 完税证明可以重复打印吗
  • 所得税申报表收入
  • 项目奖金个人所得税怎么算
  • 劳务服务公司不含派遣,能接外包吗
  • 收入入账会计分录
  • 个税系统异常怎么申报
  • 代扣代缴完税凭证是什么
  • 红字发票信息表需要盖章吗
  • 企业公益金账务的会计处理怎么做
  • 应发工资包含扣款吗
  • 办公设备维修费会计分录
  • 企业所得税季度申报
  • 物流快递代收货款怎么退
  • 工程领用物资退回会计分录怎么写?
  • 技术服务行业分析
  • 建筑行业会计如何合理避税
  • 为什么要开增值税
  • 银行已扣社保会计分录怎么做
  • 高速公路通行费怎么计算的
  • 如何开具红字增值发票
  • 报刊杂志广告文案写作
  • 专票税率开错了有什么影响
  • 增值税专用发票怎么开
  • 发票作废了还能查验吗
  • 如何查发票真伪发票查询
  • 上月销项税额错了怎么修改凭证?
  • 牛奶的增值税普通发票
  • 税务申报利润表本期金额
  • 外购已税化妆品生产的护肤护发品
  • 文件夹不能打开没有反应
  • 电脑内存条是干什么的?
  • 王者荣耀中哪吒的图片
  • 税务局查账补交所得税怎样处理
  • G40/50-70、SR1000随机Linux改安装Win7系统的实现方法
  • bwkp.exe是什么程序
  • 温哥华瀑布
  • 木质家具出口
  • 增值税退税要准备什么资料
  • vue中computed作用
  • css设置3d
  • fdisk cfdisk
  • 预收账款怎么变成应收
  • 养老保险产生的利息怎么入账
  • 金税盘一定要交费吗
  • 报销员工电话费怎么算
  • javascript 模块
  • wordpress怎么降级
  • dedecms5.7
  • 年数总和法怎么算年折旧额
  • mysql数据库性能优化方法有哪些
  • 企业所得税季度预缴纳税申报表
  • 一台设备分期开票数量
  • 材料合理损耗计入入账价值吗
  • 企业开负数发票退货后如何处理?
  • 国外差旅费如何确定汇率呢
  • 公司办公室装修装修
  • 个人独资企业税收政策2023规定
  • 对公账户转私人账户有限额吗
  • 办税人员可以是开票员吗
  • 对子公司的投资损失可以税前扣除吗
  • 笔记本电脑的触摸板怎么开启和关闭
  • windowsxp如何隐藏文件
  • win7注册表文件位置
  • cocos 2.5d
  • unity3d游戏教程
  • jqgrid设置宽度
  • winrar指令
  • jquery html函数
  • [置顶]星陨计划
  • bud3d跑酷
  • 福建省地方amc
  • 龙华区有几个地方
  • 税务区域代码怎么填
  • 广东省国家税务总局稽查局局长
  • 扶贫绩效目标申请怎么写
  • 龙岗区龙岗税务局地址
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设