使用BitmapFactory (Creates Bitmap objects from various sources, including files, streams, and byte-arrays.)
利用BitmapFactory可以从一个指定文件中,利用decodeFile()解出Bitmap;也可以定义的图片资源中,利用decodeResource()解出Bitmap。
使用BitmapFactory类decodeStream(InputStream is)解码位图资源,获取位图。
使用BitmapFactory类Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180); 方法解码位图资源。
使用Canvas类显示位图
public class Main 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.pic180); canvas.drawColor(Color.BLACK); canvas.drawBitmap(bmp, 10, 10, null); } } }
通过BitmapDrawable显示位图
// 获取位图
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);
// 转换为BitmapDrawable对象
BitmapDrawable bmpDraw=new BitmapDrawable(bmp);
// 显示位图
ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);
iv2.setImageDrawable(bmpDraw);
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-100.0表示最低画质压缩,100以最高画质压缩。对于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/16。
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.
相关推荐
Android Bitmap位图渲染与操作源码.rar
安卓Android源码——(Bitmap位图渲染与操作).zip
Android不支持将Bitmap转换成单色的Bmp图片,所以参考Bmp格式说明,自己写了一个转换类。亲测有效!!!
源码参考,欢迎下载
安卓Android源码——(Bitmap位图渲染与操作).rar
Android应用源码之(Bitmap位图渲染与操作.zip项目安卓应用源码下载Android应用源码之(Bitmap位图渲染与操作.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术...
Bitmap位图的创建和编辑 Bitmap明度,亮度,RGB色相的调整
主要为大家详细介绍了Android实现Bitmap位图旋转效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Transparent Bitmap实现透明的位图(7KB)
将Bitmap转成byte[],进行保存,用到时再取出byte[],转化成Bitmap,进行显示
Android应用源码开发Demo,主要用于毕业设计学习。
Android 应用开发源码 参考与学习使用
Android Bitmap详解及Bitmap的内存优化 一、Bitmap: Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。 常用方法: ...
40M的安卓Demo,就冲这容量,也值得一下。有福利的,亲。
android位图的一些东西,对一些基本的操作的demo。主要是view和bitmap一些东西 比如说倒影之类的东西
Bitmap位图的旋转案例源码,位图的旋转也可以借助Matrix或者Canvas来实现。通过postRotate方法设置旋转角度,然后用createBitmap方法创建一个经过旋转处理的Bitmap对象,最后用drawBitmap方法绘制到屏幕上,于是就...
本文为大家分享了Android bitmap使用细节,供大家参考,具体内容如下 1、计算机表示图形的几种方式 1)BMP :几乎不进行压缩 占用空间比较大 2)JPG : 在BMP的基础上对相邻的像素进行压缩,占用空间比BMP小 3)...
Android中图片是以bitmap形式存在的,那么bitmap所占内存,直接影响到了应用所占内存大小,首先要知道bitmap所占内存大小计算方式: 图片长度 x 图片宽度 x 一个像素点占用的字节数 以下是图片的压缩格式: 其中,A...
android 源码学习. 资料部分来源于合法的互联网渠道收集和整理,供大家学习参考与交流。本人不对所涉及的版权问题或内容负法律责任。如有侵权,请通知本人删除。感谢CSDN官方提供大家交流的平台