创建Bitmap之BitmapFactory
package com.loaderman.customviewdemo; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView; import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView iv = (ImageView) findViewById(R.id.img); new Thread(new Runnable() {
@Override
public void run() {
try {
byte[] data = getImage("https://files-cdn.cnblogs.com/files/loaderman/lp.bmp");
int length = data.length; final Bitmap bitMap = BitmapFactory.decodeByteArray(data, 0, length); iv.post(new Runnable() {
@Override
public void run() {
iv.setImageBitmap(bitMap);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start(); } public static byte[] getImage(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection httpURLconnection = (HttpURLConnection) url.openConnection();
httpURLconnection.setRequestMethod("GET");
httpURLconnection.setReadTimeout(6 * 1000);
InputStream in = null;
if (httpURLconnection.getResponseCode() == 200) {
in = httpURLconnection.getInputStream();
byte[] result = readStream(in);
in.close();
return result; }
return null;
} 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();
return outputStream.toByteArray();
} }
package com.loaderman.customviewdemo; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView; import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView iv = (ImageView) findViewById(R.id.img); new Thread(new Runnable() {
@Override
public void run() {
try {
InputStream inputStream = getImage("https://files-cdn.cnblogs.com/files/loaderman/lp.bmp");
final Bitmap bitMap = BitmapFactory.decodeStream(inputStream); iv.post(new Runnable() {
@Override
public void run() {
iv.setImageBitmap(bitMap);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start(); } public static InputStream getImage(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection httpURLconnection = (HttpURLConnection) url.openConnection();
httpURLconnection.setRequestMethod("GET");
httpURLconnection.setReadTimeout(6 * 1000);
if (httpURLconnection.getResponseCode() == 200) {
return httpURLconnection.getInputStream(); }
return null;
} }
BitmapFactory用于各种资源,文件,数据流和字节数组中创建bitmap位图对象,BitmapFactory是一个工具类,,提供大量的函数,可以解析和创建bitmap对象
public static Bitmap decodeFile(String pathName, BitmapFactory.Options opts) {
throw new RuntimeException("Stub!");
}
public static Bitmap decodeFile(String pathName) {//通过文件路径来加载图片,必须是全路径名
throw new RuntimeException("Stub!");
}
public static Bitmap decodeResourceStream(Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts) {
throw new RuntimeException("Stub!");
}
public static Bitmap decodeResource(Resources res, int id, BitmapFactory.Options opts) {
throw new RuntimeException("Stub!");
}
public static Bitmap decodeResource(Resources res, int id) {//表示从资源中解码一张位图,res一般通过,id为资源id
throw new RuntimeException("Stub!");
}
public static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts) {
throw new RuntimeException("Stub!");
}
public static Bitmap decodeByteArray(byte[] data, int offset, int length) {//根据byte数组解析出bitmap
throw new RuntimeException("Stub!");
}
public static Bitmap decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts) {
throw new RuntimeException("Stub!");
}
public static Bitmap decodeStream(InputStream is) {//一般用于加载网络上获取的图片
throw new RuntimeException("Stub!");
}
public static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, BitmapFactory.Options opts) {
throw new RuntimeException("Stub!");
}
public static Bitmap decodeFileDescriptor(FileDescriptor fd) {//fd包含解码位图数据的文件路径 比 decodeFile更节省内存
throw new RuntimeException("Stub!");
}
创建Bitmap之BitmapFactory的更多相关文章
- android图像与图像处理系列(一、Bitmap和BitmapFactory)
1.Drawable对象 Android应用添加了Drawabe资源之后,Android SDK会为这份资源文件在R清单文件中创建一个索引项:R.drawable.file_name,接着我们可以在x ...
- [Android学习笔记]Bitmap,BitmapDrawable,BitmapFactory学习笔记
Bitmap:图片文件的封装,可以看做是一张位图此类中的静态方法可以通过源Bitmap创建新的Bitmap对象此类封装了位图的一些信息Bitmap文档 BitmapFactory:一个工具类,用于创建 ...
- Android学习之——图形图像处理(Bitmap、BitmapFactory)(一)
转载自http://blog.csdn.net/csxwc/article/details/10345235 Bitmap是Android系统中的图像处理的最重要的类之一.用它可以获取图像文件信息,对 ...
- Android入门——Bitmap和BitmapFactory
我们都知道一个App的成败,首先取决于是否具有优秀的UI,而除了交互功能之外还需要丰富的图片背景和动画去支撑.在开发中我们应用到的图片不仅仅包括.png..gif..9.png..jpg和各种Draw ...
- 创建Bitmap之Bitmap静态方法使用示例
package com.loaderman.customviewdemo; import android.app.Activity; import android.content.Intent; im ...
- 在android创建bitmap避免低记忆法
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...
- 基于安卓高仿how-old.net实现人脸识别估算年龄与性别
前几段微软推出的大数据人脸识别年龄应用how-old.net在微博火了一把,它可以通过照片快速获得照片上人物的年龄,系统会对瞳孔.眼角.鼻子等27个“面部地标点"展开分析,进而得出你的“颜龄 ...
- Android笔记(二十八) Android中图片之简单图片使用
用户界面很大程度上决定了APP是否被用户接收,为了提供友好的界面,就需要在应用中使用图片了,Android提供了丰富的图片处理功能. 简单使用图片 使用Drawable对象 为Android应用增加了 ...
- Bitmap、BitmapDrawable、BitmapFactory、Matrix类之间的关系
1.BitmapFactory是一个工具类 Bitmap实现在android.graphics包中.但是Bitmap类的构造函数是私有的,外面并不能实例化,只能是通过JNI实例化.这必然是 某个辅助类 ...
随机推荐
- 基于fastai的分类网络
classify.fastai Classifier based on fastai 欢迎访问:https://github.com/pprp/classify.fastai/ 字符识别分类器-基于f ...
- MySQL进阶16 - 视图的创建/修改/删除/更新--可更新性的不适用条件
#进阶16 : 视图 /* 含义: 虚拟表,和普通表一样使用;(从5.1开始使用的:)是通过表动态生成的数据 创建语法: create view 视图名 as 查询语句; ---------- 作用: ...
- 六、用DataContractSerialize类序列化XML
一.层次结构 基类:XmlObjectSerializer 派生类: DataContractSerializer NetDataContractSerializer DataContractJson ...
- 使用docker简单启动springboot项目
1.搭建docker环境 需要linux系统必须是centOS7以上 执行一下命令: yum install epel-release –y yum clean all yum list 2.安装 y ...
- vue 数组更新检测注意事项
- 使用itchat进行自动微信聊天
import itchat def we_chat(message): #enableCmdQR=2用于linux中显示二维码,hotReload=True退出程序后暂存登录状态 itchat.aut ...
- 在virtualenv中使用不同的Python版本
来源:百度SEO公司 我有一个目前使用python 2.5.4运行的Debian系统. 我正确安装了virtualenv,一切正常. 我是否可以将virtualenv与其他版本的Python一起使 ...
- BigDecimal计算
货币金额的计算 - Java中的BigDecimal 在<Effective Java>这本书中也提到这个原则,float和double只能用来做科学计算或者是工程计算,在商业计算中我 ...
- php类的定义与实例化方法
php类的定义 类是对某个对象的定义.它包含有关对象动作方式的信息,包括它的名称.方法.属性和事件.实际上它本身并不是对象,因为它不存在于内存中.当引用类的代码运行时,类的一个新的实例,即对象,就在内 ...
- WiredTiger 4.1.0 发布,MongoDB 存储引擎
WiredTiger 4.1.0 发布了.WiredTiger 是一个开源的.高性能.可伸缩的 MongoDB 数据存储引擎. 下面是一些性能比较图表: 该版本暂未提供更新内容. 下载地址: Sour ...