创建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!"); }
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!