1. Download our Official Android App: Forums for Android!

Apps Using Canvas draw to draw clickable circle

Discussion in 'Android Development' started by s0uLx09, Mar 8, 2016.

  1. s0uLx09

    s0uLx09 Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    7
    Joined:
    Mar 8, 2016

    Mar 8, 2016
    7
    0
    15
    Male
    Hi,
    I am new to android programming and I am trying to create an application that randomly appear green circle for user to click on. My current code doesnt even make an circle appear. Does anyone have an answer to it?


    protectedvoid onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    String timeData = getIntent().getExtras().getString("timeDuration");
    if(timeData =="30sec"){
    timeLeft =30;
    }

    textViewTime =(TextView)findViewById(R.id.textViewTime);
    textViewScore =(TextView)findViewById(R.id.textViewScore);
    textViewTime.setText(timeData);
    finalCounterClass timer =newCounterClass(30000,1000);
    timer.start();
    onDraw();
    }

    privatevoid onDraw(){
    Random random =newRandom();
    Display display = getWindowManager().getDefaultDisplay();
    Point screenSize=newPoint();
    display.getSize(screenSize);
    int width = screenSize.x;
    int height = screenSize.y;
    float x =random.nextFloat()* width;
    float y =random.nextFloat()* height;
    Paint paint =newPaint();
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.STROKE);
    int RADIUS =50;
    Canvas canvas =newCanvas();
    canvas.drawCircle(x, y, RADIUS, paint);
    }
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,440
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,440
    10,724
    1,988
    Male
    Software developer
    South West of England
    Is your onDraw() method actually getting executed?
    Set a breakpoint in onDraw() to check.
     
  3. s0uLx09

    s0uLx09 Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    7
    Joined:
    Mar 8, 2016

    Mar 8, 2016
    7
    0
    15
    Male
    Yes, it is being executed.. but the circle is not drawn
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,440
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,440
    10,724
    1,988
    Male
    Software developer
    South West of England
  5. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,440
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,440
    10,724
    1,988
    Male
    Software developer
    South West of England
    Another thing to check is, what values do variables 'x' and 'y' have? Are they valid?
     
  6. s0uLx09

    s0uLx09 Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    7
    Joined:
    Mar 8, 2016

    Mar 8, 2016
    7
    0
    15
    Male
    yes, I have checked that both x and y are float values.. I have to constantly redraw the circle, is that method possible? I want the user to click the green circle, then it will disappear and appear at another random location again.
     
  7. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,440
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,440
    10,724
    1,988
    Male
    Software developer
    South West of England
    Did you try that example I linked to?
     
  8. s0uLx09

    s0uLx09 Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    7
    Joined:
    Mar 8, 2016

    Mar 8, 2016
    7
    0
    15
    Male
    yeap, but it seems like the circle is not being drawn again.. Do you think it is easier if I display the green circle using a image instead of canvas draw? making the image clickable and then making the image appear and disappear on random location upon clicking it
     
  9. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,440
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,440
    10,724
    1,988
    Male
    Software developer
    South West of England
    No I think drawing a circle on a canvas is straightforward enough and should work.
     
  10. s0uLx09

    s0uLx09 Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    7
    Joined:
    Mar 8, 2016

    Mar 8, 2016
    7
    0
    15
    Male
    I did it with image, and I think is simpler. but how do I remove the image once it has been clicked on? I currently used visibility but I can still click on it even thou the visibility is set to gone? is there a way to totally remove the image?

    My current code is:
    private void drawCircle() {
    Random random = new Random();
    Display display = getWindowManager().getDefaultDisplay();
    Point screenSize= new Point();
    display.getSize(screenSize);
    int width = screenSize.x;
    int height = screenSize.y;
    float x =random.nextFloat() * width;
    float y =random.nextFloat() * height;
    ImageView imgView = (ImageView)findViewById(R.id.gameass);
    imgView.setImageResource(R.drawable.gameass);
    imgView .setVisibility(View.VISIBLE);
    Matrix matrix = new Matrix();
    matrix.reset();
    matrix.postTranslate(x, y);
    imgView.setScaleType(ImageView.ScaleType.MATRIX);
    imgView.setImageMatrix(matrix);
    imgView.setOnClickListener(this);

    }

    @override
    public void onClick(View v) {
    ImageView imgView = (ImageView)findViewById(R.id.gameass);
    imgView.setImageResource(R.drawable.gameass);
    imgView.setVisibility(View.GONE);
    score++;
    tempScore = String.valueOf(score);
    textViewScore.setText(tempScore);
    drawCircle();
    }
     
  11. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,440
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,440
    10,724
    1,988
    Male
    Software developer
    South West of England
    Yes you must call removeView(imgView) on the parent view
     
  12. s0uLx09

    s0uLx09 Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    7
    Joined:
    Mar 8, 2016

    Mar 8, 2016
    7
    0
    15
    Male
    I tried using removeView but the app crashes the moment I click on the screen.


    public void onClick(View v) {
    ViewGroup parent = (ViewGroup) v.getParent();
    parent.removeView(v);
    score++;
    tempScore = String.valueOf(score);
    textViewScore.setText(tempScore);
    drawCircle();
    }

    Where did my code went wrong??
     
  13. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,440
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,440
    10,724
    1,988
    Male
    Software developer
    South West of England
    You need to learn how to debug your app. Sorry, this may sound very unhelpful, but until you master the basics of debugging your code, you will be doomed to ask a stream of basic questions like this, and your progress will be excruciatingly slow. In fact, nobody can tell you the answer to what you ask above because you've not provided enough information to diagnose it.
    To get some pointers, please read the sticky thread in this forum, which tells you the basics of how to debug.
     
  14. s0uLx09

    s0uLx09 Lurker
    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    7
    Joined:
    Mar 8, 2016

    Mar 8, 2016
    7
    0
    15
    Male
    Im sorry, but thanks for your help so far. I manage to troubleshoot and manage to solve certain issues. But I dont understand about the Onclicklistener. Even when I did not click on my image, the image still refreshes and went into the onclick method. How do I prevent this?

    This my ondraw method code:


    private void drawCircle() {
    int a=0,b=0;
    Random random = new Random();
    Display display = getWindowManager().getDefaultDisplay();
    Point screenSize= new Point();
    display.getSize(screenSize);
    int width = screenSize.x;
    int height = screenSize.y;
    float x =random.nextFloat() * width;
    float y =random.nextFloat() * height;
    a = (int)x;
    b = (int)y;
    final ImageView imgView = (ImageView)findViewById(R.id.gameass);
    RelativeLayout.LayoutParams absParam = (RelativeLayout.LayoutParams) imgView.getLayoutParams();
    imgView.setPadding(a,b,0,0);
    imgView.setImageResource(R.drawable.gameass);
    imgView .setVisibility(View.VISIBLE);
    imgView.setOnClickListener(new View.OnClickListener() {
    @override
    public void onClick(View v) {
    imgView.setImageResource(0);
    score++;
    tempScore = String.valueOf(score);
    textViewScore.setText(tempScore);
    drawCircle();
    }
    });

    }
     

Share This Page

Loading...