Android View之Canvas

2018-04-26  本文已影响22人  Jowney

Canvas官方介绍

The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).

//假设srcBitmap已经创建      
       Bitmap destBitmap = Bitmap.createBitmap(srcBitmap.getWidth, srcBitmap.getHeight, srcBitmap.getConfig);//创建一个空白的Bitmap
       Canvas canvas = new Canvas(destBitmap );//创建Canvas并绑定destBitmap
       canvas.drawBitmap(srcBitmap, ,0,0, new Paint());
       imageView.setImageBitmap(afterBitmap);
    

2.自定义View

@Override  
    OnDraw(Canvas canvas){  
        //利用canvas执行绘图操作  
    }  

3.SurfaceView:优点可以在子线程中进行绘制

    SurfaceHoler holder = surfaceView.getHolder();  
    Canvas canvas = holder.lockCanvas();  
    //利用canvas进行绘图操作  
      
    holder.unlockCanvasAndPost(canvas);//对canvas解锁,并更新  
上一篇 下一篇

猜你喜欢

热点阅读