Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Free collision Animation of small Ball by Android

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "Android how to achieve free collision animation of small ball". In daily operation, I believe many people have doubts about how to achieve free collision animation of small ball in Android. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubt of "how to achieve free collision animation of small ball in Android". Next, please follow the editor to study!

Preface

This article will give a brief introduction to a free collision animation project of a small ball based on Android, and show some key codes and dynamic diagrams of actual effects.

1. Add balls List

Add green, yellow, red, black, blue and gray balls and set their size

BallList.add (new Ball, Color.GREEN, 700, width / 2, height / 2); ballList.add (new Ball (100,Color.YELLOW, 600,width / 4 * 3, height / 4 * 3)); ballList.add (new Ball (120,Color.RED, 800,width / 3 * 3, height / 3 * 3); ballList.add (new Ball (100,Color.BLACK, 800,width / 3 * 4, height / 3 * 4)) BallList.add (new Ball, Color.BLUE, 800, width / 5 * 3, height / 5 * 3); ballList.add (new Ball (100,Color.DKGRAY, 800, width / 4 * 5, height / 5 * 4)); 2.ball parameterint radius; / / Radius long speed =-1; / / collision velocity int color; / / Color long degree =-1; / / initial direction value int bgAlpha / / Transparency long lifeSpan =-1 * * 3. To judge whether there is a collision ball.

Key code

Private void checkoutBall () {for (int I = 0; I)

< ballList.size(); i++) { Ball ball1 = ballList.get(i); Ball ball2 = (Ball) distanceMap.get(ball1); if (ball2.getX() == -1 && ball2.getY() == -1) { continue; } int distance2 = Math.abs(ball1.getX() - ball2.getX()) * Math.abs(ball1.getX() - ball2.getX()) + Math.abs(ball1.getY() - ball2.getY()) * Math.abs(ball1.getY() - ball2.getY()); //最小距离小于阈值,发生碰撞 计算碰撞角度 if (distance2 x2) { ball1.setDegree(90); ball2.setDegree(270); } else { ball1.setDegree(270); ball2.setDegree(90); } } else { //斜碰 两球圆心连接线的角度做反向运动 float tan = (x1 - x2) / (y1 - y2); //正弦对应的角度在[-90,90] 在这取绝对值让角度值为正值 int angle = (int) Math.abs(Math.atan(tan)); if (x1 >

X 2 & & y 1 > y 2) {/ / sine value is positive angle is [0Magne90] ball1 is lower right ball2 is upper left ball1.setDegree (angle + 90); ball2.setDegree (angle + 270);} else if (x 1 > x 2 & & y 1)

< y2) { //正弦值为负数 角度为[-90,0] ball1在右上 ball2在左下 ball1.setDegree(90 - angle); ball2.setDegree(270 - angle); } else if (x1 < x2 && y1 >

Y2) {/ / the sine value is negative and the angle is [- 90 angle 0] ball1 in the lower right ball2 in the upper left ball1.setDegree (270-angle); ball2.setDegree (90-angle);} else if (x1)

< x2 && y1 < y2) { //正弦值为正数 角度为[0,90] ball1在左上 ball2在右下 ball1.setDegree(angle + 270); ball2.setDegree(angle + 90); } else if (x1 == x1) { //正弦值为0 angle为0 if (y1 >

Y2) {ball1.setDegree (180); ball2.setDegree (angle);} else {ball1.setDegree (angle); ball2.setDegree (180) } / / collision reinitialization data distanceMap.put (ball2, new Ball ()); distanceMap.put (ball1, new Ball ());}

1.collision detection

two。 Call onDraw again after collision (draw ball)

Private class MyThread extends Thread {@ Override public void run () {while (true) {actionBall (); postInvalidate (); / / update the interface and call onDraw () try {/ / sleep (1000 / 36); sleep (10) } catch (Exception e) {e.printStackTrace ();}}

OnDraw

Protected void onDraw (Canvas canvas) {super.onDraw (canvas); for (int I = 0; I

< ballList.size(); i++) { Ball ball = ballList.get(i); Paint paint = new Paint(); paint.setColor(ball.getColor()); paint.setAlpha(ball.getAlpha()); paint.setStyle(Paint.Style.FILL); paint.setStrokeWidth(1); canvas.drawCircle(ball.getX(), ball.getY(), ball.getRadius(), paint); } }4.application display 1.静态界面

two。 Dynamic effect diagram

At this point, the study of "how to realize the free collision animation of small balls by Android" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report