android與j2me移植之clipRect

j2me移動到android時,你可能須要用到clipRect這個方法,咱們用它來截取必定區域,讓來看看j2me和android有什麼不一樣吧java

 

Android code:android

  1. canvas.save();//保存當前狀態   
  2. canvas.clipRect(x,y, x+width, y+height)   
  3. canvas.restore();//釋放當前狀態  

 

J2ME code:canvas

  1. int clipX = g.getClipX();   
  2. int clipY = g.getClipY();   
  3. int clipWidth = g.getClipWidth();   
  4. int clipHeight = g.getClipHeight();   
  5. g.clipRect(x, y, width, height);   
  6. g.setClip(clipX, clipY, clipWidth, clipHeight);//釋放當前狀態  

 

比較兩段代碼,Android比較簡潔,要注意的是canvas.clipRect(left, top, right, bottom),它們的座標爲left, top, right, bottom,因此要加上x,y,這一點在剛開始開發j2me轉android時會發現有些區別,也容易忽略,像fillRect,drawRect等方法也和j2me有區別!spa

 

j2me需用setClip釋放當前狀態,而android沒必要!rest

相關文章
相關標籤/搜索