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. javascript数据判断是否有指定元素

    jquery方法: $.inArray(dataKey, dataArray) == -1 javascript方法: indexOf() 功能:根据指定的数据,从左向右,查询在数组中出现的位置,如果 ...

  2. 【开200数组解决二叉搜索树的建立、遍历】PAT-L3-016. 二叉搜索树的结构——不用链表来搞定二叉搜索树

    L3-016. 二叉搜索树的结构 二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值:若它的右子树不空,则右子树上所有结点的值均大于它 ...

  3. BZOJ1101 [POI2007]Zap 和 CF451E Devu and Flowers

    Zap FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d.作为FGD的同学,FGD希望得到 ...

  4. web-----chrome DevTools工具的常用使用记录

     注:1.Chrome浏览器,2.在浏览器页面 右键--->检查,即可看到此页面 跟开发沟通过.他们查看一个页面的性能一般都会使用”检查“来观测.查找对应的数据记录. 设置网络的网速和通过设置某 ...

  5. Mybatis之foreach批量插入

    1接口 public boolean insertMembersBatch(@Param("memberList") List<Members> members); @ ...

  6. Django中的日期和时间格式 DateTimeField

    创建django的model时,有DateTimeField.DateField和TimeField三种类型可以用来创建日期字段,其值分别对应着datetime().date().time()三中对象 ...

  7. jquery手机端产品列表响应式宽高检测宽度赋值给高度让宽高相同

    在手机端浏览商品列表,开发人员懂得设计图片做正好的尺寸,那样浏览很好,但有人天生泛懒图片都是随手上传不管大小合适不合适,要求还是响应式的.今天就用jquery检测宽度,并赋值给高度来实现错位问题 &l ...

  8. php数据类型之查看和判断数据类型

    我们知道了一个数据的类型,才能进行下一步操作.后面的时候,大家可以学习到更多的知识——自定义功能(函数). 我们来做一个场景模拟:(注:眼前不用会写这个函数,以后会教大家) 假设,我们可以写一个智能的 ...

  9. mysql修改default值

    ALTER TABLE xxxxx   ALTER COLUMN  xxxxx   SET DEFAULT '0';

  10. 12 | 为什么我的MySQL会“抖”一下?

    平时的工作中,不知道你有没有遇到过这样的场景,一条SQL语句,正常执行的时候特别快,但是有时也不知道怎么回事,它就会变得特别慢,并且这样的场景很难复现,它不只随机,而且持续时间还很短. 看上去,这就像 ...