2015-11-24 72 views
3

我正在创建一个可重用的类,它实现了onclick和一个设置onclick的方法。该类将代表在画布上绘制的“按钮”。我知道使用x和y位置来评估按钮是否被按下。Android类实现OnClick和setOnClick方法

如果你看看代码,你可以看到一个isPressed实现,它只是返回这个值(这只适用于澄清)。

我需要的是能够给其在画布上绘制这就是为什么我想实现一个onclick监听器不同行为的每个项目

public class CanvasButton extends CanvasItem implements View.OnClickListener { 
    private View.OnClickListener ClickListener; 

    public CanvasButton(float x, float y, int width, int height){ 
     super(x, y, width, height); 
    } 

    public boolean isPressed(float x, float y){ 
     return isTouched(x, y); 
    } 

    public void setOnClickListener(View.OnClickListener listener){ 
     this.ClickListener = listener; 
    } 

    @Override 
    public void onClick(View v) { 
    } 
} 

似乎无法弄清楚如何继续

更新:我想我给了从提供的代码错误的印象。我正在尝试View.OnClickListener,但这只是我认为的一个选项。任何类似于上述onclick监听器的方法都可以。

回答

0

感谢@mdtuyen's answer我能装备的东西了,对我的作品。 我实现了一个基CanvasItem类,我现在可以继承这样的伎俩(我离开了不相关的类的方法

import android.view.View; 

public class CanvasItem { //notice does not need to implement onClickListener 

    //this is used to get the click 
    private View.OnClickListener onClickListener; 

    protected CanvasItem(){ 
     /* contructor logic goes here */ 
     /* at minimum x and y and width and height is needed */ 
    } 

    protected boolean isHit(float x, float y){ 
     /* left() right() top() and bottom() use widths and height from my constructor */ 
     /* x and y is passed from canvas ontouch */ 
     if(x >= left() && x <= right() && y >= top() && y <= bottom()){ 

      //fire any onclick listeners 
      if(onClickListener != null){ 
       onClickListener.onClick(null); 
      } 

      //return true 
      return true; 
     } 

     //not touched 
     return false; 
    } 

    protected void setOnTouchListener(View.OnClickListener clickListener){ 
     onClickListener = clickListener; 
    } 
} 

一旦这被实现我使用继承CanvasItem CanvasItem或类的ArrayList那运行以确定isHit()需要

public boolean onTouchEvent(MotionEvent event) { 

     final int action = event.getAction(); 

     float x = event.getX(); 
     float y = event.getY(); 

     switch (action & MotionEvent.ACTION_MASK) { 
      case MotionEvent.ACTION_DOWN: 
       for(CanvasItem itm : mItems){ 
        //if item istouched 
        if(itm.isHit(x, y)){ 
         /* Logic can go here to alter any properties */ 
        } 
       } 
     } 
    } 

没什么到位if语句一旦item.isHit()被触发,则的onClick被激发。

是的,这实现了类似于通常做,但使用的onClick什么我可以明确的触摸动作,而管理和跟踪的项目在其上的项目更大的任务,在画布

上是 拖动帆布
2

的OnClick需要有格式:

@Override 
public void onClick(View v) { 
    if (ClickListener != null){ 
     ClickListener.onClick(v); 
    } 
    //The code to reuse 
} 
1

只能添加onClickListeners和的onClick方法的意见。画布及其内的任何东西都不是视图,也不可点击。使用可点击按钮的画布的唯一方法是将整个画布放在视图或扩展View的类中,重写onTouch方法,然后在该方法中获取触摸的位置并查看它们是否位于边界内你的任何按钮,所以你可以运行一些代码。这里是一些其他StackOverflow问题的链接。

onClickListener in Canvas

add touch event listener to android canvas

public class MyCanvasView extends View implements OnTouchListener { 

    Bitmap button1; 
    Bitmap button2; 
    Bitmap button3; 
    Bitmap button4; 

    public MyCanvasView(Context context) { 
     super(context); 
     setFocusable(true); 
     setFocusableInTouchMode(true); 
     this.setOnTouchListener(this); 
    } 

    public void onDraw(Canvas canvas) { 
     paint = new Paint(); 
     canvas.drawBitmap(button1, 10, 10, paint); 
     canvas.drawBitmap(button2, 60, 10, paint); 
     canvas.drawBitmap(button3, 110, 10, paint); 
     canvas.drawBitmap(button4, 160, 10, paint); 
    } 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     int x = event.getX(); 
     int y = event.getY(); 
     if (x >= 60 && x <= (60 + canvasButtonWidth)){ 
      if (y >= 10 && x <= (10 + canvasButtonHeight)) { 
       //Insert code you want to use when button2 is clicked 
      } 
     } 
     /*Copy and paste the pair of if statements above for each button you have 
     /drawn on the canvas*/ 
     return true; 
    } 
} 
+0

我理解这种方法,因为我已经使用过很多次了。问题是我的按钮是可拖动的,每个按钮都会触发不同的操作(音频播放等),所以这使得这种方法在我看来有点麻烦 –