创建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实例化.这必然是 某个辅助类 ...
随机推荐
- 二叉树遍历(非递归版)——python
二叉树的遍历分为广度优先遍历和深度优先遍历 广度优先遍历(breadth first traversal):又称层次遍历,从树的根节点(root)开始,从上到下从从左到右遍历整个树的节点. 深度优先遍 ...
- Chkdsk /f 修复无法识别EXFAT卷文件系统
Chkdsk 工具将错误地报告为损坏的 Windows 7 和 Windows Server 2008 R2 中 exFAT 卷文件系统 适用于: Windows Server 2008 R2 Ser ...
- PAT_B 20
这道题就是简单的贪心,有几个注意点: 1.又是一个单体有多个属性(即月饼有总量,总价,单价几个属性),这个时候可以考虑使用struct结构,我还是傻傻使用pair,还是没有养成习惯,幸好这道题存下来两 ...
- Maven配置环境变量
Windows: 1:新建系统M2_HOME变量,并把安装maven路径拷贝上去 2:配置path变量,并把maven路径拷贝上去,这次的maven路径到bin 3:测试maven环境是否配置 ...
- 使用SpringTask 进行Java定时任务开发
(我这里的案例 是模拟 将项目包放到tomcat里面运行 ) 新建一个Java Web的Maven项目....... 此过程省略... 项目结构如图: 1.pom.xml 配置 <?xml ve ...
- Spring Boot学习--spring-boot-starter-parent及starters(转)
在官方文档的第三部分的13块讲述了引用的管理,官方推荐的是使用Maven和Gradle. 我一直在用的是maven,而且使用maven有些优势–spring-boot-starter-parent,这 ...
- 到spring官网创建第一个springboot工程
登录到spring的官网,直接生成一个,然后倒入本地工程就可以了. https://start.spring.io/ 点击创建的时候. 就等于下载了这个工程. 下载后,倒入到我们的maven工程可以直 ...
- sql server select 1 from 的作用
select 1 from table 语句中的1代表什么意思 在这里我主要讨论的有以下几个select 语句: doo_archive表是一个数据表,表的行数为4行,如下: 分别用三条selec ...
- 51nod P1354 选数字 题解
每日一题 day8 打卡 Analysis 背包+离散化 这题是我们一次模拟赛的T2,结果我的暴力全TLE了. 关键是如果将两个因数的乘积离散化在因数数组中之后等于这个乘积本身,说明a[j]*in离散 ...
- 011_Python3 集合
集合(set)是一个无序的不重复元素序列. 可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典. 创 ...