• 创建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!");
        }
  • 相关阅读:
    Visual Studio ------ 多项目启动
    windows ------ 系统激活 win10
    Java ----- 递归与迭代
    SpringCloud:初识(微服务与SpringCloud)
    The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.
    Solr:SolrJ(增删改查)
    Solr:后台管理界面的使用(管理索引库、导入数据库数据、查询)
    Solr:Slor初识(概述、Windows版本的安装、添加IK分词器)
    Vue:v-model的使用
    Vue:循环遍历(v-for)
  • 原文地址:https://www.cnblogs.com/loaderman/p/10231410.html
Copyright © 2020-2023  润新知