public class MyGameView extends View implements OnTouchListener{
static int x, y;
final static int radius = 25;
private int androidX;
private int androidY;
Paint paint;
Bitmap android;
boolean touch = false;
Handler handler = new Handler();
private Runnable r = new Runnable() {
@Override
public void run() {
invalidate();
}
};
private Runnable r2 = new Runnable() {
@Override
public void run() {
androidX = (int) Math.ceil(Math.random() * (MyGameView.this.getWidth()-android.getWidth()));
androidY = (int) Math.ceil(Math.random() * (MyGameView.this.getHeight()-android.getHeight()));
}
};
public MyGameView(Context context) {
super(context);
paint = new Paint();
paint.setAntiAlias(true); // smooth rendering
android = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
setFocusable(true);
setOnTouchListener(this);
}
@Override
protected void onDraw(Canvas canvas) {
if(touch == true){
paint.setARGB(255, 222, 78, 112);
canvas.drawBitmap(android, androidX, androidY, null);
canvas.drawCircle(x,y,radius,paint);
}
handler.postDelayed(r, 1000);
handler.postDelayed(r2, 3000);
super.onDraw(canvas);
}
@Override
public boolean onTouch(View view, MotionEvent event) {
x=(int)event.getX()-(radius/2);
y=(int)event.getY()-(radius/2);
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
touch = true;
break;
case MotionEvent.ACTION_UP:
touch = false;
break;
}
invalidate();
return true;
}
}
Here my code What i want is when i hold my finger the bitmap start drawing every 3 second in random position x y. Everything work but the problem image draw like crazy to every position and i can't figure how to do that i try calling invalidate() on ACTION_DOWN but now my circle draw every 3 second to the position i touch. Any help is appreciate.
I'm not convinced that I totally understand what you want, but the problem with your code is the following: onDraw
is called very often. In each of those calls, you run r2
with a delay of 3 seconds. So effectively, r2
is just run as often as onDraw
, but the first time is delayed for 3 seconds.
What I think you want to do instead is run r2
in a separate thread and continuously update the position every 3 seconds, as in:
public class MyGameView extends View implements OnTouchListener{
static int x, y;
final static int radius = 25;
private int androidX;
private int androidY;
Paint paint;
Bitmap android;
boolean touch = false;
private Runnable r2 = new Runnable() {
@Override
public void run() {
androidX = (int) Math.ceil(Math.random() * (MyGameView.this.getWidth()-android.getWidth()));
androidY = (int) Math.ceil(Math.random() * (MyGameView.this.getHeight()-android.getHeight()));
}
};
public MyGameView(Context context) {
super(context);
paint = new Paint();
paint.setAntiAlias(true); // smooth rendering
android = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
setFocusable(true);
setOnTouchListener(this);
new Thread(r2).start(); // run r2 in a separate thread
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(touch == true){
paint.setARGB(255, 222, 78, 112);
canvas.drawBitmap(android, androidX, androidY, null);
canvas.drawCircle(x,y,radius,paint);
}
}
@Override
public boolean onTouch(View view, MotionEvent event) {
x=(int)event.getX()-(radius/2);
y=(int)event.getY()-(radius/2);
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
touch = true;
break;
case MotionEvent.ACTION_UP:
touch = false;
break;
}
return true;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.