2011-09-06 69 views
6

我有兩個圖像在屏幕上移動,一個是球,一個是男人。 我想要發生的事情是,當用戶觸摸男人的形象時,球就會掉下來。如何爲Android自定義添加OnClick事件查看

我的問題是我似乎無法添加onclick/ontouch事件,並得到它的工作。

我沒有正確實施,任何人都可以幫忙嗎?

我已經包括了下面的3個類。格雷格是男人,球被命名爲球:)

TestAnimationActivity.java

package com.test.firstAnimation; 

    import android.app.Activity; 
    import android.os.Bundle; 

    public class TestAnimationActivity extends Activity { 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(new MyAnimationView(this)); 
     } 
    } 

Sprite.java

package com.test.firstAnimation; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Toast; 

public class Sprite extends View implements OnClickListener{ 

    private static int gregCoordX = 410; // the x coordinate at the canvas for greg 
    private Bitmap img; // the image of Greg 
    private Bitmap img2; // the image of pointer 
    private static int gregCoordY = 125; // the y coordinate at the canvas for greg 
    private static int pointCoordX = 10; 
    private static int pointCoordY = 10; 
    private static int count = 1; 
    private static int ballSpeed = 25; 
    private static boolean goingRight = false; 
    private static boolean goingLeft = true; 
    private static boolean pointerGoingRight = false; 
    private static boolean pointerGoingLeft = true; 


    public Sprite(Context context, int drawable) { 

     super(context); 

     BitmapFactory.Options opts = new BitmapFactory.Options(); 
     opts.inJustDecodeBounds = true; 
     img = BitmapFactory.decodeResource(context.getResources(), drawable); 
     img2 = (BitmapFactory.decodeResource(context.getResources(), drawable)); 
     count++; 
    } 

    public static int getCount() { 
     return count; 
    } 

    void setX(int newValue) { 
     gregCoordX = newValue; 
    } 

    public static int getX() { 
     return gregCoordX; 
    } 

    public static int getY() { 
     return gregCoordY; 
    } 

    public static int getBX() { 
     return pointCoordX; 
    } 

    public static int getBY() { 
     return pointCoordY; 
    } 

    public Bitmap getBitmap() { 
     return img; 
    } 

    public Bitmap getImg2() { 
     return img2; 
    } 

    public static void dropBall() 
    { 
     pointCoordY++; 
    } 

    public static void moveBall(int x) { 

      // check the borders 
      //if more than ten go right 
      //if ten go left 
      //if more than 250 go left 
      if (x <= 10 && pointerGoingLeft) 
      { 
      pointCoordX = pointCoordX + ballSpeed; 
      pointerGoingRight = true; 
      pointerGoingLeft = false; 
      } 
      else if (x >= 410 && pointerGoingRight) 
      { 
       pointCoordX = pointCoordX - ballSpeed; 
       pointerGoingLeft = true; 
       pointerGoingRight = false; 
      } 
      else if (pointerGoingRight) 
       pointCoordX = pointCoordX + ballSpeed; 
      else 
       pointCoordX = pointCoordX - ballSpeed; 

      if(MyAnimationView.ballDropping == true) 
      { 
       while (pointCoordY<gregCoordY) 
        dropBall(); 
      } 
    } 

    public static void moveGreg(int x) { 

     if (x <= 10 && goingLeft) 
     { 
     gregCoordX = gregCoordX + count; 
     goingRight = true; 
     goingLeft = false; 
     } 
     else if (x >= 410 && goingRight) 
     { 
     gregCoordX = gregCoordX - count; 
     goingLeft = true; 
     goingRight = false; 
     } 
     else if (goingRight) 
     gregCoordX = gregCoordX + count; 
     else 
     gregCoordX = gregCoordX - count; 
} 

    @Override 
    public void onClick(View arg0) { 
     dropBall(); 
    } 
} 

MyAnimationView.java

package com.test.firstAnimation; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.view.View; 

public class MyAnimationView extends View{ 

    private Sprite greg; 
    private Sprite ball; 
    private int xCoOr; 
    private int ballXCoOr; 
    public static boolean ballDropping; 

    public MyAnimationView(Context context) { 
     super(context); 

     ballDropping = false; 
     greg = new Sprite(context,R.drawable.greg); 
     ball = new Sprite(context, R.drawable.ball); 

     OnClickListener gregClicked = new OnClickListener() { 
      public void onClick(View v) { 
      ballDropping = true; 
      } 
     }; 
     greg.setOnClickListener(gregClicked); 
     } 

    @Override protected void onDraw(Canvas canvas) { 

    canvas.drawColor(0xFFFFFFFF);         //white background  

    ballXCoOr = Sprite.getBX(); 
    xCoOr = Sprite.getX(); 
    Sprite.moveGreg(xCoOr);           //move ball left or right depending 
    Sprite.moveBall(ballXCoOr); 

    if(ballDropping == true) 
    { 
     Sprite.dropBall(); 
    } 

    canvas.drawBitmap(greg.getBitmap(), xCoOr, Sprite.getY(), null); 
    canvas.drawBitmap(ball.getBitmap(), ballXCoOr, Sprite.getBY(), null); 
    invalidate(); 
    } 
} 
提前

謝謝,我被困了好幾天!

+0

我爲窮人格式道歉,這是我的第一篇文章:) –

+0

我不認爲你的代碼工作,因爲雪碧觀點並不MyAnimationView的子視圖。 onClick監聽器不會觸發,因爲視圖永遠不會被使用 - 您只需讀出它們的位圖並在onDraw中繪製它們即可。我會張貼這個答案,但這是問題,而不是解決方案。 –

+0

看看這個解決方案:http://stackoverflow.com/a/17978891/779408 – breceivemail

回答

8
float touched_x, touched_y; 
    boolean touched = false; 
    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     // TODO Auto-generated method stub 
     touchCounter++; 
     touched_x = event.getX(); 
     touched_y = event.getY(); 

     int action = event.getAction(); 
     switch (action) { 
     case MotionEvent.ACTION_DOWN: 
      touched = true; 
      break; 
     case MotionEvent.ACTION_MOVE: 
      touched = true; 
      break; 
     case MotionEvent.ACTION_UP: 
      touched = false; 
      break; 
     case MotionEvent.ACTION_CANCEL: 
      touched = false; 
      break; 
     case MotionEvent.ACTION_OUTSIDE: 
      touched = false; 
      break; 
     default: 
     } 
     return true; // processed 
    } 

即可;

if (touched) { 
     //control here 
    } 

touched_x,touched_y是點擊屏幕上的點的座標。您可以比較Greg的座標和這些座標。如果相同,那就做你想做的事。

+0

感謝您的幫助,生病了試試:) –

4


public class CustomView extends View implements OnClickListener { 

    public CustomView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    public CustomView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public CustomView(Context context) { 
     super(context); 
     init(); 
    } 

    private void init(){ 
     setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 

     // Do whatever you need to 
    } 

} 
相關問題