赞
踩
属性名 | Bitmap | Drawable |
---|---|---|
显示清晰度 | 相同 | 相同 |
占用内存 | 大 | 小 |
支持缩放 | 是 | 是 |
支持色相色差的调整 | 是 | 否 |
支持旋转 | 是 | 是 |
支持透明色 | 是 | 是 |
绘制速度 | 慢 | 快 |
支持像素操作 | 是 | 否 |
1.转换为BitmapDrawable对象使用
//先转换为BitmapDrawable对象, 再将其用作资源背景
Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.picture);
BitmapDrawable bmp = new BitmapDrawable(bitmap);
image.setImagebackground(bmp);
2.当作画布使用
//方式一:使用默认的画布, 这里面View对应一个默认的Bitmap对象,canvas已经默认使用这个bitmap创建出来,最终绘制在这个Bitmap上
public void onDraw(Canvas canvas){
...
canvas.drawXXX();
}
//方式二:自建画布, 有的时候需要一块空白的画布进行加水印等操作,就需要自己创建画布
Bitmap bitmap = Bitmap.createBitmap(200, 100, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.YELLOW);
一张位图所占用的内存:图片长度(px) x 图片宽度(px)x 一个像素点所占的字节数
在Android中,存储一个像素点所占用的字节数是使用枚举类型Bitmap.Config中的各个参数来表示的参数如下:
每个色值所占的位数越大,颜色越艳丽:
public static Bitmap decodeResource(Resource res, int id);
public static Bitmap decodeResource(Resource res, int id, Options opts)
public static Bitmap decodeFile(String pathName);
public static Bitmap decodeFile(String pathName, Options opts);
public static Bitmap decodeByteArray(byte[] data, int offset, int length);
public static Bitmap decodeByteArray(byte[] data, int offset, int length, Options opts);
public static Bitmap decodeFileDescriptor(FileDescriptor fd);
public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts);
public static Bitmap decodeStream(InputStream is);
public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts);
public static Bitmap decodeResourceStream(Resources res, TypeValue value, InputStream is, Rect padding, Options opts);
//从资源中解码一张位图,主要以R.drawable.xxx的形式从本地资源中加载
//res:包含图像数据的资源对象,一般通过Context.getResource()函数获得
id:包含图像数据的资源id
public static Bitmap decodeResource(Resource res, int id);
eg:
Bitmap bitmap = Bitmap.decodeRespurce(getResource(), R.drawable.xxx);
//主要通过文件路径来加载图片
//pathName:解码文件的全路径名,必须是路径全名
public static Bitmap decodeFile(String pathName);
eg:
String filename = "/data/dataa/demo.jpg"
Bitmap bitmap = BitmapFactory.decodeFile(filename);
//根据byte数组 解析出来Bitmap
//data:压缩图像数据的字节数组
//offset:图像数据偏移量,用于解码器定位从哪里开始解析
//length:字节数,从偏移量开始,指定取多少字节进行解析
new Thread(new Runnable(){
@Override
public void run(){
try{
byte[] data = getImage(path);
int length = data.length;
final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, length);
....
}catch(Exception e){
e.printStackTrace();
}
}
}).start()
public static byte[] readStream(InputStream in) throws Exception{ ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; while((len = in,read(buffer)) !=-1){ outputStream.write(buffer, 0, len) } outputStream.close(); in.close; //因为data字节数组是把输入流转换为字节内存输出流的字节数组格式,如果不进行outputStream进行转换,则返回结果会一直为null return outputStreamm.toByteArray(); } public static byte[] getImage(String path) throws Exception{ URL url = new URL(path); HttpURLConnection httpURLconnection = (HttpURLConnection)url.openConnection(); httpURLconnection.setRequestMethod("GET"); httpURLconnection.setReadTimeout(1000); InputStream in = null; if(httURLconnection.getResponseCode() == 200){ in = httpURLconnection.getInputStream(); byte[] result = readStream(in); in.close(); return result; } return null; }
//fd:包含解码位图数据的文件路径
//outPadding:用于返回矩形的内边距,如果bitmap没有解析成功,则返回(-1, -1, -1, -1),如果不需要,可以传入null, 这个参数一般不使用
public static Bitmap decodeFileDescriptor(FileDescriptor fd);
public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, Options opts);
eg:
String path = "/data/data/demo.jpg";
FileInputStream is = new FileInputStream(path);
bitmap = BitmapFactory.decodeFileDescriptor(is.getFD());
if(bitmap==null){
....
}
public static Bitmap decodeFileDescriptor(FIleDescriptor fd, Rect outPadding, Options opts){
Bitmap bitmap = nativeDecodeFileDescriptor(fd, outPadding, opts);
if(bitmap == nnull && opts != null && opts.inBitmap != null){
throw new IllegalArgmentException("...");
}
return finishDecode(bitmap, outPadding,opts);
}
public static Bitmap decodeFile(String pathName, Options opts) { validate(opts); Bitmap bm = null; InputStream stream = null; try { stream = new FileInputStream(pathName); bm = decodeStream(stream, null, opts); } catch (Exception e) { /* do nothing. If the exception happened on open, bm will be null. */ Log.e("BitmapFactory", "Unable to decode stream: " + e); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { // do nothing here } } } return bm; } @Nullable public static Bitmap decodeStream(@Nullable InputStream is, @Nullable Rect outPadding, @Nullable Options opts) { if(!is.markSupported()){ is = new BufferedInputSttream(is, 16 * 1024); } is.mark(1024); Bitmap bmp; byte[] tempStorage = null; if(opts != null){ tempStorage = opts.inTempStorage; } if(tempStorage == null){ tempStorage = new byte[16 * 1024]; } bmp = nativeDecodeStream(is, tempStorage, outPadding, opts); return finishDecode(bmp, outPadding, opts); }
is = new BufferedInnputStream(is, 16 * 1024);
tempStorage = new byte[16 * 1024];
//is:用于解码位图的原始数据输入流
//outPadding:与decodeFileDescriptor中的 outPadding参数含义相同,用于返回矩形的内边距,如果没有解析成功,则返回(-1, -1, -1,-1),这个参数一般不使用,可以传入null
//一般用来从网络上获取图片
public static Bitmap decodeStream(InputStream is);
public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts);
eg:
new Thread(new Runnable(){ @Override public void run(){ try{ InputStream inputStream = getImage(path); final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); .... }catch(Exception e){ e.printStackTrace(); } } }).start() //通过getImmage()函数返回一个InputStream对象 public static byte[] getImage(String path) throws Exception{ URL url = new URL(path); HttpURLConnection httpURLconnection = (HttpURLConnection)url.openConnection(); httpURLconnection.setRequestMethod("GET"); httpURLconnection.setReadTimeout(1000); if(httURLconnection.getResponseCode() == 200){ return httpURLconnection.getInputStream(); } return null; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。