【发布时间】:2012-03-21 16:07:09
【问题描述】:
我在 android 中创建了一个自定义视图来在屏幕上显示球。现在我想要的是,当我触摸那个球时,它应该分成四个部分爆炸,每个部分应该移动不同的四个方向,即上、下、左、右。我知道我必须设置触摸监听器来检测球上的触摸但是如何创建爆炸效果?这个问题现在解决了。我在屏幕上显示了多个球,以便用户可以点击它并爆炸它们。
这是我的自定义视图:
public class BallView extends View {
private float x;
private float y;
private final int r;
public BallView(Context context, float x1, float y1, int r) {
super(context);
this.x = x1;
this.y = y1;
this.r = r;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(x, y, r, mPaint);
}
}
SmallBall 具有类似的属性,除了一个是方向和一个在方向上移动它的爆炸方法和一个动画标志来停止它的移动。
private final int direction;
private boolean anim;
public void explode() {
// plus or minus x/y based on direction and stop animation if anim flag is false
invalidate();
}
我的布局xml如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/main_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF66FF33" />
我将 BallView 和 SmallBall 添加到活动类中,如下所示:
final FrameLayout mainFrameLayout = (FrameLayout) findViewById(R.id.main_frame_layout);
SmallBall[] smallBalls = new SmallBall[4];
smallBalls[0] = new SmallBall(getApplicationContext(), 105, 100, 10, 1, false);
smallBalls[0].setVisibility(View.GONE);
mainFrameLayout .addView(smallBalls[0]);
// create and add other 3 balls with different directions.
BallView ball = new BallView(getApplicationContext(), 100, 100, 25, smallBalls);
listener = new MyListener(ball);
ball.setOnClickListener(listener);
mainFrameLayout.addView(ball);
我在不同的位置添加了多个 BallView 及其相关的 SmallBall 数组。现在,无论我在屏幕上最后添加的位置单击哪里,会发生什么 BallView 开始爆炸。在倒数第二个之后,依此类推。所以这里有两个问题:
- 为什么无论我点击屏幕的哪个位置,它都会调用 onClick/onTouch 事件?当我单击特定的 BallView 时,它应该只调用侦听器事件。
- 其次是为什么 BallView 开始以与添加到布局的方式相反的方式爆炸?
我的监听类:
public void onClick(View v) {
BallView ballView = (BallView) v;
ballView.setVisibility(View.GONE);
//get small balls associated with this ball.
//loop through small ball and call their explode method.
}
由于有问题的字符限制,我已经修剪了代码。
【问题讨论】:
-
您在为爆炸效果编码时遇到了什么问题?你想要零件运动的动画还是爆炸图形?如果你能提供一张图片或你想要实现的东西就更好了。
-
@Creator:我想要的是当有人点击任何球时,它会爆炸成四个小球,并且每面墙应该朝四个方向之一移动。我不确定要使用什么,因为我是 android 新手。可能是爆炸部分的动画将是一个不错的选择。