2016-01-08 1 views
0

C'est la première fois que j'utilise Canvas dans Android. Je crée une application qui afficherait des cercles à certaines positions sur l'écran, l'un après l'autre (les positions sont sélectionnées de manière aléatoire). Un nouveau cercle devrait être dessiné après que le précédent a été sélectionné/touché, et le précédent devrait disparaître. J'ai quelques idées à ce sujet: garder une liste d'objets Point (chaque objet contient les coordonnées x, y du centre du cercle) et en sélectionner aléatoirement une à chaque fois que le cercle est dessiné sur l'écran. Donc d'abord, je suis en train de remplir un tableau de points. Je sais aussi comment sélectionner aléatoirement l'élément d'arraylist. Ma plus grande confusion est de savoir comment connecter les méthodes onDraw et onTouchEvent entre elles? Je sais que je devrais vérifier si le cercle a été sélectionné et ensuite seulement dessiner un nouveau cercle à la position choisie au hasard, mais je ne sais pas comment appeler la méthode onDraw() de l'onTouchEvent ...Dessinez un cercle dans Android Canvas lorsque le cercle précédent a été sélectionné à des positions prédéfinies

S'il vous plaît aider avec ce problème? Mon code est ci-dessous:

package com.example.researcher.heatmap; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Point; 
import android.graphics.PorterDuff; 
import android.graphics.drawable.Drawable; 
import android.text.TextPaint; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.view.View; 

import java.util.ArrayList; 
import java.util.List; 

/** 
* TODO: document your custom view class. 
*/ 
public class MyView extends View { 

    Paint paint; 
    ArrayList<Point> points = new ArrayList<>(); 



    public MyView(Context context) { 
     super(context); 
     init(); 
    } 

    public MyView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public MyView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    private void init() { 
     // Load attributes 
     paint = new Paint(); 
     paint.setColor(Color.BLUE); 
     paint.setStrokeWidth(5); 
     paint.setStyle(Paint.Style.STROKE); 
     populateArrayList(); 

    } 


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


     paint.setStyle(Paint.Style.STROKE); 
      canvas.drawColor(Color.WHITE); 
      int i=1; // should be random, will randomize later 

      for(Point p: points) { 
       p.x = points.get(i).x; 
       p.y = points.get(i).y; 

       canvas.drawCircle(p.x, p.y, 50, paint); 
      } 

    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 


       int i=1; 

       for(Point p: points) { 
        Canvas canvas = new Canvas(); 
        p.x = points.get(i).x; 
        p.y = points.get(i).y; 
        canvas.drawCircle(p.x, p.y, 50, paint); 
       } 

       postInvalidate(); 

      case MotionEvent.ACTION_MOVE: 
      case MotionEvent.ACTION_UP: 
      case MotionEvent.ACTION_CANCEL: { 
       break; 
      } 
     } 
     postInvalidate(); 
     return true; 

    } 

    public void populateArrayList(){ 
     points.clear(); 
     points.add(new Point(120, 120)); 
     points.add(new Point(150, 320)); 
     points.add(new Point(280, 200)); 
    } 

} 

Répondre

0

classe var

private int state = 0  //0 normal, 1 new circle 
private int pointPos = 0; //Which point we will be drawing 

onDraw était en remplacant x/y de tous vos autres points de

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


    paint.setStyle(Paint.Style.STROKE); 
    canvas.drawColor(Color.WHITE); 

    if(state == 1){ 
     pointPos = random(); //between 0 and points.length 
     state = 0; 
    } 

    canvas.drawCircle(points.get(pointsPos).x, points.get(pointsPos).y, 50, paint); 

} 

onTouchEvent: Dessin ne doit être fait sur la Retirez, utilisez les drapeaux/états pour garder une trace de ce que vous devriez faire lors du prochain tirage au sort

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 

      //Though these days this is usually done on the up event 
      //Check if the point press is within the circle 
      if(contains(event, points.get(pointPos))){ 
       state = 1; 
       postInvalidate(); 
      } 



     case MotionEvent.ACTION_MOVE: 
     case MotionEvent.ACTION_UP: 
     case MotionEvent.ACTION_CANCEL: { 
      break; 
     } 
    } 
    postInvalidate(); 
    return true; 

} 
+0

Cher Chris Handy, merci beaucoup pour votre réponse! Cela fonctionne bien maintenant! Pour ceux qui ont le même problème que moi, je mettrai à jour le code plus tard une fois que j'aurai fait fonctionner les choses –

1

Merci Chris pour votre aide! J'apprécie vraiment cela.

Voici ma solution si quelqu'un aura besoin pour référence

package com.example.researcher.heatmap; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.graphics.Point; 
import android.graphics.PorterDuff; 
import android.graphics.drawable.Drawable; 
import android.text.TextPaint; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.view.View; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.Random; 

/** 
* TODO: document your custom view class. 
*/ 
public class MyView extends View { 

    Paint paint; 
    ArrayList<Point> points = new ArrayList<>(); 
    private int pointsPos = 0; //Which point we will be drawing 
    public float x; 
    public float y; 
    public int radius = 150; 



    public MyView(Context context) { 
     super(context); 
     x = this.getX(); 
     y = this.getY(); 
     init(); 
    } 

    public MyView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     x = this.getX(); 
     y = this.getY(); 
     init(); 
    } 

    public MyView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     x = this.getX(); 
     y = this.getY(); 
     init(); 
    } 

    private void init() { 
     // Load attributes 
     paint = new Paint(); 
     paint.setColor(Color.BLUE); 
     paint.setStrokeWidth(5); 
     paint.setStyle(Paint.Style.STROKE); 
     populateArrayList(); 

    } 


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


     paint.setStyle(Paint.Style.STROKE); 
     canvas.drawColor(Color.WHITE); 

     canvas.drawCircle(points.get(pointsPos).x, points.get(pointsPos).y, radius, paint); 

    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
      case MotionEvent.ACTION_MOVE: 

      case MotionEvent.ACTION_UP: 
       //Check if the point press is within the circle 
       if(contains(event, points.get(pointsPos))){ 

        Random r = new Random(System.nanoTime()); 
        pointsPos = r.nextInt(points.size());; //between 0 and points.length 

        postInvalidate(); 
       } 


      case MotionEvent.ACTION_CANCEL: { 
       break; 
      } 
     } 
     postInvalidate(); 
     return true; 

    } 

    private boolean contains(MotionEvent event, Point point) { 
     float xTouch = event.getX(); 
     float yTouch = event.getY(); 
     if ((xTouch - point.x) * (xTouch - point.x) + (yTouch - point.y) * (yTouch - point.y) <= radius * radius) { 
      return true; 
     } 
     else { 
      return false; 
     } 
    } 

    public void populateArrayList(){ 
     points.clear(); 
     points.add(new Point(220, 1020)); 
     points.add(new Point(550, 320)); 
     points.add(new Point(780, 500)); 
    } 

}