Android ApiDemos樣本解析(66):Graphics->FingerPaint

來源:互聯網
上載者:User

本例FingerPaint實現了使用手指在螢幕上任意塗鴉,支援顏色選擇,多種畫刷模式,並可以擦除部分繪圖。

例子使用Path來通過onTouchEvent事件記錄手指在螢幕上的繪製路線。

[java] 
@Override 
public boolean onTouchEvent(MotionEvent event) { 
 float x = event.getX(); 
 float y = event.getY();  www.2cto.com
  
 switch (event.getAction()) { 
 case MotionEvent.ACTION_DOWN: 
 touch_start(x, y); 
 invalidate(); 
 break; 
 case MotionEvent.ACTION_MOVE: 
 touch_move(x, y); 
 invalidate(); 
 break; 
 case MotionEvent.ACTION_UP: 
 touch_up(); 
 invalidate(); 
 break; 
 } 
 return true; 

@Override
public boolean onTouchEvent(MotionEvent event) {
 float x = event.getX();
 float y = event.getY();
 
 switch (event.getAction()) {
 case MotionEvent.ACTION_DOWN:
 touch_start(x, y);
 invalidate();
 break;
 case MotionEvent.ACTION_MOVE:
 touch_move(x, y);
 invalidate();
 break;
 case MotionEvent.ACTION_UP:
 touch_up();
 invalidate();
 break;
 }
 return true;
}Path類可以通過 moveTo, quadTo, lineTo記錄手指在螢幕上的路徑。

本例還使用了到MaskFilter,MaskFilter 可以對映像的Alaph-Channel 做些變換(邊緣效果),Android包含了下面幾種MaskFilter:

BlurMaskFilter 指定了一個模糊的樣式和半徑來處理Paint的邊緣。
EmbossMaskFilter 指定了光源的方向和環境光線強度來添加浮雕效果。
其中 EmbossMaskFilter emboss=new EmbossMaskFilter(direction,light,specular,blur);

direction 定義了光源的方向,light 定義光的亮度, specular定義光的等級,blur模糊度。

實現擦除使用PorterDuff 的Clear 模式,可以參見Android ApiDemos樣本解析(59):Graphics->ColorFilters。

[java] 
mPaint.setXfermode(new PorterDuffXfermode( 
 PorterDuff.Mode.CLEAR)); 

mPaint.setXfermode(new PorterDuffXfermode(
 PorterDuff.Mode.CLEAR));ColorPickerDialog 為顏色選取對話方塊,選擇顏色的結果是通過實現ColorPickerDialog.OnColorChangedListener 回呼函數來擷取:

[java] view plaincopyprint?
public class FingerPaint extends GraphicsActivity 
 implements ColorPickerDialog.OnColorChangedListener { 
  
 .... 
 public void colorChanged(int color) { 
 mPaint.setColor(color); 
 } 
  
  
 new ColorPickerDialog(this, this, mPaint.getColor()).show(); 
  
 ... 

public class FingerPaint extends GraphicsActivity
 implements ColorPickerDialog.OnColorChangedListener {
 
 ....
 public void colorChanged(int color) {
 mPaint.setColor(color);
 }
 
 
 new ColorPickerDialog(this, this, mPaint.getColor()).show();
 
 ...
}
 

 

在手機上運行效果如:

 

 
作者:mapdigit
 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.