位图Bitmap学习1

    技术2022-05-18  11

    Android画图学习—Bitmap

    位图是我们开发中最常用的资源,毕竟一个漂亮的界面对用户是最有吸引力的。

    1. 从资源中获取位图 

     可以使用BitmapDrawable或者BitmapFactory来获取资源中的位图。

     //首先需要获取资源: 

     Resources res=getResources(); 

     //使用BitmapDrawable (InputStream is)构造一个BitmapDrawable; 

     //读取InputStream并得到位图   

     InputStream is=res.openRawResource(R.drawable.pic180); 

     BitmapDrawable bmpDraw=new BitmapDrawable(is); 

     //使用BitmapDrawable类的getBitmap()获取得到位图; 

     Bitmap bmp=bmpDraw.getBitmap();

     

     或者采用下面的方式:   

     BitmapDrawable bmpDraw=(BitmapDrawable)res.getDrawable(R.drawable .pic180); 

     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类compress(Bitmap.CompressFormat.PNG, 100, outputStream);压缩位图资源,获取位图

     

    Bitmap还提供了compress()接口来压缩图片,不过AndroidSAK只支持PNG、JPG格式的压缩


    最新回复(0)