Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Juego extends SurfaceView implements View.OnTouchListener{
- private Paint paint;
- int x = 100, y = 100, radio = 100, otroX, otroY;
- public Juego(Context context, AttributeSet attrs) {
- super(context, attrs);
- this.setOnTouchListener(this);
- setFocusable(true);
- paint = new Paint();
- }
- public void onDraw(Canvas canvas) {
- paint.setColor(Color.WHITE);
- canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
- paint.setColor(Color.BLACK);
- canvas.drawCircle(x, y, radio, paint);
- otroX = canvas.getWidth() / 2;
- otroY = canvas.getHeight() / 2;
- canvas.drawCircle(otroX, otroY, radio, paint);
- invalidate();
- }
- @Override
- public boolean onTouch(View view,MotionEvent motionEvent){
- x = (int)motionEvent.getX();
- y = (int)motionEvent.getY();
- double dist = Math.sqrt(Math.pow((x - otroX), 2) + Math.pow((y - otroY), 2));
- if (dist <= radio + radio) {
- if (x < otroX) {
- x = otroX - radio;
- }
- if (x > otroX) {
- x = otroX + radio;
- }
- if (y < otroY) {
- y = otroY - radio;
- }
- if (y > otroY) {
- y = otroY + radio;
- }
- }
- invalidate();
- return true;
- }
Add Comment
Please, Sign In to add comment