2014-01-27 16 views
-1

我有圖像(球)這種類型的BitmapDrawable的,它在animatedview.java以其優良的移動屏幕上的所有方向爲我正在的OnDraw(畫布)。我的要求是我想要單擊圖像只可惜整個屏幕作爲一個event.so普萊舍幫助我,如何在圖像上單擊只有這樣,我就出發更遠development.i會下降我在這裏總碼如何在bitmapdrawable單擊從畫布

MainActivity 

public class MainActivity extends Activity { 

    Context context; 
    int count=0; 
    EditText text; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     AnimatedView animatedView=(AnimatedView) findViewById(R.id.anim_view); 
     animatedView.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       EditText text=(EditText) findViewById(R.id.editText1); 

       ImageView imageView=(ImageView) findViewById(R.drawable.ball); 
       v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation)); 


      } 
     }); 

animatedview.java: 

public class AnimatedView extends ImageView{ 

    private Context mContext; 
    int x = -1; 
    int y = -1; 
    private int xVelocity = 10; 
    private int yVelocity = 5; 
    private Handler h; 
    private final int FRAME_RATE = 30; 
    BitmapDrawable ball; 

    public AnimatedView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mContext = context; 
     h = new Handler(); 
    } 

    private Runnable r = new Runnable() { 
     @Override 
     public void run() { 
      invalidate(); 
     } 
    }; 

    @Override 
    protected void onDraw(Canvas c) { 

     BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball); 
     if (x<0 && y <0) { 
      x = this.getWidth()/2; 
      y = this.getHeight()/2; 
     } else { 
      x += xVelocity; 
      y += yVelocity; 
      if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) { 
       xVelocity = xVelocity*-1; 
      } 
      if ((y > this.getHeight() - ball.getBitmap().getHeight()) || (y < 0)) { 
       yVelocity = yVelocity*-1; 
      } 
     } 
     c.drawBitmap(ball.getBitmap(), x, y, null); 

     h.postDelayed(r, FRAME_RATE);  

    } 


main.xml 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="#000000"> 

<com.example.example.AnimatedView 
     android:id="@+id/anim_view" 
     android:layout_width="fill_parent" 
     android:layout_height="420dp" 
     android:onClick="imageClicked" /> 
</LinearLayout> 

回答

0

您需要實現onTouch(MotionEvent事件),然後您需要檢查您當前的觸摸座標是否與位圖的座標匹配,如果是,那麼您需要實現您想要執行的操作..以下是一個示例這樣做..

@Override 
public boolean onTouchEvent(MotionEvent event) { 
// TODO Auto-generated method stub 
int touchType = event.getAction(); 
float x,y; 
switch(touchType){ 
    case MotionEvent.ACTION_DOWN: 
    //x and y give you your touch coordinates 
     x = event.getX(); 
     y = event.getY(); 
     /*you just have to chek that your touch coordinates matches 
     with current coordinates of bitmap*/ 
       if(your match is true) 
       //do what you want to do.. 
     break; 
    case MotionEvent.ACTION_UP : 

     break; 

    case MotionEvent.ACTION_MOVE: 

      break; 

    } 
    return true; 
} 
+0

感謝你的迴應沙基,其實我是新的android,如果可能的話,請你修改我的代碼根據我的要求,我知道這是不好的請求其他明智的我會嘗試。 – kumar

+0

我沒有那麼多時間..你仍然可以給我的代碼..我會嘗試,如果我得到的時間來修改.. –

+0

thanku的朋友,我已經很粘貼上述總碼除了animation.xml <翻譯的xmlns:機器人=「http://schemas.android.com/apk/res/android」 android:interpolator =「@ android:anim/cycle_interpolator」 android:duration =「4000」 android:fromXDelta =「0%p」 機器人:toXDelta = 「0%p」 機器人:toYDelta = 「10%」 機器人:fromYDelta = 「0%」 機器人:repeatCount = 「無限」 機器人:REPEATMODE = 「反向」 /> – kumar