2017-02-15 4 views
0

je pouvais programmer une application pour dessiner, mais j'ai un problèmecommencer la peinture du cercle Android

Je veux commencer à dessiner à partir du cercle, mon point de départ sera toujours le cercle, comment pourrais-je faire cela?

Maintenant, je peux tirer de tout point, j'ai essayé de trouver le milieu de l'écran, mais cela n'a pas fonctionné

enter image description here

public CanvasView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.context = context; 
    path = new Path(); 
    paint = new Paint(); 
    paint.setColor(Color.RED); 
    paint.setAntiAlias(true); 
    paint.setStyle(Paint.Style.STROKE); 
    paint.setStrokeJoin(Paint.Join.ROUND); 
    paint.setStrokeWidth(20f); 
} 

@Override 
protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
    super.onSizeChanged(w, h, oldw, oldh); 
    mbitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888); 
    mcanvas = new Canvas(mbitmap); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics(); 
    int a = metrics.widthPixels; // to get width of the screen 
    int b = metrics.heightPixels; 

    canvas.drawCircle(a/2,((b/2)-160),50, paint); 
    if ((canDraw)) { 
     canvas.drawPath(path, paint); 
    } 
} 

private void startTouch (float x , float y){ 
    if ((canDraw)) { 
    path.moveTo(x,y); 
    mX = x; 
    mY = y ; 
}} 
public void moveTouche (float x,float y) { 
    if ((canDraw)) { 
    float dx = Math.abs(x - mX); 
    float dy = Math.abs(y - mY); 
    if(dx >= Tolerance || dy >= Tolerance){ 
     path.quadTo(mX,mY,(x+mX)/2,(y+mY)/2); 
     mX = x ; 
     mY = y; 

    }} 
} 
private void upTouch(){ 
    if ((canDraw)) { 
    path.lineTo(mX,mY); 
}} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    float x = event.getX(); 
    float y = event.getY(); 
    switch (event.getAction()){ 
     case MotionEvent.ACTION_DOWN: 
      startTouch(x,y); 
      invalidate(); 
      break ; 
     case MotionEvent.ACTION_UP: 
      upTouch(); 
      invalidate(); 
      break ; 
     case MotionEvent.ACTION_MOVE: 
      moveTouche(x,y); 
      invalidate(); 
      break; 
    } 
    return true ; 
} 
}  

Répondre

0
  • trouver les coordonnées qui délimitent votre région centrale à partir de laquelle les touches doivent commencer, ou créer un élément qui l'identifie
  • à l'intérieur ouTouchEvent, écrivez ceci:

    case MotionEvent.ACTION_DOWN: 
        if (touchIsInsideCircle(x,y)) { 
         startTouch(x,y); 
         invalidate(); 
        } 
        break ; 
    
  • définir une fonction qui connaît le centre de l'écran en utilisant les coordonnées prédéfinies:

    boolean touchIsInsideCircle(float x, float y) { 
        return (x < maxX && x > minX && y < maxY && y > minY); 
    } 
    
+0

donc j'ai essayé avec ce touchIsInsideCircle private boolean '(x float, float y) { int a = metrics.widthPixels; // pour obtenir la largeur de l'écran int b = metrics.heightPixels; return (x < a/2 && x > a/2 && y <(b/2) -160) && y> ((b/2) -160))} ' Ce sont les coordonnées du cercle, le problème que le dessin commence toujours avec x == 0 et y == 0 et chaque fois que je touche l'écran, les points sont connectés – CamlX