Android-OnTouchDon';行不通

Android-OnTouchDon';行不通,android,ontouch,Android,Ontouch,这是我的代码: public class ViewSecond extends View implements View.OnTouchListener{ private Context context; private Rect tailleEcran; private Vibrator vibrator; private Paint paint; private Paint monPerso = new Paint(); private Paint flecheHaut = new Pa

这是我的代码:

public class ViewSecond extends View implements View.OnTouchListener{

private Context context;
private Rect tailleEcran;
private Vibrator vibrator;

private Paint paint;
private Paint monPerso = new Paint();
private Paint flecheHaut = new Paint();
private Paint flecheBas = new Paint();
private Paint flecheGauche = new Paint();
private Paint flecheDroite = new Paint();

private Bitmap bmp, bmpFlecheHaut, bmpFlecheBas, bmpFlecheGauche, bmpFlecheDroite;

private Carte carteActuelle;

public ViewSecond(Context context) {
    super(context);
    this.context = context;
    this.vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    this.tailleEcran = getWindowSize();

    this.paint = new Paint();
    this.paint.setAntiAlias(true);
    this.paint.setStyle(Paint.Style.FILL);

    // ON S'OCCUPE DU PERSO PRINCIPAL
    monPerso.setColor(Color.RED);
    monPerso.setAntiAlias(false);
    // ON S'OCCUPE DE LA CARTE
    this.carteActuelle = new Carte(1,1,"lol");
    // ON S'OCCUPE DES FLECHES DIRECTIONNEL
    this.flecheHaut   = new Paint();
    this.flecheBas    = new Paint();
    this.flecheGauche = new Paint();
    this.flecheDroite = new Paint();
    bmp             = BitmapFactory.decodeResource(getResources(),R.drawable.flechehaut);
    bmpFlecheHaut   = Bitmap.createScaledBitmap(bmp,(int)(tailleEcran.exactCenterX()*2)/8,(int)(tailleEcran.exactCenterY()*2)/8,false);
    bmp             = BitmapFactory.decodeResource(getResources(),R.drawable.flechebas);
    bmpFlecheBas    = Bitmap.createScaledBitmap(bmp,(int)(tailleEcran.exactCenterX()*2)/8,(int)(tailleEcran.exactCenterY()*2)/8,false);
    bmp             = BitmapFactory.decodeResource(getResources(),R.drawable.flechegauche);
    bmpFlecheGauche = Bitmap.createScaledBitmap(bmp,(int)(tailleEcran.exactCenterX()*2)/8,(int)(tailleEcran.exactCenterY()*2)/8,false);
    bmp             = BitmapFactory.decodeResource(getResources(),R.drawable.flechedroite);
    bmpFlecheDroite = Bitmap.createScaledBitmap(bmp,(int)(tailleEcran.exactCenterX()*2)/8,(int)(tailleEcran.exactCenterY()*2)/8,false);

}

public boolean onTouch(View v, MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    if(((x>100) && (x<550)) && ((y>100) && (y<550))){
        this.vibrator.vibrate(500);
    }
    this.invalidate();
    return false;
}

public void onClick(View v) {
    this.paint.setColor(Color.RED);
    this.invalidate();
}

protected void onDraw(Canvas canvas){
    canvas.drawColor(Color.BLACK);
    canvas.drawCircle(10, 10, 10, monPerso);
    canvas.drawBitmap(bmpFlecheHaut,(int)((tailleEcran.exactCenterX()*2)/8)+10,(int)((tailleEcran.exactCenterY()*2)-(((tailleEcran.exactCenterY()*2)/8)*2)-10),null);
    canvas.drawBitmap(bmpFlecheBas,(int)((tailleEcran.exactCenterX()*2)/8)+10,(int)((tailleEcran.exactCenterY()*2)-(tailleEcran.exactCenterY()*2)/8),null);
    canvas.drawBitmap(bmpFlecheGauche,0,(int)((tailleEcran.exactCenterY()*2)-(tailleEcran.exactCenterY()*2)/5.5),null);
    canvas.drawBitmap(bmpFlecheDroite,(int)((tailleEcran.exactCenterX()*2)/4)+20,(int)((tailleEcran.exactCenterY()*2)-(tailleEcran.exactCenterY()*2)/5.5),null);
}

// CETTE FONCTION RENVOIE UN RECTANGE DE LA TAILLE DE L'ECRAN
Rect getWindowSize() {
    Rect r = new Rect();
    WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    display.getRectSize(r);
    return r;
}}
公共类ViewSecond扩展视图实现View.OnTouchListener{
私人语境;
私人直尾吊;
私人振动器;
私人油漆;
private Paint monPerso=新油漆();
私人油漆Flechhaut=新油漆();
私有油漆flecheBas=新油漆();
私人油漆flecheGauche=新油漆();
私人油漆flecheDroite=新油漆();
私人位图bmp、bmpFlecheHaut、bmpFlecheBas、bmpFlecheGauche、BMPFlechedriote;
私人点菜;
公共视图秒(上下文){
超级(上下文);
this.context=上下文;
this.可控震源=(可控震源)context.getSystemService(context.可控震源\u服务);
this.tailleEcran=getWindowsSize();
this.paint=新油漆();
this.paint.setAntiAlias(true);
this.paint.setStyle(paint.Style.FILL);
//论人本占有
蒙Perso.setColor(颜色:红色);
monPerso.setAntiAlias(假);
//论点菜占领
this.carteActuelle=新的Carte(1,1,“lol”);
//关于S'占据斑点方向
this.flechhaut=新油漆();
this.flecheBas=新油漆();
this.flecheGauche=新油漆();
this.flecheDroite=新油漆();
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.flechhaut);
bmpFlecheHaut=Bitmap.createScaledBitmap(bmp,(int)(tailleCran.exactCenterX()*2)/8,(int)(tailleCran.exactCenterY()*2)/8,false);
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.flechebas);
bmpFlecheBas=Bitmap.createScaledBitmap(bmp,(int)(tailleCran.exactCenterX()*2)/8,(int)(tailleCran.exactCenterY()*2)/8,false);
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.flechegauche);
bmpFlecheGauche=Bitmap.createScaledBramp(bmp,(int)(taileeCran.exactCenterX()*2)/8,(int)(taileeCran.exactCenterY()*2)/8,false);
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.flechedroite);
bmpFlecheDroite=Bitmap.createScaledBitmap(bmp,(int)(tailleCran.exactCenterX()*2)/8,(int)(tailleCran.exactCenterY()*2)/8,false);
}
公共布尔onTouch(视图v,运动事件){
float x=event.getX();
float y=event.getY();

if((x>100)和&&(x100)和&(yHi)如果要覆盖公共布尔onTouch(视图v,运动事件)。 如果要处理整个控件,则返回false,否则返回true。 如果您返回false,请更新它并尝试返回true。例如,请参见下面的onTouch视图方法

public boolean onTouch(View view, MotionEvent event) {
    final int X = (int) event.getRawX();
    final int Y = (int) event.getRawY();
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            break; 
        case MotionEvent.ACTION_UP:
            break; 
        case MotionEvent.ACTION_POINTER_DOWN:
            break; 
        case MotionEvent.ACTION_POINTER_UP:
            break; 
        case MotionEvent.ACTION_MOVE:
            break; 
    } 
    _root.invalidate();
    return true; 
}} 

看到它返回true。谢谢。

你应该使用
onTouchEvent()
而不是简单的
onTouch()
并在其中返回
true
。如果你返回
true
,这意味着你已经使用了touch事件,你告诉Android它不需要进一步处理事件

public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    if(((x>100) && (x<550)) && ((y>100) && (y<550))){
        this.vibrator.vibrate(500);
    }
    this.invalidate();
    return true;
}
public boolean onTouchEvent(运动事件){
float x=event.getX();
float y=event.getY();

如果(((x>100)和(&&&(x100)和(&&)实现onTouchListener以检测触摸…作为noob,…sry.但不工作:/如果您正在扩展视图,请使用onTouchEvent,而不是onTouch