• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Using Canvas draw to draw clickable circle

s0uLx09

Lurker
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);
}
 
Is your onDraw() method actually getting executed?
Set a breakpoint in onDraw() to check.
 
Another thing to check is, what values do variables 'x' and 'y' have? Are they valid?
 
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.
 
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
 
No I think drawing a circle on a canvas is straightforward enough and should work.
 
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();
}
 
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??
 
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.
 
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();
}
});

}
 
Back
Top Bottom