Ontouch() 不适用于自定义视图? [英] Ontouch() isn't work with custom view?

查看:39
本文介绍了Ontouch() 不适用于自定义视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它扩展了View"并实现了OnTouchListener",因此它会自动覆盖 ontouch 方法,但此方法不起作用.我尝试通过在此方法中放置断点来进行调试,但没有调用它.

I have a class it extends "View" and Implement "OnTouchListener" so it's will override ontouch method automatically but this method not work. I try to debug by put breakpoint in this method but it isn't called.

我尝试从其他论坛解决此问题.他们告诉 onTouch() 必须返回 true;但也好不到哪里去.

I try to this problem from another forums. they tell that onTouch() must return true; But it's not better.

我的课堂问题:

public class Viewer extends View implements  OnTouchListener{

/** Constant tile width. */
public static final int TILEW = 32;
/** Constant tile height. */
public static final int TILEH = 70;
/** Constant tile shift due to increased level. */
public static final int TILESKEW = 7;   
/** Tile images. */
private Bitmap[][] tileImages;
/** Highlighted tile images. */
private Bitmap[][] tileImagesHL;
/** Board itself. */
private Board b;
private Tile[][][] tTile;
private float screenWidth  = Defines.getScreenWidth();
private float screenHeight = Defines.getScreenHeight();

private float offsetImg = Defines.getOffsetImage();
private float reScalePosX = Defines.getReScalePosX();
private float reScalePosY = Defines.getReScalePosY();

private boolean firstIter;

/** Stores selected tiles */
private ArrayList<Tile> selectedTiles;// = new ArrayList<Tile>();

/** Stores highlighted tiles by Hint */
private ArrayList<Tile> highlightedTiles;// = new ArrayList<Tile>();

//////////////////////////////////////////////////
private static RectF screen;
private Images imgObj;  

public Viewer(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    imgObj = new Images(context);   
    b = new MainActivity().board;

    Defines.setScreenRender(1280, 800);     
    makeTileImages(Board.MAXGROUPS);    
}

@Override
protected void onDraw(Canvas canvas){   
    drawBackGround(imgObj.bg01, canvas);    
    try{
        tTile = b.getContent();
        if(tTile==null || tileImages==null){
            return;
        }
        drawTile(tTile, canvas);


    }catch (Exception e) {
        // TODO: handle exception                       
    }
}


@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    Log.d("Mydebug", "YEHHHHHH");
    Tile[][][] content = b.getContent();
    for (int i = 0; i < content.length; i++) {
        for (int y = 0; y < content[i].length; y++) {
            for (int x = 0; x < content[i][y].length; x++) {
                processMouseClick(content, i, y , x, event);
            }
        }
    }               
    return true;
}



public void drawBackGround(Bitmap bitmapBG,Canvas canvas){      
    screen = new RectF(0.0f, 0.0f, Defines.getScreenWidth(),Defines.getScreenHeight());
    canvas.drawBitmap(bitmapBG, null, screen, null);

}

private void makeTileImages(int groups){
    tileImages = loadTileset(groups, "unlit_");
    tileImagesHL = loadTileset(groups, "lit_");
}

private Bitmap[][] loadTileset(int groups,String prefix){
    Bitmap[][] tr = new Bitmap[groups][Board.GROUPSIZE];        
    try{
        InputStream is =  this.getResources().getAssets().open("Tiles/tiles.set");
        BufferedReader f = new BufferedReader(new InputStreamReader(is));
        String s = f.readLine();
        while (s!=null) {
            if (!s.startsWith("#")) {
                String[] tokens = s.split(" ");
                try{
                    int groupNum =Integer.parseInt(tokens[0]);
                    for(int i =1; i<=(Board.GROUPSIZE);i++){    
                            String pathPNG = "Tiles/"+prefix+tokens[i]+".png";
                            Bitmap img = imgObj.loadImage(pathPNG);
                            img = imgObj.resizeBitmap(img,Tile.tileHeight, Tile.tileWidth);
                            //Bitmap img = loadImage(pathPNG);  
                            //Log.d("Mydebug", "PATH:"+pathPNG);
                            tr[groupNum][i-1] = img;
                    }       

                }catch (Exception e) {
                    // TODO: handle exception
                    Log.d("Mydebug", "File error TIles/tile.set");
                }

            }
            s = f.readLine();
        }
    }catch (Exception e) {
        // TODO: handle exception

    }       
    return tr;
}


public void drawTile(Tile[][][] content,Canvas canvas){
    for (int i = 0; i < content.length; i++) {
        for (int y = 0; y < content[i].length; y++) {
            for (int x = 0; x < content[i][y].length; x++) {
                final Tile t = content[i][y][x];
                if (t != null) {
                    if (y>0 && content[i][y-1][x]!=null && t.equals(content[i][y-1][x])) {
                        continue;
                    }
                    if (x>0 && content[i][y][x-1]!=null && t.equals(content[i][y][x-1])) {
                        continue;
                    }
                    Bitmap image = tileImages[t.getValue()][t.getSubindex()];

                    if (b.free(t)) {
                        image = tileImagesHL[t.getValue()][t.getSubindex()];
                    }       
                        /* Draw tile images to panel */
                    //canvas.drawBitmap(image, x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, null);
                    canvas.drawBitmap(image, (x*TILEW+TILEW/2+i*TILESKEW)+200, ((y+1)*TILEH/2-i*TILESKEW)+100, null);

                }
            }
        }
    }
}


/** A helper function for the Viewer constructor which accompanies the mouseListener object on each tiles
 * @param content The current board's 3D array of positions
 * @param i The current tile's x position
 * @param y The current tile's y position
 * @param x The current tile's z position
 * @param me The mouseEvent for each tile
 */
private void processMouseClick(Tile[][][] content, int i, int y, int x, MotionEvent touchPos){
    final Tile t = content[i][y][x];

    if (t != null) {
        if (y>0 && content[i][y-1][x]!=null && t.equals(content[i][y-1][x])) {
            return;
        }
        if (x>0 && content[i][y][x-1]!=null && t.equals(content[i][y][x-1])) {
            return;
        }

        Bitmap image = tileImages[t.getValue()][t.getSubindex()];

        if (b.free(t)) {
            image = tileImagesHL[t.getValue()][t.getSubindex()];                
        }


        // Rectangle representing space of tile 
        final Rect rect = new Rect(x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, image.getWidth(), image.getHeight());

        //if ((rect.contains(me.getPoint())) && b.free(t) && t.isVisible()) {
        if ((rect.contains((int)touchPos.getX(), (int)touchPos.getY()) && b.free(t))){
            t.wasClicked = true;
            firstIter = false;
            Log.d("MyDebug", "Clicked ME");
            //if (content[i][y][x].isVisible()){
                     //Add corresponding JLabel to panel                    
            //      t.tLabel.setBounds(x*TILEW+TILEW/2+i*TILESKEW, (y+1)*TILEH/2-i*TILESKEW, image.getWidth(null), image.getHeight(null));
            //      t.tLabel.setVisible(false); 
            //      t.tLabel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
            //}             
            //validClick(t); 
        }
    }
}
}

推荐答案

移除 implements OnTouchListener 并覆盖方法:

public boolean onTouch(MotionEvent event)

更新

当然是onTouchEvent,而不是onTouch

of course it's onTouchEvent, not onTouch

这篇关于Ontouch() 不适用于自定义视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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