现在的位置: 首页 > 综合 > 正文

andriod图片的加载、处理

2013年08月25日 ⁄ 综合 ⁄ 共 5391字 ⁄ 字号 评论关闭

Bitmap 相关 

1. Bitmap比较特别 因为其不可创建 而只能借助于BitmapFactory 而根据图像来源又可分以下几种情况: 

* png图片 如:R.drawable.tianjin 

Java代码 

  1. Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.tianjin);   


* 图像文件 如: /sdcard/dcim/tianjin.jpeg 

Java代码 

  1. Bitmap bmp = BitmapFactory.decodeFile("/sdcard/dcoim/tianjin.jpeg")    


2. Bitmap 相关应用 

- 本地保存 即 把 Bitmap 保存在sdcard中 
* 创建目标文件的File 
Java代码

  1. File fImage = new File("/sdcard/dcim","beijing.jpeg");     
  2.     
  3. FileOutputStream iStream = new FileOutputStream(fImage);   

  

* 取出Bitmap oriBmp 
Java代码 

  1. oriBmp.compress(CompressFormat.JPEG, 100, iStream);   



参照Bitmap 的API方法 compress(Bitmap.CompressFormat format, int quality, OutputStream stream) 
Write a compressed version of the bitmap to the specified outputstream. 
写到输出流里,就保存到文件了。 


可以保存为几种格式:png,gif等: 

Java代码
  1. public void saveMyBitmap(String bitName) throws IOException {  
  2.                         File f = new File("/sdcard/Note/" + bitName + ".png");  
  3.                         f.createNewFile();  
  4.                         FileOutputStream fOut = null;  
  5.                         try {  
  6.                                 fOut = new FileOutputStream(f);  
  7.                         } catch (FileNotFoundException e) {  
  8.                                 e.printStackTrace();  
  9.                         }  
  10.                         mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);  
  11.                         try {  
  12.                                 fOut.flush();  
  13.                         } catch (IOException e) {  
  14.                                 e.printStackTrace();  
  15.                         }  
  16.                         try {  
  17.                                 fOut.close();  
  18.                         } catch (IOException e) {  
  19.                                 e.printStackTrace();  
  20.                         }  
  21.                 }  


- 得到网路图片 * 定义网络图片对应的BufferedInputStream 

Java代码 

  1. String icoURI = "http://202.140.96.134:8080/FS-RSS/img/RN.png";     
  2.     
  3. URL imgURL = new URL(iu);     
  4. URLConnection conn = imgURL.openConnection();     
  5.                  
  6. conn.connect();     
  7. InputStream is = conn.getInputStream();     
  8.                  
  9. BufferedInputStream bis = new BufferedInputStream(is);    


* 下载

  1. Bitmap bmp = BitmapFactory.decodeStream(bis);  

  
* 关闭Stream 

  1. bis.close();     
  2. is.close();    



位图是我们开发中最常用的资源,毕竟一个漂亮的界面对用户是最有吸引力的。 
1. 从资源中获取位图 
可以使用BitmapDrawable或者BitmapFactory来获取资源中的位图。 
当然,首先需要获取资源: 
Resources res=getResources(); 
使用BitmapDrawable获取位图 
   1. 
      使用BitmapDrawable (InputStream is)构造一个BitmapDrawable; 
   2. 
      使用BitmapDrawable类的getBitmap()获取得到位图; 

// 读取InputStream并得到位图 

  1. InputStream is=res.openRawResource(R.drawable.pic180);  
  2. BitmapDrawable bmpDraw=new BitmapDrawable(is);  
  3. Bitmap bmp=bmpDraw.getBitmap();  


或者采用下面的方式: 

  1. BitmapDrawable bmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.pic180);  
  2. Bitmap bmp=bmpDraw.getBitmap();  


使用BitmapFactory获取位图 
(Creates Bitmap objects from various sources, including files, streams, and byte-arrays.) 

使用BitmapFactory类decodeStream(InputStream is)解码位图资源,获取位图。 
     Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180); 
BitmapFactory的所有函数都是static,这个辅助类可以通过资源ID、路径、文件、数据流等方式来获取位图。 

以上方法在编程的时候可以自由选择,在Android SDK中说明可以支持的图片格式如下:png (preferred), jpg (acceptable), gif (discouraged),和bmp(Android SDK Support Media Format)。 
2. 获取位图的信息 
要获取位图信息,比如位图大小、像素、density、透明度、颜色格式等,获取得到Bitmap就迎刃而解了,这些信息在Bitmap的手册中,这里只是辅助说明以下2点: 
      在Bitmap中对RGB颜色格式使用Bitmap.Config定义,仅包括ALPHA_8、ARGB_4444、ARGB_8888、RGB_565,缺少了一些其他的,比如说RGB_555,在开发中可能需要注意这个小问题; 
      Bitmap还提供了compress()接口来压缩图片,不过AndroidSAK只支持PNG、JPG格式的压缩;其他格式的需要Android开发人员自己补充了。 

3. 显示位图 
显示位图可以使用核心类Canvas,通过Canvas类的drawBirmap()显示位图,或者借助于BitmapDrawable来将Bitmap绘制到Canvas。当然,也可以通过BitmapDrawable将位图显示到View中。 

转换为BitmapDrawable对象显示位图 

Java代码  收藏代码
  1.  // 获取位图  
  2.  Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);  
  3.  // 转换为BitmapDrawable对象  
  4.  BitmapDrawable bmpDraw=new BitmapDrawable(bmp);  
  5.  // 显示位图  
  6.  ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);  
  7. iv2.setImageDrawable(bmpDraw);  


使用Canvas类显示位图 
这儿采用一个继承自View的子类Panel,在子类的OnDraw中显示 

  1. public class MainActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(new Panel(this));  
  7.     }  
  8.       
  9.     class Panel extends View{            
  10.         public Panel(Context context) {   
  11.             super(context);  
  12.         }       
  13.         public void onDraw(Canvas canvas){   
  14.             Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);   
  15.             canvas.drawColor(Color.BLACK);   
  16.             canvas.drawBitmap(bmp, 1010null);   
  17.         }   
  18.     }  
  19. }  



4. 位图缩放 
(1)将一个位图按照需求重画一遍,画后的位图就是我们需要的了,与位图的显示几乎一样:drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)。 
(2)在原有位图的基础上,缩放原位图,创建一个新的位图:CreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) 
(3)借助Canvas的scale(float sx, float sy) (Preconcat the current matrix with the specified scale.),不过要注意此时整个画布都缩放了。 
(4)借助Matrix: 
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180); 
            Matrix matrix=new Matrix(); 
            matrix.postScale(0.2f, 0.2f); 
            Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(), 
            bmp.getHeight(),matrix,true); 
            canvas.drawColor(Color.BLACK); 
            canvas.drawBitmap(dstbmp, 10, 10, null); 
5. 位图旋转 
同样,位图的旋转也可以借助Matrix或者Canvas来实现。 
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180); 
            Matrix matrix=new Matrix(); 
  

抱歉!评论已关闭.