创建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实例化.这必然是 某个辅助类 ...
随机推荐
- JDBC课程4--使用PreparedStatement进行增删查改--封装进JDBCTools的功能中;模拟SQL注入 ; sql的date()传入参数值格式!
主要内容: /*SQL 的date()需要传入参数值: preparedStatement().setDate(new java.util.Date().getTime()); 熟悉了使用Prepar ...
- REST is not the Best for Micro-Services GRPC and Docker makes a compelling case
原文:https://hackernoon.com/rest-in-peace-grpc-for-micro-service-and-grpc-for-the-web-a-how-to-908cc05 ...
- 1128 聚合查询 orm字段及属性
目录 一 聚合查询 1. 级联 级联删除 级联更新 2. 聚合函数 使用 aggregate 使用场景 3. 分组查询 语法 使用 annotate 代码 4. F与Q查询 F查询 Q查询 二 ORM ...
- 用于异步事件驱动的 P 语言 P Language
微软最近开源了P语言,致力于在Linux.macOS和Windows上编写安全的异步事件驱动程序. 微软将P描述为一种领域特定语言,对异步系统的组件间通信进行建模,例如嵌入式.网络或分布式系统.P程序 ...
- CodeForces 840C - On the Bench | Codeforces Round #429 (Div. 1)
思路来自FXXL中的某个链接 /* CodeForces 840C - On the Bench [ DP ] | Codeforces Round #429 (Div. 1) 题意: 给出一个数组, ...
- PHP yield占用内存测试
function com($start) { $tmp = []; for($i=0; $i<300000; $i++){ $tmp[] = $i; } $end = memory_get_us ...
- hive表的DDL
查看表 hive> show tables;创建表 hive> create table t1(id int);查看表结构 ...
- MVC 元数据验证
ASP.NET MVC 3 Validation - 正则表达式验证RegularExpressionAttribute之日期验证 http://blog.csdn.net/jackvs/articl ...
- Jquery Datepicker DateTimepicker
1.Datepicker http://jqueryui.com/datepicker/ 2.DateTimepicker jquery-ui-timepicker-addon.js http://w ...
- php文件上传下载组件
核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...