Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.billy.accelerator;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.RectF;
- import android.graphics.Typeface;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.Toast;
- import java.util.Formatter;
- import java.util.Random;
- public class Ball extends View {
- private int xMin = 0;
- private int xMax;
- private int yMin = 0;
- private int yMax;
- private float ballRadius = 80, ballX = ballRadius + 20, ballY = ballRadius + 20, ballSpeedX = 0, ballSpeedY = 0;
- private RectF ballBounds;
- private Paint paint ;
- public boolean touch = false;
- public float rx, ry, currentX, currentY;
- private Context c;
- private StringBuilder statusMsg = new StringBuilder();
- private Formatter formatter = new Formatter(statusMsg);
- public Ball(Context context) {
- super(context);
- c = context;
- ballBounds = new RectF();
- paint = new Paint();
- paint.setTypeface(Typeface.MONOSPACE);
- paint.setTextSize(50);
- this.setFocusableInTouchMode(true);
- }
- int x = 0, y = 1;
- @Override
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- ballBounds.set(rx-ballRadius, ry-ballRadius, rx+ballRadius, ry+ballRadius);
- paint.setColor((Color.BLUE));
- canvas.drawOval(ballBounds, paint);
- canvas.drawText(String.valueOf(statusMsg), 0, 50 ,paint);
- MainActivity mc = (MainActivity)c;
- this.ballSpeedX = mc.getX();
- this.ballSpeedY = mc.getY();
- update();
- try{
- Thread.sleep(1);
- }catch (Exception e){
- e.printStackTrace();
- }
- invalidate();
- }
- public void update() {
- rx += ballSpeedX;
- ry += ballSpeedY;
- if (currentX < rx + ballRadius && currentX > rx - ballRadius && touch) {
- ballSpeedX = 0;
- }
- if (currentY< ry + ballRadius && currentY > ry - ballRadius && touch) {
- ballSpeedY = 0;
- }
- if(currentX > rx + ballRadius && touch){
- ballSpeedX = 16;
- }else if(currentX < rx + ballRadius && touch){
- ballSpeedX = -16;
- }
- if(currentY > ry + ballRadius && touch){
- ballSpeedY = 16;
- }else if(currentY < ry + ballRadius && touch){
- ballSpeedY = -16;
- }
- statusMsg.delete(0, statusMsg.length());
- formatter.format("Ball@(%3.0f, %3.0f).Speed=(%2.0f, %2.0f)", rx, rx, ballSpeedX, ballSpeedY);
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- currentX = event.getX();
- currentY = event.getY();
- if(event.getAction() == MotionEvent.ACTION_DOWN){
- touch = true;
- if(event.getAction() == MotionEvent.ACTION_MOVE){
- currentX = event.getX();
- currentY = event.getY();
- }
- }
- if(event.getAction() == MotionEvent.ACTION_UP){
- ballSpeedX = 0;
- ballSpeedY = 0;
- touch = false;
- }
- return true;
- }
- @Override
- public void onSizeChanged(int w, int h, int oldW, int oldH) {
- xMax = w - 1;
- yMax = h - 1;
- rx = new Random().nextFloat() * (xMax - ballX) + ballRadius;
- ry = new Random().nextFloat() * (yMax - ballY) + ballRadius;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement