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的更多相关文章

  1. android图像与图像处理系列(一、Bitmap和BitmapFactory)

    1.Drawable对象 Android应用添加了Drawabe资源之后,Android SDK会为这份资源文件在R清单文件中创建一个索引项:R.drawable.file_name,接着我们可以在x ...

  2. [Android学习笔记]Bitmap,BitmapDrawable,BitmapFactory学习笔记

    Bitmap:图片文件的封装,可以看做是一张位图此类中的静态方法可以通过源Bitmap创建新的Bitmap对象此类封装了位图的一些信息Bitmap文档 BitmapFactory:一个工具类,用于创建 ...

  3. Android学习之——图形图像处理(Bitmap、BitmapFactory)(一)

    转载自http://blog.csdn.net/csxwc/article/details/10345235 Bitmap是Android系统中的图像处理的最重要的类之一.用它可以获取图像文件信息,对 ...

  4. Android入门——Bitmap和BitmapFactory

    我们都知道一个App的成败,首先取决于是否具有优秀的UI,而除了交互功能之外还需要丰富的图片背景和动画去支撑.在开发中我们应用到的图片不仅仅包括.png..gif..9.png..jpg和各种Draw ...

  5. 创建Bitmap之Bitmap静态方法使用示例

    package com.loaderman.customviewdemo; import android.app.Activity; import android.content.Intent; im ...

  6. 在android创建bitmap避免低记忆法

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...

  7. 基于安卓高仿how-old.net实现人脸识别估算年龄与性别

    前几段微软推出的大数据人脸识别年龄应用how-old.net在微博火了一把,它可以通过照片快速获得照片上人物的年龄,系统会对瞳孔.眼角.鼻子等27个“面部地标点"展开分析,进而得出你的“颜龄 ...

  8. Android笔记(二十八) Android中图片之简单图片使用

    用户界面很大程度上决定了APP是否被用户接收,为了提供友好的界面,就需要在应用中使用图片了,Android提供了丰富的图片处理功能. 简单使用图片 使用Drawable对象 为Android应用增加了 ...

  9. Bitmap、BitmapDrawable、BitmapFactory、Matrix类之间的关系

    1.BitmapFactory是一个工具类 Bitmap实现在android.graphics包中.但是Bitmap类的构造函数是私有的,外面并不能实例化,只能是通过JNI实例化.这必然是 某个辅助类 ...

随机推荐

  1. 虚拟机更改MAC

    有两种方式修改MAC地址 方法一 现实中网卡一出厂就有MAC地址,虚拟机的MAC地址见下图,这个就相当于出厂MAC.在这里修改MAC相当于直接修改硬件MAC 方法二 这里修改MAC,硬件MAC并没有变 ...

  2. django工作原理简介

    django工作原理简介 先简单的介绍一下django的工作原理,其中还会涉及到Middleware(中间件,包括request, view, exception, response),URLConf ...

  3. FastDateFormat日期工具类

    原文:https://blog.csdn.net/u013823538/article/details/80197476 public class DateUtil { public static f ...

  4. Go语言中的数据格式(json、xml 、msgpack、protobuf)

    在分布式的系统中,因为涉及到数据的传输,所以一定会进行数据的交换,此时就要定义数据交换的格式,例如二进制.Json.Xml等等.本篇文章就是总结一下常用的几种数据格式. 一.Json格式 如果想使用J ...

  5. 缺包与maven

    一. 缺包 1. 打开pom.xml 将依赖加入. 2. mvn clean install -DskipTests 3. scp target/lib/****.jar(刚刚的依赖的架包) 服务器地 ...

  6. markdown demo 学习

    ## <center>2019-05-08 12:05 DDoS攻击检测报告</center> ## - **MME ID**: 1123424 - **DDoS攻击类型**: ...

  7. div 水平垂直居中

    css <style> .main{ background: #999999; width: 600px; height: 400px; position: absolute; top: ...

  8. [go] 循环与函数

    练习:循环与函数 为了练习函数与循环,我们来实现一个平方根函数:用牛顿法实现平方根函数. 计算机通常使用循环来计算 x 的平方根.从某个猜测的值 z 开始,我们可以根据 z² 与 x 的近似度来调整 ...

  9. 学到了林海峰,武沛齐讲的Day30 完 TCP UDP

    TCP UDP 其中讲了数据的传输.各有利弊 个人理解 就是这样将高并发,低数据,高数据的传输,稳定高效

  10. zabbix数据的时序-

    gj的proxy服务器经过重启之后时序有变化. zabbix数据库中数据的存储是以哪方为准server端还是agent端, 触发事件跟恢复时间反了,本应该恢复的事件在数据库中查询event,得到的事件 ...