2013-10-20 54 views
0

基本上我有一個網格(存儲爲圖像),像這樣:如何繪製到與手指的ImageView的位圖,並獲得座標,並保存繪製的圖像

A Grid

我需要做的就是畫在這個網格上用我的手指(多筆畫)並顯示並保存這個新的位圖。

此外,繪圖時我需要獲取筆畫的座標,以便我可以從中計算一些數據(整個網格分爲多個區域)。

座標部分很簡單,我使用Rect(),getX()和getY()即可。

int getZoneLocation(int x, int y) 
    { 

     Rect rectangle = new Rect(); 

     amslerView.getGlobalVisibleRect(rectangle); 

     String coords = "Left:%d Top:%d Right:%d Bottom:%d ActionBarHeight:%d StatusBarHeight:%d"; 

     // Zone 1 Rectangle 

     Log.i("Rectangle Coordinates", 
       String.format(coords, rectangle.left, rectangle.top, rectangle.right, rectangle.bottom, getActionBarHeight(), getStatusBarSize())); 

     Rect outerMostRect = new Rect(rectangle); 

     int xOffset = rectangle.width()/10; 
     int yOffset = rectangle.height()/10; 

     Log.i("Rectangle Attribs", "Width: " + xOffset + "Height: " + yOffset); 

     // Zone 2 Rectangle 

     Rect zone2Rectangle = new Rect(outerMostRect.left + xOffset, outerMostRect.top + yOffset, outerMostRect.right - xOffset, outerMostRect.bottom 
       - yOffset); 

     Log.i("Zone 2 Coordinates", "" + zone2Rectangle.left + " " + zone2Rectangle.top + " " + zone2Rectangle.right + " " + zone2Rectangle.bottom); 

     // Zone 3 Rectangle 

     Rect zone3Rectangle = new Rect(zone2Rectangle.left + xOffset, zone2Rectangle.top + yOffset, zone2Rectangle.right - xOffset, 
       zone2Rectangle.bottom - yOffset); 

     // Zone 4 Rectangle 

     Rect zone4Rectangle = new Rect(zone3Rectangle.left + xOffset, zone3Rectangle.top + yOffset, zone3Rectangle.right - xOffset, 
       zone3Rectangle.bottom - yOffset); 

     // Zone 5 Rectangle 
     Rect zone5Rectangle = new Rect(zone4Rectangle.left + xOffset, zone4Rectangle.top + yOffset, zone4Rectangle.right - xOffset, 
       zone4Rectangle.bottom - yOffset); 

     // Check from inside out for point existence 
     if (zone5Rectangle.contains(x, y)) 
     { 
      return 5; 
     } else if (zone4Rectangle.contains(x, y)) 
     { 
      return 4; 
     } else if (zone3Rectangle.contains(x, y)) 
     { 
      return 3; 
     } else if (zone2Rectangle.contains(x, y)) 
     { 
      return 2; 
     } else if (outerMostRect.contains(x, y)) 
     { 
      return 1; 
     } 
     return -1; 
    } 

基本上我所做的就是獲得顯示該網格,然後只需調用此方法來獲取我需要裏面onTouchListener的數據的ImageView的localVisibleRect。

對於我來說,真正的困境是如何實現手指繪製以及如何實現這一點。

到目前爲止,我已經看過SurfaceView,Canvas甚至是GestureOverlayView(我知道這很愚蠢)。

我也看了一下api示例中的FingerPaint演示,但是它繪製了一個空的視圖,並且我真的不知道如何使用ImageView實現此視圖。

任何建議將是無價的。

+0

延伸像FingerPaint的圖是一樣的ImageView的,和ImageView的是並擴大看法你米納斯將只是這樣做 – JRowan

+0

@JRowan建議是最好的方式去做。 – Hav3n

回答

0

可以使用這個類:

import android.view.View.OnTouchListener; 

public class XCanvas extends ImageView implements OnTouchListener 
{    
    private Activity context = null; 
    private Paint paint = null;   

    public XCanvas(Activity context) 
    { 
     super(context); 
     this.context = context; 

     this.paint = new Paint();  

     setFocusable(true); 
     setFocusableInTouchMode(true); 

     this.setOnTouchListener(this);  

     paint.setAntiAlias(true);  
    } 

    @Override 
    protected void onDraw(Canvas g) 
    {        
     super.onDraw(g);  

     //Set background   
     this.paint.setColor(Color.rgb(0xff, 0xff, 0xff));  
     g.drawRect(0, 0, this.getWidth(), this.getHeight(), this.paint); 

     //Paint everything here! 
    }    

    @Override 
    public boolean onTouch(View v, MotionEvent event) 
    { 
     int action = event.getAction() & MotionEvent.ACTION_MASK; 

     switch (action) 
     { 
      case MotionEvent.ACTION_DOWN: 
      { 
       this.pointerPressed(event); 
       break; 
      } 
      case MotionEvent.ACTION_MOVE: 
      { 
       this.pointerDragged(event);   
       break; 
      } 
      case MotionEvent.ACTION_UP: 
      { 
       this.pointerReleased(); 
       break; 
      }     
     } 

     return true; 
    }   

    protected void pointerPressed(MotionEvent event) 
    { 
     //you can save touch point here! 
    } 

    protected void pointerDragged(MotionEvent event) 
    { 
     //for get x ==> eveevent.getX() 
     //for get y ==> eveevent.getY()   

     this.repaint(); 
    } 

    protected void pointerReleased() 
    { 

    }  

    public void repaint() 
    {  
     this.invalidate(); 
    }   
} 

和使用類粘貼在主要活動的代碼:

private XCanvas xCanvas = null; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{  
    super.onCreate(savedInstanceState);              

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
             WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    requestWindowFeature(Window.FEATURE_NO_TITLE);  

    //getWindow().setBackgroundDrawableResource(R.color.MapBackground); 

    this.xCanvas = new XCanvas(this);        
    this.xCanvas.repaint(); 

    setContentView(this.xCanvas); 
    this.xCanvas.requestFocus(); 
} 
相關問題