zoukankan      html  css  js  c++  java
  • Android Bitmap开发之旅--基本操作

    1 Bitmap加载方式

    在介绍Bitmap--OOM 异常时,首先介绍一下Bitmap有哪几种加载方式。通常Bitmap的加载方式有Resource资源加载、本地(SDcard)加载、网络加载等加载方式。

    1.1 Resource资源加载

    1. Assets资源加载方式:
      [java] view plain copy
       
      1. AssetManager am = getAssets();  
      2. InputStream is = am.open("high_pixel_img.jpg");  
      3. Bitmap bitmap = BitmapFactory.decodeStream(is);  
    2.  Res资源加载方式:
      [java] view plain copy
       
      1. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.high_pixel_img);  

    1.2 本地(SDcard)加载

    [java] view plain copy
     
    1. String file_name = Environment.getExternalStorageDirectory().toString()+"/"+"high_pixel_img.jpg";  
    2. nbsp;Bitmap bitmap = BitmapFactory.decodeFile(file_name);  

    文件描述符

    1.3 网络加载

    注意:网络加载图片的时候必须在非主线成中操作

    [java] view plain copy
     
    1. String website = "http://www.baidu.com/img/baidu_sylogo1.gif";  
    2. URL image_url = new URL(website);  
    3. HttpURLConnection conn = (HttpURLConnection) image_url.openConnection();  
    4. conn.connect();  
    5. InputStream is = conn.getInputStream();  
    6. bitmap = BitmapFactory.decodeStream(is);  

    2 Bitmap | Drawable | InputStream | Byte[ ] 之间进行转换

    2.1 Drawable转化成Bitmap

    [java] view plain copy
     
    1. Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);  
    2. Bitmap bitmap = Bitmap.createBitmap(  
    3.                                drawable.getIntrinsicWidth(),  
    4.                                drawable.getIntrinsicHeight(),  
    5.                                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
    6.                                                : Bitmap.Config.RGB_565);  
    7. Canvas canvas = new Canvas(bitmap);  
    8. //canvas.setBitmap(bitmap);  
    9. drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
    10. drawable.draw(canvas);  

    2. 2 Bitmap转换成Drawable

    [java] view plain copy
     
    1. Drawable drawable = new BitmapDrawable(bitmap);  

    2.3 Bitmap转换成byte[]

    [java] view plain copy
     
    1. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.high_pixel_img);  
    2. ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    3. bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);  
    4. byte[] info = baos.toByteArray();  

    2.4 byte[]转换成Bitmap

    [java] view plain copy
     
    1. Bitmap bitmap = BitmapFactory.decodeByteArray(byte, 0, b.length);  

    2.5 InputStream转换成Bitmap

    [java] view plain copy
     
    1. InputStream is  = getResources().openRawResource(id);  
    2. Bitmap bitmap = BitmaoFactory.decodeStream(is);  

    2.6 InputStream转换成byte[]

    [java] view plain copy
     
    1. InputStream is = getResources().openRawResource(id);//也可以通过其他方式接收一个InputStream对象  
    2. ByteArrayOutputStream baos = new ByteArrayOutputStream();    
    3. byte[] b = new byte[1024*2];    
    4. int len = 0;    
    5. while ((len = is.read(b, 0, b.length)) != -1)    
    6. {    
    7.    baos.write(b, 0, len);    
    8.    baos.flush();    
    9. }    
    10. byte[] bytes = baos.toByteArray();    

    3 转换Bitmap大小

    [java] view plain copy
     
    1. Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.high_pixel_img);  
    2. Bitmap target = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);  
    3. Canvas canvas = new Canvas(tartget);  
    4. canvas.scale(scale,scale);  
    5. Paint paint = new Pain(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);  
    6. canvas.drawBitmap(bitmap,0,0,paint);  
    7. bitmap.recycle();  

    4 将Bitmap保存为本地文件

    [java] view plain copy
     
    1. String filename = "save.jpg";  
    2. File file = new File(Environmnet.getExternalStorageDirectory,filename);  
    3. try{  
    4.     OutputStream os  =  new FileOutputString(file);  
    5.     bitmap.compress(CompressFormat.JPEG,100,os);  
    6. }catch(FileNotFoundException e){  
    7.    e.printStackTrace();  
    8. }  


    以上是关于Bitmap的相关操作,如果大家在阅读中发现有什么问题,请在评论中留言。

  • 相关阅读:
    grep 精确匹配
    @ARGV
    同时查看多个文件
    R画双y轴图
    R画饼图
    linux的sort排序
    $0
    QQ图
    Config::Std模块安装
    R语言做正态分布检验
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/5544986.html
Copyright © 2011-2022 走看看