Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package viewmotionexample.ballanimation;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Point;
- import android.graphics.Rect;
- import android.graphics.RectF;
- import android.graphics.drawable.Drawable;
- import android.util.AttributeSet;
- import android.view.Display;
- import android.view.MotionEvent;
- import android.view.VelocityTracker;
- import android.view.View;
- import android.view.WindowManager;
- import java.util.ArrayList;
- import java.util.Random;
- /**
- * Created by David Olsen, 2015
- * Public Domain. Use for anything.
- */
- public class CirclesView extends View {
- Random random = new Random();
- ArrayList<BallDrawing> circles = new ArrayList<>();
- Activity activity;
- Paint backgroundPaint = new Paint();
- public CirclesView(Context context) {
- super(context);
- this.activity = (Activity) context;
- init();
- }
- public CirclesView(Context context, AttributeSet attrs) {
- super(context, attrs);
- this.activity = (Activity) context;
- init();
- }
- public CirclesView(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- this.activity = (Activity) context;
- init();
- }
- VelocityTracker velocityTracker;
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_UP:
- BallDrawing bd = new BallDrawing(event.getX(), event.getY());
- velocityTracker.computeCurrentVelocity(10);
- bd.vx = velocityTracker.getXVelocity();
- bd.vy = velocityTracker.getYVelocity();
- circles.add(bd);
- break;
- case MotionEvent.ACTION_MOVE:
- velocityTracker.addMovement(event);
- break;
- case MotionEvent.ACTION_DOWN:
- velocityTracker.clear();
- break;
- }
- return true;
- }
- Drawable drawable;
- Rect drawableBounds = null;
- int maxX, maxY;
- int minX = 0, minY = 0;
- private void init() {
- WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
- Display display = wm.getDefaultDisplay();
- Point size = new Point();
- display.getSize(size);
- maxX = size.x;
- maxY = size.y;
- velocityTracker = VelocityTracker.obtain();
- backgroundPaint.setColor(Color.BLUE);
- drawable = getResources().getDrawable(R.drawable.ball);
- if (drawable != null) {
- drawable.setBounds(0, 0, 40, 40);
- drawableBounds = drawable.getBounds();
- }
- Thread t = new Thread(new Runnable() {
- @Override
- public void run() {
- while (true) {
- try {
- Thread.sleep(100);
- tick();
- } catch (InterruptedException e) {
- return;
- }
- }
- }
- });
- t.start();
- }
- public void tick() {
- activity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- for (BallDrawing circle : circles) {
- circle.tickMotion();
- }
- for (int j = 0; j < circles.size(); j++) {
- BallDrawing jball = circles.get(j);
- RectF jballbounds = jball.getBounds();
- for (int k = 0; k < circles.size(); k++) {
- if (j != k) {
- BallDrawing kball = circles.get(k);
- RectF kballBounds = kball.getBounds();
- if (kballBounds.intersect(jballbounds)) {
- kball.hit();
- //jball.hit();
- }
- }
- }
- }
- }
- });
- }
- @Override
- protected void onDraw(Canvas canvas) {
- for (BallDrawing c : circles) {
- c.draw(canvas);
- }
- }
- private class BallDrawing {
- public float cx, cy;
- public float vx, vy;
- public BallDrawing(float cx, float cy) {
- this.cx = cx;
- this.cy = cy;
- }
- public RectF getBounds() {
- return new RectF(cx,cy,cx+drawableBounds.width(),cy+drawableBounds.height());
- }
- public void applyMotion() {
- cx += vx;
- cy += vy;
- if (cx < minX) {
- vx =-vx;
- cx = minX - (cx - minX);
- }
- else if (cx > maxX) {
- vx =-vx;
- cx = maxX - (cx - maxX);
- }
- if (cy < minY) {
- vy =-vy;
- cy = minY - (cy - minY);
- }
- else if (cy > maxY) {
- vy =-vy;
- cy = maxY - (cy - maxY);
- }
- }
- public void hit() {
- vx *= -1;
- vy *= -1;
- }
- public void tickMotion() {
- invalidate((int) cx + drawableBounds.left, (int) cy + drawableBounds.top, (int) cx + 1 + drawableBounds.right, (int) cy + 1 + drawableBounds.bottom);
- applyMotion();
- invalidate((int) cx + drawableBounds.left, (int) cy + drawableBounds.top, (int) cx + 1 + drawableBounds.right, (int) cy + 1 + drawableBounds.bottom);
- }
- public void draw(Canvas canvas) {
- canvas.save();
- canvas.translate(cx, cy);
- drawable.draw(canvas);
- canvas.restore();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement