Android studio烟花动画简单程序带注释
时间: 2023-08-31 15:42:39 浏览: 168
好的,以下是一个简单的Android Studio烟花动画程序,带有注释,供您参考:
1. 首先,在XML布局文件中创建一个空的FrameLayout,用于显示烟花效果。例如:
```
<FrameLayout
android:id="@+id/fireworks_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
2. 在Java代码中,创建一个FireworksView类,继承自View,用于绘制烟花效果。代码如下:
```
public class FireworksView extends View {
private Paint paint;
private List<Firework> fireworks;
public FireworksView(Context context) {
super(context);
init();
}
public FireworksView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FireworksView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paint = new Paint();
fireworks = new ArrayList<>();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (Firework firework : fireworks) {
firework.draw(canvas, paint);
}
invalidate();
}
public void addFirework(Firework firework) {
fireworks.add(firework);
}
}
```
3. 创建一个Firework类,用于表示一个烟花的属性和行为。代码如下:
```
public class Firework {
private int x;
private int y;
private int color;
private int radius;
private int speed;
private int angle;
public Firework(int x, int y, int color, int radius, int speed, int angle) {
this.x = x;
this.y = y;
this.color = color;
this.radius = radius;
this.speed = speed;
this.angle = angle;
}
public void draw(Canvas canvas, Paint paint) {
paint.setColor(color);
canvas.drawCircle(x, y, radius, paint);
move();
}
private void move() {
double radians = Math.toRadians(angle);
x += speed * Math.cos(radians);
y += speed * Math.sin(radians);
}
}
```
4. 在Activity中,获取FrameLayout并创建FireworksView对象,将其添加到FrameLayout中。代码如下:
```
public class MainActivity extends AppCompatActivity {
private FrameLayout fireworksContainer;
private FireworksView fireworksView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fireworksContainer = findViewById(R.id.fireworks_container);
fireworksView = new FireworksView(this);
fireworksContainer.addView(fireworksView);
}
}
```
5. 在Activity中,编写逻辑代码,用于控制烟花效果的生成和显示。例如,可以在点击屏幕时生成一个烟花,并将其添加到FireworksView中显示。代码如下:
```
public class MainActivity extends AppCompatActivity {
// 省略部分代码
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
int color = getRandomColor();
int radius = getRandomRadius();
int speed = getRandomSpeed();
int angle = getRandomAngle();
Firework firework = new Firework(x, y, color, radius, speed, angle);
fireworksView.addFirework(firework);
}
return super.onTouchEvent(event);
}
private int getRandomColor() {
// 随机生成颜色
return Color.rgb(
(int) (Math.random() * 256),
(int) (Math.random() * 256),
(int) (Math.random() * 256)
);
}
private int getRandomRadius() {
// 随机生成半径
return (int) (Math.random() * 40) + 20;
}
private int getRandomSpeed() {
// 随机生成速度
return (int) (Math.random() * 20) + 5;
}
private int getRandomAngle() {
// 随机生成角度
return (int) (Math.random() * 360);
}
}
```
以上就是一个简单的Android Studio烟花动画程序,带有注释。希望对您有所帮助!
阅读全文