Android bitmap和canvas小记

1.从资源中获取位图(Bitmap)

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

  当然,首先需要获取资源:Resources res=getResources();

  使用BitmapDrawable获取位图

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

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

// 读取InputStream并得到位图
InputStream is=res.openRawResource(R.drawable.pic180);
BitmapDrawable bmpDraw=new BitmapDrawable(is);
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的所有函数都是static,这个辅助类可以通过资源ID、路径、文件、数据流等方式来获取位图。

  在Android SDK中说明可以支持的图片格式如下:png (preferred), jpg (acceptable), gif (discouraged),和bmp(Android SDK Support Media Format)。

2.获取位图的信息

  获取到Bitmap后通过bitmap类的接口 可以获取到位图的位图大小、像素、density、透明度、颜色格式等信息  

  • 在Bitmap中对RGB颜色格式使用Bitmap.Config定义,仅包括ALPHA_8、ARGB_4444、ARGB_8888、RGB_565,缺少了一些其他的,比如说RGB_555,在开发中可能需要注意这个小问题;
  • Bitmap还提供了compress()接口来压缩图片,不过AndroidSAK只支持PNG、JPG格式的压缩;其他格式的需要Android开发人员自己补充了。

3.显示位图

  显示位图有三种方法可以实现

  (1)可以使用核心类Canvas,通过Canvas类的drawBirmap()显示位图;

  (2)借助于BitmapDrawable来将Bitmap绘制到Canvas;

  (3)通过BitmapDrawable将位图显示到View中。

  转换为BitmapDrawable对象显示位图  

         // 获取位图
        Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);
        // 转换为BitmapDrawable对象
        BitmapDrawable bmpDraw=new BitmapDrawable(bmp);
        // 显示位图
        ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);
       iv2.setImageDrawable(bmpDraw);        

  使用Canvas类显示位图

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

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new Panel(this));
    }
    
    class Panel extends View{           
        public Panel(Context context) {  
            super(context); 
        }      
        public void onDraw(Canvas canvas){  
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);  
            canvas.drawColor(Color.BLACK);  
            canvas.drawBitmap(bmp, 10, 10, null);  
        }  
    } 
}

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();
matrix.postScale(0.8f, 0.8f);
matrix.postRotate(45);
Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(),
bmp.getHeight(),matrix,true);
canvas.drawColor(Color.BLACK); 
canvas.drawBitmap(dstbmp, 10, 10, null); 

6.Canvas的save和restore

  onDraw方法会传入一个Canvas对象,它是你用来绘制控件视觉界面的画布。

  在onDraw方法里,我们经常会看到调用save和restore方法,它们到底是干什么用的呢?

  ? save:用来保存Canvas的状态。save之后,可以调用Canvas的平移、放缩、旋转、错切、裁剪等操作。

  ? restore:用来恢复Canvas之前保存的状态。防止save后对Canvas执行的操作对后续的绘制有影响。

  save和restore要配对使用(restore可以比save少,但不能多),如果restore调用次数比save多,会引发Error。save和restore之间,往往夹杂的是对Canvas的特殊操作。

原文链接:

http://www.cnblogs.com/feisky/archive/2010/01/10/1643460.html

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。