在Android的自定义对象点击问题 [英] Custom object click issue in android

查看:186
本文介绍了在Android的自定义对象点击问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了安卓的自定义视图显示球在屏幕上。现在,当我谈谈那个球应该发生爆炸的四个部分,每个部分应该将不同的四个方向,即上,下,左,右我想是。我知道我必须设置触摸监听器来检测触摸的球,但随后如何创建一个爆炸效果?<子>这个问题解决了,现在。我在屏幕上显示多个球,使用户可以点击它和爆炸他们。

I have created an custom view in android to display ball on screen. Now what I want is when I touch on that ball it should explode in four parts and every part should move different four directions ie up, down, left, right. I know I have to set touch listener to detect touch on ball but then how to create a explode effect?This issue is solved now. I'm displaying multiple balls on screen so that user can click on it and explode them.

下面是我的自定义视图:

Here is my custom view:

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具有类似性质的,除了一个是方向和爆炸方法将其移动方向和动画标志,以阻止其移动。

SmallBall with similar properties except one that is direction and an explode method to move it in direction and animation flag to stop it moving.

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如下:

My layout xml is as follows:

<?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到活动类,如下所示:

I'm adding BallView and SmallBall to activity class as follows:

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开始爆炸。在此之后第二次最后,等等。因此,这里有2个问题:

I'm adding multiple BallView and their relative SmallBall array at different positions. Now what happens no matter where I click on screen last added BallView starts to explode. After that second last, and so on. So here are 2 issues:

  1. 为什么它叫的onClick / onTouch事件无论我在哪里点击屏幕上的?它应该只调用侦听器事件,当我点击特定BallView。
  2. 其次就是为什么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.
    }

<子>我修剪code由于问题性质的限制。

推荐答案

我不认为你需要努力code这一切都在画布上。您可以在触摸监听器调用 ball.setVisibility(View.GONE)键,显示4个球用 smallBall.setVisibility(View.Visible)的每个小球。你会这样能够隐藏大球,而且可以显示小球。 现在的运行效果,有可能是一个方法,在每一个smallBall方向在哪里需要传递,你可以这样调用smallBall.explode(方向)。该方法的实现可能是

I don't think you need to hardcode all this in a canvas. You can call ball.setVisibility(View.GONE) in the Touch Listener and show 4 extra balls by using smallBall.setVisibility(View.Visible) for every small ball. This way you'd be able to hide big ball and can show small balls. Now for the movement effect, there can be a method in every smallBall where the direction needs to be passed and you can call it like this smallBall.explode(direction). The method implementation could be

explode(int direction){//can be String
  if(direction=NORTH)
     y--;
  //other condition checks
}

的爆炸方法将开始改变其x和y坐标,根据传递方向。 我希望这会给你一个关于如何实现它的提示。

The explode method will start changing their x and y coordinate, based on the direction passed. I hope this'll give you a hint about how to implement it.

这篇关于在Android的自定义对象点击问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆